Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. import random
  4. import regex
  5. import numpy as np
  6.  
  7. from pprint import pprint
  8. from PyInquirer import style_from_dict, Token, prompt, print_json
  9. from PyInquirer import Validator, ValidationError
  10.  
  11. style = style_from_dict({
  12. Token.QuestionMark: '#E91E63 bold',
  13. Token.Selected: '#673AB7 bold',
  14. Token.Instruction: '', # default
  15. Token.Answer: '#2196f3 bold',
  16. Token.Question: '',
  17. })
  18.  
  19. words = ["pipas", "agua", "libreta", "ventana", "alfred", "guason", "bromas", "shyamalan", "brucewillis"]
  20.  
  21. def generate(length, diff):
  22. chain = ""
  23. if diff == "hard":
  24. for _ in range(length):
  25. chain += selectRandomChar()
  26. else:
  27. for _ in range(random.randrange(1,3)):
  28. chain += random.choice(words)
  29. for _ in range(4):
  30. chain += str(random.randrange(10))
  31. return chain
  32.  
  33. ###### AUXILIARY FUNCTIONS ######
  34.  
  35. def selectRandomChar():
  36. switcher = {
  37. '1': random.choice('abcdefghijklmnopqrstuvwxyz'),
  38. '2': random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ'),
  39. '3': str(random.randrange(10)),
  40. '4': random.choice('{}[]()#:;^?.,!|&_~@$%/=+-*')
  41. }
  42. return switcher.get(np.random.choice(np.arange(1,5), p=[0.55, 0.25, 0.15, 0.05]).astype(str))
  43.  
  44. def hard_question(answers):
  45. return answers['difficulty'] == 'hard'
  46.  
  47. def __main__():
  48. print("Hi. let's set up your password.")
  49.  
  50. questions = [
  51. {
  52. 'type': 'list',
  53. 'name': 'difficulty',
  54. 'message': 'Do you want a simple or a hard password?',
  55. 'choices': ['hard', 'simple']
  56. },
  57. {
  58. 'type': 'input',
  59. 'name': 'length',
  60. 'message': 'How long do you want your password to be? Please input the number of characters.',
  61. # 'validate': NumberValidator,
  62. 'when': hard_question,
  63. 'filter': lambda val: int(val)
  64. }]
  65.  
  66. answers = prompt(questions, style=style)
  67. print('And the generated password is:')
  68. password = generate(answers.get('length'), answers.get('difficulty'))
  69. print(password)
  70. #print_json(answers)
  71.  
  72. f = open('password.txt', 'w+')
  73. f.write(password)
  74. print('Your password has been saved in the file "password.txt"')
  75.  
  76.  
  77. __main__()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement