util.py 2.0 KB

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