Commits

Anonymous committed 889a039

Added recipe: mkpasswd

Comments (0)

Files changed (1)

recipes/mkpasswd.py

+#!/usr/bin/env python
+
+import string
+from time import time
+from itertools import chain
+from random import seed, choice, sample
+
+
+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())
+
+    lowercase = string.lowercase.translate(None, "o")
+    uppercase = string.uppercase.translate(None, "O")
+    letters = "{0:s}{1:s}".format(lowercase, uppercase)
+
+    password = list(
+        chain(
+            (choice(uppercase) for _ in range(upper)),
+            (choice(lowercase) for _ in range(lower)),
+            (choice(string.digits) for _ in range(digits)),
+            (choice(letters) for _ in range((length - digits - upper - lower)))
+        )
+    )
+
+    return "".join(sample(password, len(password)))
+
+
+def test():
+    print mkpasswd()
+    print mkpasswd(12)
+    print mkpasswd(upper=3)
+
+
+if __name__ == "__main__":
+    test()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.