[ create a new paste ] login | about

Link: http://codepad.org/WkWX1FUg    [ raw code | output | fork ]

Python, pasted on Feb 21:
#!/usr/bin/env python

import string
from time import time
from itertools import chain
from random import seed, choice, shuffle


def mkpasswd(length=8, digits=2, upper=2, lower=2):
    """Create a random password

    Create a random password with the specified length and no. of
    digit, upper and lower case letters.

    :param length: Maximum no. of characters in the password
    :type length: int

    :param digits: Minimum no. of digits in the password
    :type digits: int

    :param upper: Minimum no. of upper case letters in the password
    :type upper: int

    :param lower: Minimum no. of lower case letters in the password
    :type lower: int

    :returns: A random password with the above constaints
    :rtype: str
    """

    seed(time())

    letters = string.letters.strip("oO")

    password = list(
        chain(
            (choice(string.digits) for _ in range(digits)),
            (choice(string.uppercase) for _ in range(upper)),
            (choice(string.lowercase) for _ in range(lower)),
            (choice(letters) for _ in range((length - digits - upper - lower)))
        )
    )

    shuffle(password)

    return "".join(password)


print mkpasswd()
print mkpasswd(12)
print mkpasswd(digits=3)
print mkpasswd(12, upper=4)


Output:
1
2
3
4
LpORby32
Gu1KzFmfE7PL
Sw3yJQ85
Hec9S0VYLjIh


Create a new paste based on this one


Comments: