@@ -174,15 +174,7 @@ def __init__(self,*arg,**kargs):
174174 self .__dict__ ['_Parameterizable__validator' ] = None
175175 """The named arguments are the 'parameters' which are added as 'python attributes' to the object"""
176176 if len (arg ) != 0 :
177- #raise ValueError("unnamed arguments are not allowed. Please use the syntax 'name = value' when assigning arguments.")
178- for block in arg :
179- # Allow __PSet for testing
180- if type (block ).__name__ not in ["PSet" , "__PSet" , "dict" ]:
181- raise ValueError ("Only PSets can be passed as unnamed argument blocks. This is a " + type (block ).__name__ )
182- if isinstance (block ,dict ):
183- kargs = block
184- else :
185- self .__setParameters (block .parameters_ ())
177+ self .__setParametersFromArg (* arg )
186178 self .__setParameters (kargs )
187179 self ._isModified = False
188180
@@ -258,6 +250,15 @@ def __addParameter(self, name:str, value):
258250 self .__dict__ [name ]= value
259251 self .__parameterNames .append (name )
260252 self ._isModified = True
253+ def __setParametersFromArg (self , * arg ):
254+ for block in arg :
255+ # Allow __PSet for testing
256+ if type (block ).__name__ not in ["PSet" , "__PSet" , "dict" ]:
257+ raise ValueError ("Only PSets can be passed as unnamed argument blocks. This is a " + type (block ).__name__ )
258+ if isinstance (block ,dict ):
259+ self .__setParameters (block )
260+ else :
261+ self .__setParameters (block .parameters_ ())
261262
262263 def __setParameters (self ,parameters ):
263264 v = None
@@ -290,6 +291,17 @@ def __setattr__(self,name:str,value):
290291 else :
291292 self .__dict__ [name ].setValue (value )
292293 self ._isModified = True
294+ def update_ (self , d ):
295+ """"Takes a PSet or dict and adds the entries as parameters. Already existing parameters will be overwritten.
296+ """
297+ if type (d ).__name__ not in ["PSet" , "__PSet" , "dict" ]:
298+ raise ValueError ("Only PSets or dicts can be passed to update_. This is a " + type (d ).__name__ )
299+
300+ items = d .items () if isinstance (d , dict ) else d .parameters_ ().items ()
301+ for k ,v in items :
302+ setattr (self , k , v )
303+
304+
293305
294306 def isFrozen (self ) -> bool :
295307 return self ._isFrozen
@@ -828,6 +840,30 @@ def __init__(self,*arg,**args):
828840 self .assertEqual (b .a .value (), 1 )
829841 self .assertEqual (b .b .value (), 2 )
830842 self .assertRaises (ValueError , lambda : __Test ("MyType" , __PSet (a = __TestType (1 )), __PSet (a = __TestType (2 ))))
843+ c = __Test ("MyType" , dict (a = __TestType (1 )), dict (b = __TestType (2 )))
844+ self .assertEqual (c .a .value (), 1 )
845+ self .assertEqual (c .b .value (), 2 )
846+ self .assertRaises (ValueError , lambda : __Test ("MyType" , dict (a = __TestType (1 )), dict (a = __TestType (2 ))))
847+ def testUpdate_ (self ):
848+ class __Test (_TypedParameterizable ):
849+ pass
850+ class __TestType (_SimpleParameterTypeBase ):
851+ def _isValid (self ,value ):
852+ return True
853+ class __PSet (_ParameterTypeBase ,_Parameterizable ):
854+ def __init__ (self ,* arg ,** args ):
855+ #need to call the inits separately
856+ _ParameterTypeBase .__init__ (self )
857+ _Parameterizable .__init__ (self ,* arg ,** args )
858+ a = __Test ("MyType" , a = __TestType (1 ))
859+ a .update_ (dict (b = __TestType (2 )))
860+ self .assertEqual (a .a .value (), 1 )
861+ self .assertEqual (a .b .value (), 2 )
862+ a .update_ (dict (a = 3 ))
863+ self .assertEqual (a .a .value (), 3 )
864+ a .update_ (__PSet (a = __TestType (5 )))
865+ self .assertEqual (a .a .value (), 5 )
866+ self .assertRaises (TypeError , lambda : a .update_ (dict (c = 6 )))
831867
832868 def testCopy (self ):
833869 class __Test (_TypedParameterizable ):
0 commit comments