Skip to content

Commit be0c584

Browse files
committed
Added also the enforce on the attributes inside the Ensemble
1 parent 051ced1 commit be0c584

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

Modules/Ensemble.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import sscha.Parallel as Parallel
3535
from sscha.Parallel import pprint as print
3636

37+
import difflib
3738

3839
import SCHAModules
3940
#import sscha_HP_odd
@@ -98,7 +99,7 @@
9899
class 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

Modules/SchaMinimizer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class SSCHA_Minimizer(object):
104104

105105
def __init__(self, ensemble = None, root_representation = "normal",
106106
kong_liu_ratio = 0.5, meaningful_factor = 1,
107-
minimization_algorithm = "sdes", lambda_a = 1):
107+
minimization_algorithm = "sdes", lambda_a = 1, **kwargs):
108108
"""
109109
This class create a minimizer to perform the sscha minimization.
110110
It performs the sscha minimization.
@@ -134,6 +134,7 @@ def __init__(self, ensemble = None, root_representation = "normal",
134134
NOTE: Only sdes is currently implemented.
135135
lambda_a : float
136136
The force constant minimization step.
137+
**kwargs : any other attribute of this class
137138
"""
138139

139140
self.ensemble = ensemble
@@ -236,6 +237,10 @@ def __init__(self, ensemble = None, root_representation = "normal",
236237
self.__total_attributes__ = [item for item in self.__dict__.keys()]
237238
self.fixed_attributes = True # This must be the last attribute to be setted
238239

240+
# Setup any other keyword given in input (raising the error if not already defined)
241+
for key in kwargs:
242+
self.__setattr__(key, kwargs[key])
243+
239244

240245
def __setattr__(self, name, value):
241246
"""

0 commit comments

Comments
 (0)