示例#1
0
    def __getitem1__(self, key, reconstruct_python_object):

        if key not in self:
            key = self._key_cipher(key)
            if key not in self:
                raise KeyError, "Key %s does not exists" % key

        if self.is_group(key):
            SUB = HDFArchiveGroup(self, key)  # View of the subgroup
            if not reconstruct_python_object:
                return SUB
            try:
                hdf_data_scheme = SUB.read_attr("TRIQS_HDF5_data_scheme")
            except:
                return SUB
            if hdf_data_scheme:
                try:
                    sch = hdf_scheme_access(hdf_data_scheme)
                except:
                    # to decide : do we want an error or a warning ??
                    # raise IOError, "The TRIQS_HDF5_data_scheme %s is not recognized"%hdf_data_scheme
                    print "Warning : The TRIQS_HDF5_data_scheme %s is not recognized. Returning as a group" % hdf_data_scheme
                    return SUB
                r_class_name = sch.classname
                r_module_name = sch.modulename
                r_readfun = sch.read_fun
            else:  # for backward compatibility
                r_class_name = SUB.read_attr("PythonClass")
                r_module_name = SUB.read_attr("PythonModule")
            # print  "%s  : python object of type %s.%s "%(path2,r_module_name,r_class_name)
            if not (r_class_name and r_module_name):
                return SUB
            try:
                # r_class = getattr(my_import(r_module_name),r_class_name)
                exec ("from %s import %s as r_class" % (r_module_name, r_class_name)) in globals(), locals()
            except KeyError:
                raise RuntimeError, "I can not find the class %s to reconstruct the object !" % r_class_name
            if r_readfun:
                res = r_readfun(self._group, key)
            # elif "__factory_from_hdf5__" in dir(r_class) :
            #    res = r_class.__factory_from_hdf5__(self)
            elif "__factory_from_dict__" in dir(r_class):
                f = lambda K: SUB.__getitem1__(K, reconstruct_python_object) if SUB.is_group(K) else SUB._read(K)
                values = dict((self._key_decipher(K), f(K)) for K in SUB)
                res = r_class.__factory_from_dict__(values)
            else:
                raise ValueError, "Impossible to reread the class %s for group %s and key %s" % (
                    r_class_name,
                    self,
                    key,
                )
            return res
        elif self.is_data(key):
            return self._read(key)
        else:
            raise KeyError, "Key %s is of unknown type !!" % Key
示例#2
0
 def write_attributes(g):
     """Use the _hdf5_data_scheme_ if it exists otherwise the class name"""
     ds = val._hdf5_data_scheme_ if hasattr(val, "_hdf5_data_scheme_") else val.__class__.__name__
     try:
         sch = hdf_scheme_access(ds)
     except:
         err = """
        You are trying to store an object of type "%s", with the TRIQS_HDF5_data_scheme "%s". 
        But that data_scheme is not registered, so you will not be able to reread the class.
        Didn't you forget to register your class in pytriqs.base.archive.hdf_archive_schemes ?
        """ % (
             val.__class__.__name__,
             ds,
         )
         raise IOError, err
     g.write_attr("TRIQS_HDF5_data_scheme", ds)