Skip to content

Commit 2ccff4f

Browse files
authored
Merge pull request #46067 from Dr15Jones/psetUpdate
Handle setting module parameters from a dict or PSet
2 parents 220b726 + 235fbea commit 2ccff4f

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

FWCore/ParameterSet/python/Mixins.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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):

FWCore/ParameterSet/python/ModulesProxy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ def __init__(self, package, name):
77
self._package = package
88
self._name = name
99
self._caller = None
10-
def __call__(self,**kwargs):
10+
def __call__(self,*arg, **kwargs):
1111
if not self._caller:
1212
self._caller = getattr(importlib.import_module(self._package+'.'+self._name),self._name)
13-
return self._caller(**kwargs)
13+
return self._caller(*arg, **kwargs)
1414

1515

1616
def _setupProxies(fullName:str):

FWCore/ParameterSet/src/ConfigurationDescriptions.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ namespace edm {
191191
}
192192
outFile << "import FWCore.ParameterSet.Config as cms\n\n";
193193
outFile << "def " << pluginName
194-
<< "(**kwargs):\n"
194+
<< "(*args, **kwargs):\n"
195195
" mod = cms."
196196
<< baseType_ << "('" << pluginName_ << "'";
197197

@@ -201,8 +201,9 @@ namespace edm {
201201
iDesc.writeCfi(outFile, startWithComma, indentation, ops);
202202

203203
outFile << ")\n"
204-
" for k,v in kwargs.items():\n"
205-
" setattr(mod, k, v)\n"
204+
" for a in args:\n"
205+
" mod.update_(a)\n"
206+
" mod.update_(kwargs)\n"
206207
" return mod\n";
207208

208209
outFile.close();

0 commit comments

Comments
 (0)