changeset 853:5e05c41337e3

Example: generate passwords
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:18:29 -0800
parents 565875bc93b7
children 66321798ac82
files python/password.py
diffstat 1 files changed, 36 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/password.py	Sun Dec 10 17:18:29 2017 -0800
@@ -0,0 +1,36 @@
+#!/usr/bin/env python
+
+"""
+password generator
+"""
+
+import argparse
+import random
+import string
+import sys
+
+
+def generate_password(population=string.ascii_letters+string.digits,
+                      length=12):
+    """
+    returns a random password `length` characters long
+    sampled from `population`
+    """
+
+    return ''.join(random.sample(population, length))
+
+
+def main(args=sys.argv[1:]):
+
+    # parse command line
+    parser = argparse.ArgumentParser(description=__doc__)
+    parser.add_argument('-l', '--length', dest='length',
+                        type=int, default=12,
+                        help="length of password to generate [DEFAULT: %(default)s]")
+    options = parser.parse_args(args)
+
+    # print generated password
+    print (generate_password(length=options.length))
+
+if __name__ == '__main__':
+    main()