Example #1
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.archive.hdf_archive_schemes?
        """ %(val.__class__.__name__,ds)
      raise IOError,err
    g.write_attr("TRIQS_HDF5_data_scheme", ds)
Example #2
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 exist." % key

        if self.is_group(key):
            SUB = HDFArchiveGroup(self, key)  # View of the subgroup
            bare_return = lambda: SUB
        elif self.is_data(key):
            bare_return = lambda: self._read(key)
        else:
            raise KeyError, "Key %s is of unknown type !!" % Key

        if not reconstruct_python_object: return bare_return()
        # try to find the scheme
        try:
            hdf_data_scheme = self._group[key].attrs["TRIQS_HDF5_data_scheme"]
        except:
            return bare_return()
        try:
            sch = hdf_scheme_access(hdf_data_scheme)
        except:
            print "Warning : The TRIQS_HDF5_data_scheme %s is not recognized. Returning as a group. Hint : did you forgot to import this python class ?" % hdf_data_scheme
            return bare_return()
        r_class_name = sch.classname
        r_module_name = sch.modulename
        r_readfun = sch.read_fun
        if not (r_class_name and r_module_name): return bare_return()
        try:
            exec("from %s import %s as r_class" %
                 (r_module_name, r_class_name)) in globals(), locals()
        except KeyError:
            raise RuntimeError, "I cannot find the class %s to reconstruct the object !" % r_class_name
        if r_readfun:
            return r_readfun(self._group, str(
                key))  # str transforms unicode string to regular python string
        if hasattr(r_class, "__factory_from_dict__"):
            assert self.is_group(
                key), "__factory_from_dict__ requires a subgroup"
            f = lambda K: SUB.__getitem1__(K, reconstruct_python_object
                                           ) if SUB.is_group(K) else SUB._read(
                                               K)
            values = {
                self._key_decipher(str(K)): f(K)
                for K in SUB
            }  # str transforms unicode string to regular python string
            return r_class.__factory_from_dict__(key, values)
        raise ValueError, "Impossible to reread the class %s for group %s and key %s" % (
            r_class_name, self, key)
Example #3
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.archive.hdf_archive_schemes?
        """ % (val.__class__.__name__, ds)
         raise IOError, err
     g.write_attr("TRIQS_HDF5_data_scheme", ds)
Example #4
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 exist."%key

        if self.is_group(key) :
            SUB = HDFArchiveGroup(self,key) # View of the subgroup
            bare_return = lambda: SUB
        elif self.is_data(key) :
            bare_return = lambda: self._read(key)
        else :
            raise KeyError, "Key %s is of unknown type !!"%Key

        if not reconstruct_python_object : return bare_return()
        # try to find the scheme
        try :
           hdf_data_scheme = self._group[key].attrs["TRIQS_HDF5_data_scheme"]
        except:
            return bare_return()
        try :
            sch = hdf_scheme_access(hdf_data_scheme)
        except :
            print "Warning : The TRIQS_HDF5_data_scheme %s is not recognized. Returning as a group. Hint : did you forgot to import this python class ?"%hdf_data_scheme
            return bare_return()
        r_class_name  = sch.classname
        r_module_name = sch.modulename
        r_readfun = sch.read_fun
        if not (r_class_name and r_module_name) : return bare_return()
        try :
            exec("from %s import %s as r_class" %(r_module_name,r_class_name)) in globals(), locals()
        except KeyError :
            raise RuntimeError, "I cannot find the class %s to reconstruct the object !"%r_class_name
        if r_readfun :
            return r_readfun(self._group,str(key)) # str transforms unicode string to regular python string
        if hasattr(r_class,"__factory_from_dict__"):
            assert self.is_group(key), "__factory_from_dict__ requires a subgroup"
            f = lambda K : SUB.__getitem1__(K,reconstruct_python_object) if SUB.is_group(K) else SUB._read(K)
            values = {self._key_decipher(str(K)):f(K) for K in SUB }  # str transforms unicode string to regular python string
            return r_class.__factory_from_dict__(key,values)
        raise ValueError, "Impossible to reread the class %s for group %s and key %s"%(r_class_name,self, key)
Example #5
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 :
                  print "Warning : The TRIQS_HDF5_data_scheme %s is not recognized. Returning as a group. Hint : did you forgot to import this python class ?"%hdf_data_scheme
                  return SUB
              r_class_name  = sch.classname
              r_module_name = sch.modulename
              r_readfun = sch.read_fun
            if not (r_class_name and r_module_name) : return SUB
            try :
                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_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