util.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from random import random
  2. import tabulate
  3. # noinspection SpellCheckingInspection
  4. salt = 'orderer_is_a_cool_application_]{][{²$%WT§$%GV§$%SF$%&S$%FGGFHBDHJZIF254325'
  5. chars = [str(d) for d in range(1, 10)]
  6. ps = [1. for _ in chars]
  7. # the first number is absolute and the second relative
  8. letter_dist = [("E", 21912, 12.02), ("T", 16587, 9.1), ("A", 14810, 8.12), ("O", 14003, 7.68), ("I", 13318, 7.31),
  9. ("N", 12666, 6.95), ("S", 11450, 6.28), ("R", 10977, 6.02), ("H", 10795, 5.92), ("D", 7874, 4.32),
  10. ("L", 7253, 3.98), ("U", 5246, 2.88), ("C", 4943, 2.71), ("M", 4761, 2.61), ("F", 4200, 2.3),
  11. ("Y", 3853, 2.11), ("W", 3819, 2.09), ("G", 3693, 2.03), ("P", 3316, 1.82), ("B", 2715, 1.49),
  12. ("V", 2019, 1.11), ("K", 1257, 0.69), ("X", 315, 0.17), ("Q", 205, 0.11), ("J", 188, 0.1),
  13. ("Z", 128, 0.07), ]
  14. sp = sum(ps)
  15. for row in letter_dist:
  16. chars.append(row[0])
  17. ps.append(float(row[2]))
  18. ps = [p / sum(ps) for p in ps]
  19. def choice(sequence, probabilities):
  20. # if sum(probabilities) != 1:
  21. # raise AssertionError('Probabilities must sum up to 1')
  22. r = random()
  23. for idx, c in enumerate(sequence):
  24. r -= probabilities[idx]
  25. if r < 0:
  26. return c
  27. raise AssertionError('Probabilities must sum up to 1')
  28. def random_chars(count):
  29. return ''.join(choice(chars, probabilities=ps) for _ in range(count))
  30. def str2bool(v):
  31. v = str(v).strip().lower()
  32. if v in ["yes", 'y' "true", "t", "1"]:
  33. return True
  34. if v in ["no", 'n' "false", "f", "0", '', 'null', 'none']:
  35. return False
  36. raise ValueError('Can not convert `' + v + '` to bool')
  37. def my_tabulate(data, **params):
  38. if data == [] and 'headers' in params:
  39. data = [(None for _ in params['headers'])]
  40. tabulate.MIN_PADDING = 0
  41. return tabulate.tabulate(data, **params)
  42. def yn_dialog(msg):
  43. while True:
  44. result = input(msg + ' [y/n]: ')
  45. if result == 'y':
  46. return True
  47. if result == 'n':
  48. return False
  49. print('Type in \'y\' or \'n\'!')