コード例 #1
0
ファイル: HDF_Archive.py プロジェクト: TRIQS/triqs_0.x
    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 = HDF_Archive_group(self, key)  # View of the subgroup
            if not reconstruct_python_object:
                return SUB
            try:
                hdf_data_scheme = SUB.read_attr("TRIQS_HDF5_data_scheme")
                try:
                    sch = hdf_scheme_access(hdf_data_scheme)
                except:
                    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
            except:  # for backward compatibility
                try:
                    r_class_name = SUB.read_attr("PythonClass")
                    r_module_name = SUB.read_attr("PythonModule")
                except:
                    return SUB
            # 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 "__read_hdf5__" in dir(r_class):
                res = r_class()
                res.__read_hdf5__(self)
            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
ファイル: HDF_Archive.py プロジェクト: xydeng/TRIQS
 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)
コード例 #3
0
ファイル: HDF_Archive.py プロジェクト: xydeng/TRIQS
    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 = HDF_Archive_group(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
            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 "__read_hdf5__" in dir(r_class) : 
                res = r_class()
                res.__read_hdf5__(self)
            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