Package madgraph :: Package iolibs :: Module save_load_object
[hide private]
[frames] | no frames]

Source Code for Module madgraph.iolibs.save_load_object

 1  ################################################################################ 
 2  # 
 3  # Copyright (c) 2009 The MadGraph Development team and Contributors 
 4  # 
 5  # This file is a part of the MadGraph 5 project, an application which  
 6  # automatically generates Feynman diagrams and matrix elements for arbitrary 
 7  # high-energy processes in the Standard Model and beyond. 
 8  # 
 9  # It is subject to the MadGraph license which should accompany this  
10  # distribution. 
11  # 
12  # For more information, please visit: http://madgraph.phys.ucl.ac.be 
13  # 
14  ################################################################################ 
15   
16  """Function to save any Python object to file.""" 
17   
18  import pickle 
19  import cPickle 
20   
21  from . import files as files 
22   
23 -class SaveObjectError(Exception):
24 """Exception raised if an error occurs in while trying to save an 25 object to file.""" 26 pass
27
28 -def save_to_file(filename, object):
29 """Save any Python object to file filename""" 30 31 if not isinstance(filename, str): 32 raise SaveObjectError, "filename must be a string" 33 34 files.write_to_file(filename, pickle_object, object) 35 36 return True
37
38 -def load_from_file(filename):
39 """Save any Python object to file filename""" 40 41 if not isinstance(filename, str): 42 raise SaveObjectError, "filename must be a string" 43 44 return files.read_from_file(filename, unpickle_object)
45
46 -def pickle_object(fsock, object):
47 """Helper routine to pickle an object to file socket fsock""" 48 49 cPickle.dump(object, fsock, protocol=2)
50
51 -class UnPickler(pickle.Unpickler):
52 """Treat problem of librarie""" 53
54 - def find_class(self, module, name):
55 """Find the correct path for the given function. 56 Due to ME call via MG some libraries might be messed up on the pickle 57 This routine helps to find back which one we need. 58 """ 59 60 try: 61 return pickle.Unpickler.find_class(self, module, name) 62 except ImportError: 63 pass 64 65 newmodule = 'internal.%s' % module.rsplit('.',1)[1] 66 try: 67 return pickle.Unpickler.find_class(self, newmodule , name) 68 except: 69 pass 70 71 newmodule = 'madgraph.iolibs.%s' % module.rsplit('.',1)[1] 72 try: 73 return pickle.Unpickler.find_class(self, newmodule , name) 74 except: 75 pass 76 77 newmodule = 'madgraph.various.%s' % module.rsplit('.',1)[1] 78 try: 79 return pickle.Unpickler.find_class(self, newmodule , name) 80 except: 81 raise
82 83
84 -def unpickle_object(fsock):
85 """Helper routine to pickle an object to file socket fsock""" 86 87 p = UnPickler(fsock) 88 return p.load()
89 #return pickle.load(fsock) 90