Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright (c) 2012 Giorgos Verigakis <verigak@gmail.com>
  2. #
  3. # Permission to use, copy, modify, and distribute this software for any
  4. # purpose with or without fee is hereby granted, provided that the above
  5. # copyright notice and this permission notice appear in all copies.
  6. #
  7. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. from __future__ import division
  15. from collections import deque
  16. from datetime import timedelta
  17. from math import ceil
  18. from sys import stderr
  19. from time import time
  20. __version__ = '1.4'
  21. class Infinite(object):
  22. file = stderr
  23. sma_window = 10 # Simple Moving Average window
  24. def __init__(self, *args, **kwargs):
  25. self.index = 0
  26. self.start_ts = time()
  27. self.avg = 0
  28. self._ts = self.start_ts
  29. self._xput = deque(maxlen=self.sma_window)
  30. for key, val in kwargs.items():
  31. setattr(self, key, val)
  32. def __getitem__(self, key):
  33. if key.startswith('_'):
  34. return None
  35. return getattr(self, key, None)
  36. @property
  37. def elapsed(self):
  38. return int(time() - self.start_ts)
  39. @property
  40. def elapsed_td(self):
  41. return timedelta(seconds=self.elapsed)
  42. def update_avg(self, n, dt):
  43. if n > 0:
  44. self._xput.append(dt / n)
  45. self.avg = sum(self._xput) / len(self._xput)
  46. def update(self):
  47. pass
  48. def start(self):
  49. pass
  50. def finish(self):
  51. pass
  52. def next(self, n=1):
  53. now = time()
  54. dt = now - self._ts
  55. self.update_avg(n, dt)
  56. self._ts = now
  57. self.index = self.index + n
  58. self.update()
  59. def iter(self, it):
  60. try:
  61. for x in it:
  62. yield x
  63. self.next()
  64. finally:
  65. self.finish()
  66. class Progress(Infinite):
  67. def __init__(self, *args, **kwargs):
  68. super(Progress, self).__init__(*args, **kwargs)
  69. self.max = kwargs.get('max', 100)
  70. @property
  71. def eta(self):
  72. return int(ceil(self.avg * self.remaining))
  73. @property
  74. def eta_td(self):
  75. return timedelta(seconds=self.eta)
  76. @property
  77. def percent(self):
  78. return self.progress * 100
  79. @property
  80. def progress(self):
  81. return min(1, self.index / self.max)
  82. @property
  83. def remaining(self):
  84. return max(self.max - self.index, 0)
  85. def start(self):
  86. self.update()
  87. def goto(self, index):
  88. incr = index - self.index
  89. self.next(incr)
  90. def iter(self, it):
  91. try:
  92. self.max = len(it)
  93. except TypeError:
  94. pass
  95. try:
  96. for x in it:
  97. yield x
  98. self.next()
  99. finally:
  100. self.finish()