Beispiel #1
0
 def read_samples(self, samples_path=None):
     """Read and unpack all the samples in to a list of tuples (timestamp, value)"""
     # N.B. Doesn't lock
     if samples_path is None:
         samples_path = self.samples_path
     with open(samples_path, 'rb') as f:
         sample_format = self._read_header(f).decode('utf-8')
         sample_struct = struct.Struct(py2bytes(sample_format))
         sample_size = sample_struct.size
         read_sample = partial(f.read, sample_size)
         unpack = sample_struct.unpack
         samples = [unpack(sample) for sample in iter(read_sample, b'')]
     return samples
Beispiel #2
0
    def __init__(self, path, name, time_format='d', value_format='d', max_samples=1000):
        self.path = abspath(path)
        self.name = name
        self.time_format = time_format
        self.value_format = value_format
        self.max_samples = max_samples

        self.samples_path = join(path, 'samples.smp')
        self.samples_snapshot_path = join(path, 'samples.smp.snapshot')

        sample_format = self.sample_format = '<' + time_format + value_format
        sample_struct = self.sample_struct = struct.Struct(py2bytes(sample_format))
        self.sample_pack = sample_struct.pack
        self.sample_unpack = sample_struct.unpack
        self.sample_size = sample_struct.size

        self.max_file_size = len(self.header) + self.sample_size * self.max_samples

        self.lock = RLock()

        self.check_create()
        super(Sampler, self).__init__()