Skip to content

Instantly share code, notes, and snippets.

@hayashikun
Created March 25, 2021 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayashikun/a12d1d351bccb9e5ac25f6e7c60d90af to your computer and use it in GitHub Desktop.
Save hayashikun/a12d1d351bccb9e5ac25f6e7c60d90af to your computer and use it in GitHub Desktop.
#!/bin/sh
current_dir=$(pwd)
script_dir=$(dirname "$0")
python $script_dir/pwg.py $@
import argparse
import random
word_list_path = "/usr/share/dict/words"
special_chars = "#@%$^+/!?(){}"
def gen(max_word_length, min_words_count, max_word_count, capitalize, specials):
with open(word_list_path) as fp:
words = [w for w in fp.read().split() if len(w) <= max_word_length]
words_num = random.randint(min_words_count, max_word_count)
rand_words = random.sample(words, words_num)
sep = [
''.join([
str(random.randint(0, 9)) for _ in range(random.randint(1, 4))
]) for _ in range(words_num)
]
if specials:
# 0b000 or 0b111 is not suitable
while not (flags := random.getrandbits(words_num)) and ~flags:
pass
sep = [random.choice(special_chars) if flags & 2 ** i else w for i, w in enumerate(sep)]
if capitalize:
# 0b000 is not suitable
while not (flags := random.getrandbits(words_num)):
pass
rand_words = [w.capitalize() if flags & 2 ** i else w for i, w in enumerate(rand_words)]
pw = "".join([w + d for w, d in zip(rand_words, sep)])
print(pw)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--max_word_length", default=12)
parser.add_argument("--min_words_count", default=3)
parser.add_argument("--max_word_count", default=5)
parser.add_argument("--no-capitalize", action="store_true")
parser.add_argument("--no-specials", action="store_true")
args = parser.parse_args()
gen(
int(args.max_word_length), int(args.min_words_count), int(args.max_word_count),
not args.no_capitalize, not args.no_specials
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment