简体   繁体   English

在python中以大写和小写开头的密码生成

[英]Password generating that starts with uppercase and lower case in python

It should be uppercase and lowercase first. 首先应该是大写和小写。

Then it should be numbers and characters. 然后应该是数字和字符。

Then it should be a mixed of all characters. 然后,它应该是所有字符的混合。

import random
pswd = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
numbrs = "0123456789"
chars = "@#$&*_-:;',./^"
def pswd_generator(data):
    if data == 1:
       genpswd = "".join(random.sample(pswd,size))
    elif data == 2:
       genpswd ="".join(random.sample(pswd+numbrs+chars,size))
    elif data == 3:
       genpswd = "".join(random.sample(pswd+numbrs+chars,size))
       print "Your Generated Password is:",genpswd
m = False
while not m:
    size = int(raw_input("How many digits of password do you want to generate: "))
    print "1.Weak Password"
    print "2.Medium Password"
    print "3.Strong Password"
    print ("Select 1,2,3")
    data = int(raw_input("Enter the option: "))
    if data == 1:
      m = True
    elif data == 2:
        m = True
    elif data == 3:
        m = True
    else:
        print "Wrong option.Try again"
pswd_generator(data)

How can I achieve in pythonic way? 如何以pythonic方式实现?

To clean it up and make it a bit more Pythonic along with meeting the goal of your password requirements I'd suggest the following: 为了进行清理,使其更加符合Python规范,同时满足密码要求的目标,我建议采取以下措施:

  • Change your pswd_generator function so that it accepts two arguments, an option and size . 更改您的pswd_generator函数,使其接受两个参数: optionsize The reason for this is that you're relying on the size variable being created before the call to this function... pswd_generator is only working because size is in the global scope after the size = 这样做的原因是,你依靠size变量调用此函数之前创建... pswd_generator只工作,因为size是在后全球范围内size =
  • Do some very basic error checking in pswd_generator to ensure the option being passed in is valid. pswd_generator执行一些非常基本的错误检查,以确保传递的选项有效。
  • Add spaces in between the arithmetic operators. 在算术运算符之间添加空格。 Readability counts for a lot in Python. 在Python中,可读性至关重要。
  • Return the password from this function. 从此功能返回密码。 If a function appears to be "generating" something, it should return it to the caller. 如果某个函数似乎正在“生成”某些东西,则应将其返回给调用者。 This has the side benefit of making this function easy to test if you ever wrote unit tests. 如果您曾经编写过单元测试,则具有使该功能易于测试的附带好处。
  • Determine a minimum length for your password...like 6. 确定密码的最小长度...如6。
  • Pick some size for the different groups within your generate password, ie, the prefix which starts with upper an lowercase, based on the desired length. 为生成的密码中的不同组选择一些大小,即根据所需的长度,以大写小写开头的前缀。 Do the same thing for the next group. 对下一组执行相同的操作。 No you'll know how many characters to choose for each group. 不,您不会知道每个组要选择多少个字符。 This is demonstrated below in option 2 for demonstration. 这在下面的选项2中进行了演示。
def pswd_generator(option, size):
    assert(option in (1, 2, 3))

    size = max(6, size)
    prefix_size = min(5, size / 3)
    number_size = min(5, size / 3)
    filler_size = size - prefix_size - number_size

    if option == 1:
       genpswd = "".join(random.sample(pswd, size))
    elif option == 2:
       genpswd = "".join(
           random.sample(pswd, prefix_size) +
           random.sample(numbrs + chars, number_size) +
           random.sample(pswd + numbrs + chars, filler_size)
       )  
    else:
       genpswd = "".join(random.sample(pswd + numbrs + chars, size))

    return genpswd

For your main loop I'd suggest: 对于您的主循环,我建议:

  • Changing the while condition to simply look at whether a password has been created. 更改while条件以仅查看是否已创建密码。 The way it's setup now you're using a variable to just break out of the loop...there are simpler ways to accomplish that. 现在,它的设置方式是使用变量来打破循环……有更简单的方法来实现。
  • In order to check for the correct option you can tighten this up by looking for whether the input value is greater than zero and less than four. 为了检查正确的选项,您可以通过查看输入值是否大于零且小于四来加强此功能。 If it is, set the psswd variable which you can then print out and which will also terminate your loop. 如果是这样,请设置psswd变量,然后可以将其打印输出,该变量也将终止循环。
psswd = None
while not psswd:
    # stuff

    option = int(raw_input("Enter the option: "))
    if 0 < option < 4:
        psswd = pswd_generator(option, size)
        print "Your Generated Password is: ", psswd
    else:
        print "Wrong option.Try again"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM