Beispiel #1
0
class ResultsIterableReader(Sequence, _BaseResultsReader):
    
    def __init__(self, filepath):
        self._filepath = filepath
        self._lock = FileLock(filepath)
        self._group = None
    
    def __enter__(self):
        self._lock.acquire()
        
        self._group = h5py.File(self._filepath, 'r')
        self._check_version(self._group)
        self._identifiers = self._group.attrs['identifiers']
        
        return self
    
    def __exit__(self, exc_type, exc_val, exc_tb):
        self._group.close()
        self._lock.release()
    
    def __getitem__(self, index):
        if self._group is None:
            raise RuntimeError("Use reader in context manager (with statement)")
        
        identifier = self._identifiers[index]
        if isinstance(identifier, bytes):
            identifier = identifier.decode('ascii')
        subgroup = self._group['result-' + identifier]
        
        # Options
        options = self._read_options(subgroup)
        assert identifier == options.uuid

        # Load each result
        results = {}
        for key, subsubgroup in subgroup.items():
            logging.debug('Parsing %s (%s) result of %s',
                          key, subsubgroup.attrs['_class'], identifier)
            results[key] = self._read_result(subsubgroup)

        return ResultsContainer(options, results)
    
    def __len__(self):
        if self._group is None:
            raise RuntimeError("Use reader in context manager (with statement)")
        return len(self._group)
    
    @property
    def options(self):
        if self._group is None:
            raise RuntimeError("Use reader in context manager (with statement)")
        return self._read_options(self._group)
Beispiel #2
0
 def __init__(self, filepath):
     self._filepath = filepath
     self._lock = FileLock(filepath)
     self._group = None