utilities.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import uuid
  2. import hashlib
  3. from .. import constants
  4. ROUND = constants.DEFAULT_PRECISION
  5. def bit_mask(flags):
  6. bit = 0
  7. true = lambda x,y: (x | (1 << y))
  8. false = lambda x,y: (x & (~(1 << y)))
  9. for mask, position in constants.MASK.items():
  10. func = true if flags.get(mask) else false
  11. bit = func(bit, position)
  12. return bit
  13. def hash(value):
  14. hash_ = hashlib.md5()
  15. hash_.update(repr(value).encode('utf8'))
  16. return hash_.hexdigest()
  17. def id():
  18. return str(uuid.uuid4()).upper()
  19. def rgb2int(rgb):
  20. is_tuple = isinstance(rgb, tuple)
  21. rgb = list(rgb) if is_tuple else rgb
  22. colour = (int(rgb[0]*255) << 16) + (int(rgb[1]*255) << 8) + int(rgb[2]*255)
  23. return colour
  24. def round_off(value, ndigits=ROUND):
  25. is_tuple = isinstance(value, tuple)
  26. is_list = isinstance(value, list)
  27. value = list(value) if is_tuple else value
  28. value = [value] if not is_list and not is_tuple else value
  29. value = [round(val, ndigits) for val in value]
  30. if is_tuple:
  31. value = tuple(value)
  32. elif not is_list:
  33. value = value[0]
  34. return value
  35. def rounding(options):
  36. round_off = options.get(constants.ENABLE_PRECISION)
  37. if round_off:
  38. round_val = options[constants.PRECISION]
  39. else:
  40. round_val = None
  41. return (round_off, round_val)