useful.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import threading
  2. #
  3. def parse_bytes(bytes_str):
  4. conversions = {'B':1, 'KiB':1024, 'MiB':1024**2, 'GiB':1024**3, 'TiB':1024**4}
  5. #
  6. matching_conversions = [x for x in conversions if bytes_str.endswith(x)]
  7. if len(matching_conversions) > 0:
  8. # if any conversion suffix matched
  9. most_precise_match = max(matching_conversions, key=len)
  10. number = int(bytes_str[:-len(most_precise_match)])
  11. return number*conversions[most_precise_match]
  12. #
  13. return int(bytes_str)
  14. #
  15. def parse_range_list(range_list_str):
  16. """
  17. Take an input like '1-3,5,7-10' and return a list like [1,2,3,5,7,8,9,10].
  18. """
  19. #
  20. range_strings = range_list_str.split(',')
  21. all_items = []
  22. for range_str in range_strings:
  23. if '-' in range_str:
  24. # in the form '12-34'
  25. range_ends = [int(x) for x in range_str.split('-')]
  26. assert(len(range_ends) == 2)
  27. all_items.extend(range(range_ends[0], range_ends[1]+1))
  28. else:
  29. # just a number
  30. all_items.append(int(range_str))
  31. #
  32. #
  33. return all_items
  34. #
  35. def generate_range_list(l):
  36. """
  37. Take a list like [1,2,3,5,7,8,9,10] and return a string like '1-3,5,7-10'.
  38. """
  39. #
  40. l = list(set(sorted(l)))
  41. ranges = []
  42. current_range_start = None
  43. #
  44. for index in range(len(l)):
  45. if current_range_start is None:
  46. current_range_start = l[index]
  47. else:
  48. if l[index] != l[index-1]+1:
  49. ranges.append((current_range_start, l[index-1]))
  50. current_range_start = l[index]
  51. #
  52. #
  53. #
  54. ranges.append((current_range_start, l[-1]))
  55. #
  56. return ','.join(['-'.join([str(y) for y in x]) if x[0] != x[1] else str(x[0]) for x in ranges])
  57. #
  58. class QueueGetter:
  59. def __init__(self, queue, callback):
  60. self.queue = queue
  61. self.callback = callback
  62. #
  63. self.t = threading.Thread(target=self._unqueue)
  64. self.t.start()
  65. #
  66. def _unqueue(self):
  67. while True:
  68. val = self.queue.get()
  69. if val is None:
  70. break
  71. #
  72. self.callback(val)
  73. #
  74. #
  75. def stop(self):
  76. self.queue.put(None)
  77. #
  78. def join(self, timeout=None):
  79. self.t.join(timeout=timeout)
  80. if self.t.is_alive():
  81. raise TimeoutError('Could not join QueueGetter thread')
  82. #
  83. #
  84. #