From f00a530ec98cf56a2186d613d566872679bf402d Mon Sep 17 00:00:00 2001 From: Thomas Fillon Date: Fri, 4 Jul 2014 12:26:56 +0200 Subject: [PATCH] feature(tools): add buffering method based on PyTables --- .travis.yml | 2 +- README.rst | 2 +- setup.py | 1 + timeside/tools/buffering.py | 64 +++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 timeside/tools/buffering.py diff --git a/.travis.yml b/.travis.yml index 5334bcd..751a6f2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,7 @@ before_install: - sudo apt-get -qq update - sudo apt-get install python-setuptools - sudo apt-get install -qq python-numpy python-scipy python-matplotlib - - sudo apt-get install -qq libhdf5-serial-dev python-h5py + - sudo apt-get install -qq libhdf5-serial-dev python-h5py pytables - sudo apt-get install -qq python-gst0.10 gstreamer0.10-plugins-good gstreamer0.10-gnonlin gstreamer0.10-plugins-ugly gstreamer0.10-plugins-bad # command to install dependencies diff --git a/README.rst b/README.rst index 713385a..c7a79fb 100644 --- a/README.rst +++ b/README.rst @@ -292,7 +292,7 @@ Dependencies Needed:: python (>=2.7) python-setuptools python-numpy python-scipy python-h5py python-matplotlib python-imaging - python-simplejson python-yaml python-mutagen libhdf5-serial-dev python-gst0.10 + python-simplejson python-yaml python-mutagen libhdf5-serial-dev pytables python-gst0.10 gstreamer0.10-gnonlin gstreamer0.10-plugins-good gstreamer0.10-plugins-bad gstreamer0.10-plugins-ugly Optional:: diff --git a/setup.py b/setup.py index f6b5a58..96607b4 100755 --- a/setup.py +++ b/setup.py @@ -50,6 +50,7 @@ setup( 'mutagen', 'pillow', 'h5py', + 'tables', 'pyyaml', 'simplejson', 'scipy', diff --git a/timeside/tools/buffering.py b/timeside/tools/buffering.py new file mode 100644 index 0000000..f0e8320 --- /dev/null +++ b/timeside/tools/buffering.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- + +# Copyright (c) 2014 Parisson SARL +# Copyright (c) 2014 Thomas Fillon + +# This file is part of TimeSide. + +# TimeSide is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 2 of the License, or +# (at your option) any later version. + +# TimeSide is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with TimeSide. If not, see . +# +# Author : Thomas Fillon + +import tables +from tempfile import NamedTemporaryFile + + +class BufferTable(object): + def __init__(self, array_names=None): + self._tempfile = NamedTemporaryFile(mode='w', suffix='.h5', + prefix='ts_buf_', + delete=True) + self.fileh = tables.open_file(self._tempfile.name, mode='w') + + if not array_names: + array_names = [] + if isinstance(array_names, list): + self.array_names = array_names + else: + self.array_names = [array_names] + for name in array_names: + if not isinstance(name, basestring): + raise(TypeError, 'String argument require in array_names') + + def __getitem__(self, name): + return self.fileh.root.__getattr__(name) + + #def __set_item__(self, name, value): + # self.fileh.root.__setattr__(name, value) + + def append(self, name, new_array): + try: + self.fileh.root.__getattr__(name).append([new_array]) + except tables.exceptions.NoSuchNodeError: + if name not in self.array_names: + self.array_names.append(name) + self.fileh.create_earray(where=self.fileh.root, + name=name, + obj=[new_array]) + + def close(self): + for name in self.array_names: + self.fileh.remove_node(self.fileh.root, name) + self.fileh.close() + self._tempfile.close() -- 2.39.5