class NotRecognized (ValueError):
def __init__(self, curve):
- msg = 'Not a recognizable curve format: %s' % curve.path
- ValueError.__init__(self, msg)
- self.curve = curve
+ self.__setstate__(curve)
+
+ def __getstate__(self):
+ return self.curve
+
+ def __setstate__(self, data):
+ if isinstance(data, Curve):
+ msg = 'Not a recognizable curve format: %s' % data.path
+ super(NotRecognized, self).__init__(msg)
+ self.curve = data
class Data (numpy.ndarray):
"""Stores a single, continuous data set.
#the data dictionary contains: {name of data: list of data sets [{[x], [y]}]
self.path = path
self.driver = None
- self.data = []
+ self.data = None
if info == None:
info = {}
self.info = info
def load(self):
"""Use the driver to read the curve into memory.
"""
- pass
+ data,info = self.driver.read(self.path)
+ self.data = data
+ for key,value in info.items():
+ self.info[key] = value
+
+ def unload(self):
+ """Release memory intensive :attr:`.data`.
+ """
+ self.data = None
def __init__(self, drivers, name=None):
super(Playlist, self).__init__(name=name)
self.drivers = drivers
+ self._loaded = [] # List of loaded curves, see :meth:`._load`.
+ self._max_loaded = 100 # curves to hold in memory simultaneously.
def append_curve_by_path(self, path, info=None, identify=True):
if self.path != None:
- path = os.path.join(self.path, path)
+ path = os.path.join(os.path.dirname(self.path), path)
path = os.path.normpath(path)
c = curve.Curve(path, info=info)
if identify == True:
self.append(c)
return c
+ def current(self):
+ curve = super(Playlist, self).current()
+ self._load(curve)
+ return curve
+
+ def _load(self, curve):
+ if curve != None and curve not in self._loaded:
+ if curve not in self:
+ self.append(curve)
+ if curve.driver == None:
+ c.identify(self.drivers)
+ if curve.data == None:
+ curve.load()
+ self._loaded.append(curve)
+ if len(self._loaded) > self._max_loaded:
+ oldest = self._loaded.pop(0)
+ oldest.unload()
+
class FilePlaylist (Playlist):
version = '0.1'
root.unlink() # break circular references for garbage collection
return string
- def _from_xml_doc(self, doc):
+ def _from_xml_doc(self, doc, identify=True):
"""Load a playlist from an :class:`xml.dom.minidom.Document`
instance.
"""
path = curve_element.getAttribute('path')
info = dict(curve_element.attributes.items())
info.pop('path')
- self.append_curve_by_path(path, info, identify=False)
+ self.append_curve_by_path(path, info, identify=identify)
self.jump(self._index) # ensure valid index
- def from_string(self, string):
+ def from_string(self, string, identify=True):
"""Load a playlist from a string.
Examples
... </playlist>
... '''
>>> p = FilePlaylist(drivers=[],
- ... path=os.path.join('path', 'to','playlist'))
- >>> p.from_string(string)
+ ... path=os.path.join('path', 'to', 'my', 'playlist'))
+ >>> p.from_string(string, identify=False)
>>> p._index
1
>>> p.info
path/to/curve/two
"""
doc = xml.dom.minidom.parseString(string)
- self._from_xml_doc(doc)
+ self._from_xml_doc(doc, identify=identify)
- def load(self, path=None):
+ def load(self, path=None, identify=True):
"""Load a playlist from a file.
"""
self.set_path(path)
doc = xml.dom.minidom.parse(self.path)
- self._from_xml_doc(doc)
+ self._from_xml_doc(doc, identify=identify)
self._digest = self.digest()
def save(self, path=None):