Some helpers without having a specific module
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import logging
  2. import numbers
  3. import os
  4. import time
  5. try:
  6. from config import APP_NAME as ROOT_LOGGER_NAME
  7. except ImportError:
  8. ROOT_LOGGER_NAME = 'root'
  9. logger = logging.getLogger(ROOT_LOGGER_NAME).getChild(__name__)
  10. class continues_statistic(dict):
  11. def __init__(self, mean=None, min_val=None, max_val=None, quantifier=None):
  12. dict.__init__(self)
  13. if mean is None:
  14. self.__init_data__(None, None, None, None)
  15. elif min_val is not None and max_val is not None and quantifier is not None:
  16. self.__init_data__(mean, min_val, max_val, quantifier)
  17. else:
  18. self.__init_data__(mean, mean, mean, 1)
  19. def __init_data__(self, mean, min_val, max_val, quantifier):
  20. self['quantifier'] = quantifier
  21. self['max_val'] = max_val
  22. self['min_val'] = min_val
  23. self['mean'] = mean
  24. def expand(self, value):
  25. if self['quantifier'] is None:
  26. self.__init_data__(value, value, value, 1)
  27. else:
  28. cs_sum = self + continues_statistic(value)
  29. for key in self:
  30. self[key] = cs_sum[key]
  31. def __add__(self, other):
  32. rv = continues_statistic()
  33. rv['mean'] = (self.mean * self.quantifier + other.mean * other.quantifier) / (self.quantifier + other.quantifier)
  34. rv['min_val'] = min(self.min, other.min)
  35. rv['max_val'] = max(self.max, other.max)
  36. rv['quantifier'] = self.quantifier + other.quantifier
  37. return rv
  38. @property
  39. def mean(self):
  40. return self.get('mean')
  41. @property
  42. def min(self):
  43. return self.get('min_val')
  44. @property
  45. def max(self):
  46. return self.get('max_val')
  47. @property
  48. def quantifier(self):
  49. return self.get('quantifier')
  50. def pop(self):
  51. rv = continues_statistic(self.mean, self.min, self.max, self.quantifier)
  52. self.__init_data__(None, None, None, None)
  53. return rv
  54. def __str__(self):
  55. return "mean=%(mean)s, min=%(min_val)s, max=%(max_val)s, quantifier=%(quantifier)s" % self
  56. class continues_statistic_multivalue(dict):
  57. def __init__(self, **kwargs):
  58. dict.__init__(self)
  59. for key in kwargs:
  60. if type(kwargs[key]) is continues_statistic:
  61. self[key] = kwargs[key]
  62. elif type(kwargs[key]) is dict:
  63. self[key] = continues_statistic(**kwargs[key])
  64. else:
  65. self[key] = continues_statistic(kwargs[key])
  66. def expand(self, **kwargs):
  67. for key in kwargs:
  68. if key in self:
  69. self[key].expand(kwargs[key])
  70. else:
  71. self[key] = continues_statistic(kwargs[key])
  72. def pop(self, key=None):
  73. if key is None:
  74. rv = continues_statistic_multivalue(**self)
  75. self.clear()
  76. return rv
  77. else:
  78. return self[key].pop()
  79. def __str__(self):
  80. return '\n'.join([key + ': ' + str(self[key]) for key in self])
  81. class ringbuffer(list):
  82. def __init__(self, *args, **kwargs):
  83. self.__max_length__ = kwargs.pop('length')
  84. list.__init__(self, *args, **kwargs)
  85. self.__reduce_list__()
  86. def __reduce_list__(self):
  87. while len(self) > self.__max_length__:
  88. self.pop(0)
  89. def append(self, *args, **kwargs):
  90. rv = list.append(self, *args, **kwargs)
  91. self.__reduce_list__()
  92. return rv