From: Fred Cox Date: Thu, 30 Nov 2017 09:18:45 +0000 (+0200) Subject: Refactor pass generation into a closure X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=2bde4f28ffb00af9e72cd5f482edf4d944c8b9b9;p=redacted-XKCD-password-generator.git Refactor pass generation into a closure --- diff --git a/xkcdpass/xkcd_password.py b/xkcdpass/xkcd_password.py index 54024b2..0b74798 100755 --- a/xkcdpass/xkcd_password.py +++ b/xkcdpass/xkcd_password.py @@ -235,14 +235,17 @@ def generate_xkcdpassword(wordlist, if acrostic: worddict = wordlist_to_worddict(wordlist) - # useful if driving the logic from other code - if not interactive: + def gen_passwd(): if not acrostic: - passwd = delimiter.join(choose_words(wordlist, numwords)) + words = choose_words(wordlist, numwords) else: - passwd = delimiter.join(find_acrostic(acrostic, worddict)) + words = find_acrostic(acrostic, worddict) + + return delimiter.join(words) - return passwd + # useful if driving the logic from other code + if not interactive: + return gen_passwd() # else, interactive session # define input validators @@ -277,10 +280,7 @@ def generate_xkcdpassword(wordlist, accepted = False while not accepted: - if not acrostic: - passwd = delimiter.join(choose_words(wordlist, numwords)) - else: - passwd = delimiter.join(find_acrostic(acrostic, worddict)) + passwd = gen_passwd() print("Generated: " + passwd) accepted = try_input("Accept? [yN] ", accepted_validator)