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 MadGraph5_aMC@NLO Development team and Contributors 
 4  # 
 5  # This file is a part of the MadGraph5_aMC@NLO 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 MadGraph5_aMC@NLO license which should accompany this  
10  # distribution. 
11  # 
12  # For more information, visit madgraph.phys.ucl.ac.be and amcatnlo.web.cern.ch 
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, log=True):
29 """Save any Python object to file filename""" 30 31 if not isinstance(filename, basestring): 32 raise SaveObjectError, "filename must be a string" 33 34 files.write_to_file(filename, pickle_object, object, log=log) 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 return files.read_from_file(filename, unpickle_object)
44
45 -def pickle_object(fsock, object):
46 """Helper routine to pickle an object to file socket fsock""" 47 48 cPickle.dump(object, fsock, protocol=2)
49
50 -class UnPickler(pickle.Unpickler):
51 """Treat problem of librarie""" 52
53 - def find_class(self, module, name):
54 """Find the correct path for the given function. 55 Due to ME call via MG some libraries might be messed up on the pickle 56 This routine helps to find back which one we need. 57 """ 58 59 # A bit of an ugly hack, but it works and has no side effect. 60 if module == 'loop_me_comparator': 61 module = 'tests.parallel_tests.loop_me_comparator' 62 63 try: 64 return pickle.Unpickler.find_class(self, module, name) 65 except ImportError: 66 pass 67 newmodule = 'internal.%s' % module.rsplit('.',1)[1] 68 try: 69 return pickle.Unpickler.find_class(self, newmodule , name) 70 except Exception: 71 pass 72 73 newmodule = 'madgraph.iolibs.%s' % module.rsplit('.',1)[1] 74 try: 75 return pickle.Unpickler.find_class(self, newmodule , name) 76 except Exception: 77 pass 78 79 newmodule = 'madgraph.madevent.%s' % module.rsplit('.',1)[1] 80 try: 81 return pickle.Unpickler.find_class(self, newmodule , name) 82 except Exception: 83 pass 84 85 newmodule = 'madgraph.various.%s' % module.rsplit('.',1)[1] 86 try: 87 return pickle.Unpickler.find_class(self, newmodule , name) 88 except Exception: 89 raise
90 91
92 -def unpickle_object(fsock):
93 """Helper routine to pickle an object to file socket fsock""" 94 95 p = UnPickler(fsock) 96 return p.load()
97 #return pickle.load(fsock) 98