Skip to content

Added VPSetTemplate to configuration #47354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 77 additions & 1 deletion FWCore/ParameterSet/python/Types.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ def __setattr__(self,name:str, value):
raise AttributeError("%r object has no attribute %r" % (self.__class__.__name__, name))
return object.__setattr__(self, name, value)
#support container like behavior
def __len__(self):
v =self.__dict__.get('_ProxyParameter__value', None)
if v is not None:
return v.__len__()
else:
raise TypeError("'_ProxyParameter' object has no len()")

def __iter__(self):
v =self.__dict__.get('_ProxyParameter__value', None)
if v is not None:
Expand Down Expand Up @@ -201,6 +208,37 @@ def _setValueWithType(self, valueWithType):

PSetTemplate = _PSetTemplate

class _VPSetTemplate(object):
def __init__(self, template:_PSetTemplate=None):
self._template = template
def __call__(self, *value):
self.__dict__
if self._template:
return VPSet(template = self._template, *value)
return VPSet(*value)
def _isValid(self, value) -> bool:
if isinstance(value,list) or isinstance(value, VPSet):
return True
try:
iter(value)
except TypeError:
return False
return True
def dumpPython(self, options:PrintOptions=PrintOptions()) -> str:
if self._template:
options.indent()
ret = "VPSetTemplate(\n"+options.indentation()+self._template.dumpPython(options)+'\n'
options.unindent()
ret += options.indentation()+")"
return ret
return "VPSetTemplate()"
def _setValueWithType(self, valueWithType):
if not isinstance(valueWithType, VPSet):
raise TypeError("type {bad} is not a VPSet".format(bas=str(type(valueWithType))))
return valueWithType

VPSetTemplate = _VPSetTemplate

class _ProxyParameterFactory(object):
"""Class type for ProxyParameter types to allow nice syntax"""
def __init__(self, type, isUntracked:bool = False):
Expand Down Expand Up @@ -232,6 +270,16 @@ def __call__(self,*args,**kargs):
return untracked(self.type(_PSetTemplate(*args,**kargs)))
return self.type(_PSetTemplate(*args,**kargs))
return _PSetTemplateWrapper(self.__isUntracked, self.__type)
if name == 'VPSetTemplate':
class _VPSetTemplateWrapper(object):
def __init__(self, untracked, type):
self.untracked = untracked
self.type = type
def __call__(self,*args,**kargs):
if self.untracked:
return untracked(self.type(_VPSetTemplate(*args,**kargs)))
return self.type(_VPSetTemplate(*args,**kargs))
return _VPSetTemplateWrapper(self.__isUntracked, self.__type)

type = globals()[name]
if not issubclass(type, _ParameterTypeBase):
Expand Down Expand Up @@ -2124,6 +2172,26 @@ def testRequired(self):
self.assertEqual(p1.foo.a.value(), 5)
p1 = PSet(anInt = required.int32)
self.assertRaises(TypeError, setattr, p1,'anInt', uint32(2))
p1 = PSet(aVPSet = required.VPSetTemplate())
self.assertEqual(p1.dumpPython(),'cms.PSet(\n aVPSet = cms.required.VPSetTemplate()\n)')
p1.aVPSet =[PSet()]
self.assertEqual(len(p1.aVPSet), 1)
p1 = PSet(aVPSet = required.VPSetTemplate(PSetTemplate(a=required.int32)))
self.assertEqual(p1.dumpPython(),'cms.PSet(\n aVPSet = cms.required.VPSetTemplate(\n PSetTemplate(\n a = cms.required.int32\n )\n )\n)')
p1.aVPSet = [dict(a=3)]
self.assertEqual(len(p1.aVPSet), 1)
self.assertEqual(p1.aVPSet[0].a.value(),3)
p1 = PSet(aVPSet = required.VPSetTemplate())
p1.aVPSet = VPSet()
self.assertEqual(len(p1.aVPSet),0)
p1.aVPSet.append(PSet())
self.assertEqual(len(p1.aVPSet),1)
p1 = PSet(aVPSet = required.VPSetTemplate())
p1.aVPSet = (PSet(),)
self.assertEqual(len(p1.aVPSet), 1)
p1 = PSet(aVPSet = required.VPSetTemplate(PSetTemplate(a=required.int32)))
p1.aVPSet = (dict(a=i) for i in range(0,5))
self.assertEqual(len(p1.aVPSet), 5)

def testOptional(self):
p1 = PSet(anInt = optional.int32)
Expand Down Expand Up @@ -2178,7 +2246,15 @@ def testOptional(self):
#check wrong type failure
p1 = PSet(anInt = optional.int32)
self.assertRaises(TypeError, lambda : setattr(p1,'anInt', uint32(2)))

p1 = PSet(aVPSet = optional.VPSetTemplate())
self.assertEqual(p1.dumpPython(),'cms.PSet(\n aVPSet = cms.optional.VPSetTemplate()\n)')
p1.aVPSet =[PSet()]
self.assertEqual(len(p1.aVPSet), 1)
p1 = PSet(aVPSet = optional.VPSetTemplate(PSetTemplate(a=required.int32)))
self.assertEqual(p1.dumpPython(),'cms.PSet(\n aVPSet = cms.optional.VPSetTemplate(\n PSetTemplate(\n a = cms.required.int32\n )\n )\n)')
p1.aVPSet = [dict(a=3)]
self.assertEqual(len(p1.aVPSet), 1)
self.assertEqual(p1.aVPSet[0].a.value(),3)

def testAllowed(self):
p1 = PSet(aValue = required.allowed(int32, string))
Expand Down