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.
 
 
 

109 lines
3.3 KiB

  1. """File-like objects that add timestamps to the input lines"""
  2. from __future__ import absolute_import
  3. from nilmdb.utils.printf import *
  4. import time
  5. import os
  6. import datetime_tz
  7. class Timestamper(object):
  8. """A file-like object that adds timestamps to lines of an input file."""
  9. def __init__(self, file, ts_iter):
  10. """file: filename, or another file-like object
  11. ts_iter: iterator that returns a timestamp string for
  12. each line of the file"""
  13. if isinstance(file, basestring):
  14. self.file = open(file, "r")
  15. else:
  16. self.file = file
  17. self.ts_iter = ts_iter
  18. def close(self):
  19. self.file.close()
  20. def readline(self, *args):
  21. while True:
  22. line = self.file.readline(*args)
  23. if not line:
  24. return ""
  25. if line[0] == '#':
  26. continue
  27. break
  28. try:
  29. return self.ts_iter.next() + line
  30. except StopIteration:
  31. return ""
  32. def readlines(self, size = None):
  33. out = ""
  34. while True:
  35. line = self.readline()
  36. out += line
  37. if not line or (size and len(out) >= size):
  38. break
  39. return out
  40. def __iter__(self):
  41. return self
  42. def next(self):
  43. result = self.readline()
  44. if not result:
  45. raise StopIteration
  46. return result
  47. class TimestamperRate(Timestamper):
  48. """Timestamper that uses a start time and a fixed rate"""
  49. def __init__(self, file, start, rate, end = None):
  50. """
  51. file: file name or object
  52. start: Unix timestamp for the first value
  53. rate: 1/rate is added to the timestamp for each line
  54. end: If specified, raise StopIteration before outputting a value
  55. greater than this."""
  56. def iterator(start, rate, end):
  57. n = 0
  58. rate = float(rate)
  59. while True:
  60. now = start + n / rate
  61. if end and now >= end:
  62. raise StopIteration
  63. yield sprintf("%.6f ", start + n / rate)
  64. n += 1
  65. # Handle case where we're passed a datetime or datetime_tz object
  66. if "totimestamp" in dir(start):
  67. start = start.totimestamp()
  68. Timestamper.__init__(self, file, iterator(start, rate, end))
  69. self.start = start
  70. self.rate = rate
  71. def __str__(self):
  72. start = datetime_tz.datetime_tz.fromtimestamp(self.start)
  73. start = start.strftime("%a, %d %b %Y %H:%M:%S %Z")
  74. return sprintf("TimestamperRate(..., start=\"%s\", rate=%g)",
  75. str(start), self.rate)
  76. class TimestamperNow(Timestamper):
  77. """Timestamper that uses current time"""
  78. def __init__(self, file):
  79. def iterator():
  80. while True:
  81. now = datetime_tz.datetime_tz.utcnow().totimestamp()
  82. yield sprintf("%.6f ", now)
  83. Timestamper.__init__(self, file, iterator())
  84. def __str__(self):
  85. return "TimestamperNow(...)"
  86. class TimestamperNull(Timestamper):
  87. """Timestamper that adds nothing to each line"""
  88. def __init__(self, file):
  89. def iterator():
  90. while True:
  91. yield ""
  92. Timestamper.__init__(self, file, iterator())
  93. def __str__(self):
  94. return "TimestamperNull(...)"