From f3a88b207e5e2c781374127c81c2924062757613 Mon Sep 17 00:00:00 2001 From: Timotej Lazar Date: Sat, 2 Mar 2019 13:14:38 +0100 Subject: kpov_util: better random strings and filenames Do not use hard-to-distinguish characters, and use shorter names by default. --- kpov_util.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/kpov_util.py b/kpov_util.py index 8434f85..4acaf79 100755 --- a/kpov_util.py +++ b/kpov_util.py @@ -39,11 +39,17 @@ def ssh_test(host, user, password, commands=()): results['ssh'] = 'connection to {} as {}/{} failed ({})'.format(host, user, password, e) return results -def alnum_gen(r, l=1): - s = "" - for i in range(l): - s += r.choice(string.ascii_letters + string.digits) - return s +# omit i, l, o, I, O, 1, 0 for readability +uppers = 'ABCDEFGHJKLMNPQRSTUVWXYZ' +lowers = 'abcdefghjkmnpqrstuvwxyz' +numbers = '23456789' +def alnum_gen(r, length=1, lower=True, upper=True): + return ''.join( + r.choice( + numbers + + (lowers if lower else '') + + (uppers if upper else '')) + for i in range(length)) def fortune(r, max_len): all_fortunes = [] @@ -112,8 +118,8 @@ def MAC_gen(r): [r.choice(s) + r.choice(s) for i in range(5)]) common_file_extensions = ['jpg', 'png', 'txt', 'doc', 'cfg', 'pdf', 'odt', 'cpp', 'c', 'sh', 'java'] -def fname_gen(r, extension = True): - s = alnum_gen(r, 8) +def fname_gen(r, extension=True): + s = alnum_gen(r, length=5, upper=False) if extension: s += '.' + r.choice(common_file_extensions) return s -- cgit v1.2.1