@@ -77,6 +77,13 @@ def __setattr__(self,name:str, value):
7777 raise AttributeError ("%r object has no attribute %r" % (self .__class__ .__name__ , name ))
7878 return object .__setattr__ (self , name , value )
7979 #support container like behavior
80+ def __len__ (self ):
81+ v = self .__dict__ .get ('_ProxyParameter__value' , None )
82+ if v is not None :
83+ return v .__len__ ()
84+ else :
85+ raise TypeError ("'_ProxyParameter' object has no len()" )
86+
8087 def __iter__ (self ):
8188 v = self .__dict__ .get ('_ProxyParameter__value' , None )
8289 if v is not None :
@@ -201,6 +208,37 @@ def _setValueWithType(self, valueWithType):
201208
202209PSetTemplate = _PSetTemplate
203210
211+ class _VPSetTemplate (object ):
212+ def __init__ (self , template :_PSetTemplate = None ):
213+ self ._template = template
214+ def __call__ (self , * value ):
215+ self .__dict__
216+ if self ._template :
217+ return VPSet (template = self ._template , * value )
218+ return VPSet (* value )
219+ def _isValid (self , value ) -> bool :
220+ if isinstance (value ,list ) or isinstance (value , VPSet ):
221+ return True
222+ try :
223+ iter (value )
224+ except TypeError :
225+ return False
226+ return True
227+ def dumpPython (self , options :PrintOptions = PrintOptions ()) -> str :
228+ if self ._template :
229+ options .indent ()
230+ ret = "VPSetTemplate(\n " + options .indentation ()+ self ._template .dumpPython (options )+ '\n '
231+ options .unindent ()
232+ ret += options .indentation ()+ ")"
233+ return ret
234+ return "VPSetTemplate()"
235+ def _setValueWithType (self , valueWithType ):
236+ if not isinstance (valueWithType , VPSet ):
237+ raise TypeError ("type {bad} is not a VPSet" .format (bas = str (type (valueWithType ))))
238+ return valueWithType
239+
240+ VPSetTemplate = _VPSetTemplate
241+
204242class _ProxyParameterFactory (object ):
205243 """Class type for ProxyParameter types to allow nice syntax"""
206244 def __init__ (self , type , isUntracked :bool = False ):
@@ -232,6 +270,16 @@ def __call__(self,*args,**kargs):
232270 return untracked (self .type (_PSetTemplate (* args ,** kargs )))
233271 return self .type (_PSetTemplate (* args ,** kargs ))
234272 return _PSetTemplateWrapper (self .__isUntracked , self .__type )
273+ if name == 'VPSetTemplate' :
274+ class _VPSetTemplateWrapper (object ):
275+ def __init__ (self , untracked , type ):
276+ self .untracked = untracked
277+ self .type = type
278+ def __call__ (self ,* args ,** kargs ):
279+ if self .untracked :
280+ return untracked (self .type (_VPSetTemplate (* args ,** kargs )))
281+ return self .type (_VPSetTemplate (* args ,** kargs ))
282+ return _VPSetTemplateWrapper (self .__isUntracked , self .__type )
235283
236284 type = globals ()[name ]
237285 if not issubclass (type , _ParameterTypeBase ):
@@ -2124,6 +2172,26 @@ def testRequired(self):
21242172 self .assertEqual (p1 .foo .a .value (), 5 )
21252173 p1 = PSet (anInt = required .int32 )
21262174 self .assertRaises (TypeError , setattr , p1 ,'anInt' , uint32 (2 ))
2175+ p1 = PSet (aVPSet = required .VPSetTemplate ())
2176+ self .assertEqual (p1 .dumpPython (),'cms.PSet(\n aVPSet = cms.required.VPSetTemplate()\n )' )
2177+ p1 .aVPSet = [PSet ()]
2178+ self .assertEqual (len (p1 .aVPSet ), 1 )
2179+ p1 = PSet (aVPSet = required .VPSetTemplate (PSetTemplate (a = required .int32 )))
2180+ self .assertEqual (p1 .dumpPython (),'cms.PSet(\n aVPSet = cms.required.VPSetTemplate(\n PSetTemplate(\n a = cms.required.int32\n )\n )\n )' )
2181+ p1 .aVPSet = [dict (a = 3 )]
2182+ self .assertEqual (len (p1 .aVPSet ), 1 )
2183+ self .assertEqual (p1 .aVPSet [0 ].a .value (),3 )
2184+ p1 = PSet (aVPSet = required .VPSetTemplate ())
2185+ p1 .aVPSet = VPSet ()
2186+ self .assertEqual (len (p1 .aVPSet ),0 )
2187+ p1 .aVPSet .append (PSet ())
2188+ self .assertEqual (len (p1 .aVPSet ),1 )
2189+ p1 = PSet (aVPSet = required .VPSetTemplate ())
2190+ p1 .aVPSet = (PSet (),)
2191+ self .assertEqual (len (p1 .aVPSet ), 1 )
2192+ p1 = PSet (aVPSet = required .VPSetTemplate (PSetTemplate (a = required .int32 )))
2193+ p1 .aVPSet = (dict (a = i ) for i in range (0 ,5 ))
2194+ self .assertEqual (len (p1 .aVPSet ), 5 )
21272195
21282196 def testOptional (self ):
21292197 p1 = PSet (anInt = optional .int32 )
@@ -2178,7 +2246,15 @@ def testOptional(self):
21782246 #check wrong type failure
21792247 p1 = PSet (anInt = optional .int32 )
21802248 self .assertRaises (TypeError , lambda : setattr (p1 ,'anInt' , uint32 (2 )))
2181-
2249+ p1 = PSet (aVPSet = optional .VPSetTemplate ())
2250+ self .assertEqual (p1 .dumpPython (),'cms.PSet(\n aVPSet = cms.optional.VPSetTemplate()\n )' )
2251+ p1 .aVPSet = [PSet ()]
2252+ self .assertEqual (len (p1 .aVPSet ), 1 )
2253+ p1 = PSet (aVPSet = optional .VPSetTemplate (PSetTemplate (a = required .int32 )))
2254+ self .assertEqual (p1 .dumpPython (),'cms.PSet(\n aVPSet = cms.optional.VPSetTemplate(\n PSetTemplate(\n a = cms.required.int32\n )\n )\n )' )
2255+ p1 .aVPSet = [dict (a = 3 )]
2256+ self .assertEqual (len (p1 .aVPSet ), 1 )
2257+ self .assertEqual (p1 .aVPSet [0 ].a .value (),3 )
21822258
21832259 def testAllowed (self ):
21842260 p1 = PSet (aValue = required .allowed (int32 , string ))
0 commit comments