3434import sscha .Parallel as Parallel
3535from sscha .Parallel import pprint as print
3636
37+ import difflib
3738
3839import SCHAModules
3940#import sscha_HP_odd
9899class Ensemble :
99100 __debug_index__ = 0
100101
101- def __init__ (self , dyn0 , T0 , supercell = None ):
102+ def __init__ (self , dyn0 , T0 , supercell = None , ** kwargs ):
102103 """
103104 PREPARE THE ENSEMBLE
104105 ====================
@@ -115,6 +116,7 @@ def __init__(self, dyn0, T0, supercell = None):
115116 The temperature used to generate the ensemble.
116117 supercell: optional, list of int
117118 The supercell dimension. If not provided, it will be determined by dyn0
119+ **kwargs : any other attribute of the ensemble
118120 """
119121
120122 # N is the number of element in the ensemble
@@ -178,6 +180,39 @@ def __init__(self, dyn0, T0, supercell = None):
178180 # Get the extra quantities
179181 self .extra_quantities = {}
180182
183+
184+ # Setup the attribute control
185+ self .__total_attributes__ = [item for item in self .__dict__ .keys ()]
186+ self .fixed_attributes = True # This must be the last attribute to be setted
187+
188+
189+ # Setup any other keyword given in input (raising the error if not already defined)
190+ for key in kwargs :
191+ self .__setattr__ (key , kwargs [key ])
192+
193+
194+ def __setattr__ (self , name , value ):
195+ """
196+ This method is used to set an attribute.
197+ It will raise an exception if the attribute does not exists (with a suggestion of similar entries)
198+ """
199+
200+
201+ if "fixed_attributes" in self .__dict__ :
202+ if name in self .__total_attributes__ :
203+ super (Ensemble , self ).__setattr__ (name , value )
204+ elif self .fixed_attributes :
205+ similar_objects = str ( difflib .get_close_matches (name , self .__total_attributes__ ))
206+ ERROR_MSG = """
207+ Error, the attribute '{}' is not a member of '{}'.
208+ Suggested similar attributes: {} ?
209+ """ .format (name , type (self ).__name__ , similar_objects )
210+
211+ raise AttributeError (ERROR_MSG )
212+ else :
213+ super (Ensemble , self ).__setattr__ (name , value )
214+
215+
181216 def convert_units (self , new_units ):
182217 """
183218 CONVERT ALL THE VARIABLE IN A COHERENT UNIT OF MEASUREMENTS
0 commit comments