@@ -77,6 +77,13 @@ def __setattr__(self,name:str, value):
77
77
raise AttributeError ("%r object has no attribute %r" % (self .__class__ .__name__ , name ))
78
78
return object .__setattr__ (self , name , value )
79
79
#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
+
80
87
def __iter__ (self ):
81
88
v = self .__dict__ .get ('_ProxyParameter__value' , None )
82
89
if v is not None :
@@ -201,6 +208,37 @@ def _setValueWithType(self, valueWithType):
201
208
202
209
PSetTemplate = _PSetTemplate
203
210
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
+
204
242
class _ProxyParameterFactory (object ):
205
243
"""Class type for ProxyParameter types to allow nice syntax"""
206
244
def __init__ (self , type , isUntracked :bool = False ):
@@ -232,6 +270,16 @@ def __call__(self,*args,**kargs):
232
270
return untracked (self .type (_PSetTemplate (* args ,** kargs )))
233
271
return self .type (_PSetTemplate (* args ,** kargs ))
234
272
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 )
235
283
236
284
type = globals ()[name ]
237
285
if not issubclass (type , _ParameterTypeBase ):
@@ -2124,6 +2172,26 @@ def testRequired(self):
2124
2172
self .assertEqual (p1 .foo .a .value (), 5 )
2125
2173
p1 = PSet (anInt = required .int32 )
2126
2174
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 )
2127
2195
2128
2196
def testOptional (self ):
2129
2197
p1 = PSet (anInt = optional .int32 )
@@ -2178,7 +2246,15 @@ def testOptional(self):
2178
2246
#check wrong type failure
2179
2247
p1 = PSet (anInt = optional .int32 )
2180
2248
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 )
2182
2258
2183
2259
def testAllowed (self ):
2184
2260
p1 = PSet (aValue = required .allowed (int32 , string ))
0 commit comments