ATLAS Offline Software
Loading...
Searching...
No Matches
python.PythonConfig.PythonConfig Class Reference
Inheritance diagram for python.PythonConfig.PythonConfig:
Collaboration diagram for python.PythonConfig.PythonConfig:

Public Member Functions

 __init__ (self, typeAndName, **kwargs)
 getName (self)
 getType (self)
 addSelfToJob (self, job)
 __getattr__ (self, name)
 __setattr__ (self, key, value)
 __eq__ (self, other)
 __ne__ (self, other)
 __str__ (self)
 addPrivateTool (self, name, type)
 addPrivateToolInArray (self, name, type)

Static Public Attributes

int printHeaderWidth = 80
int printHeaderPre = 3

Static Protected Member Functions

 _printHeader (title)
 _printFooter (title)

Protected Attributes

dict _props = {}

Detailed Description

Standalone Analysis Component Configuration

This class is used to describe the configuration of an analysis component
(a C++ class inheriting from asg::AsgComponentConfig) in Python. It behaves
similar to an Athena configurable, but is implemented in a much simpler
way.

An example of using it in configuring an EventLoop job could look like:

   job = ROOT.EL.Job()
   ...
   from AnaAlgorithm.PythonConfig import PythonConfig
   alg = PythonConfig( "EL::UnitTestAlg2/TestAlg",
                        property = 1.23 )
   alg.setComponentType( "AnaAlgorithm" )
   alg.string_property = "Foo"
   job.algsAdd( alg )

Note that the python code doesn't know what properties can actually be set
on any given C++ algorithm. Any mistake made in the Python configuration
(apart from syntax errors) is only discovered while initialising the
analysis job.

Definition at line 8 of file PythonConfig.py.

Constructor & Destructor Documentation

◆ __init__()

python.PythonConfig.PythonConfig.__init__ ( self,
typeAndName,
** kwargs )
Constructor for an algorithm configuration object

Keyword arguments:
  typeAndName -- The type/instance name of the algorithm

Note that you can pass (initial) properties to the constructor like:

   alg = PythonConfig( "EL::UnitTestAlg2/TestAlg",
                       property = 1.23 )

Definition at line 37 of file PythonConfig.py.

37 def __init__( self, typeAndName, **kwargs ):
38 """Constructor for an algorithm configuration object
39
40 Keyword arguments:
41 typeAndName -- The type/instance name of the algorithm
42
43 Note that you can pass (initial) properties to the constructor like:
44
45 alg = PythonConfig( "EL::UnitTestAlg2/TestAlg",
46 property = 1.23 )
47 """
48
49 # Call the base class's constructor. Use the default constructor instead
50 # of the one receiving the type and name, to avoid ROOT-10872.
51 super( PythonConfig, self ).__init__()
52 self.setTypeAndName( typeAndName )
53
54 # Initialise the properties of the algorihm:
55 self._props = {}
56
57 # Set the properties on the object:
58 for key, value in kwargs.items():
59 self.setPropertyFromString( key, stringPropValue( value ) )
60 self._props[ key ] = copy.deepcopy( value )
61 pass
62
63 pass
64

Member Function Documentation

◆ __eq__()

python.PythonConfig.PythonConfig.__eq__ ( self,
other )
Check for equality with another object

The implementation of this is very simple. We only check that the type
and the name of the algorithms would match.

Definition at line 137 of file PythonConfig.py.

137 def __eq__( self, other ):
138 """Check for equality with another object
139
140 The implementation of this is very simple. We only check that the type
141 and the name of the algorithms would match.
142 """
143
144 # First check that the other object is also an PythonConfig one:
145 if not isinstance( other, PythonConfig ):
146 return False
147
148 # Now check whether the type and the name of the algorithms agree:
149 return ( ( self.type() == other.type() ) and
150 ( self.name() == other.name() ) )
151

◆ __getattr__()

python.PythonConfig.PythonConfig.__getattr__ ( self,
name )
Get a previously set property value from the configuration

This function allows us to retrieve the value of a property that was
already set for the algorithm, to possibly use it in some configuration
decisions in the Python code itself.

Keyword arguments:
  name -- The name of the property

Definition at line 92 of file PythonConfig.py.

92 def __getattr__( self, name ):
93 """Get a previously set property value from the configuration
94
95 This function allows us to retrieve the value of a property that was
96 already set for the algorithm, to possibly use it in some configuration
97 decisions in the Python code itself.
98
99 Keyword arguments:
100 name -- The name of the property
101 """
102
103 # Fail if the property was not (yet) set:
104 if not name in self._props:
105 raise AttributeError( 'Property \'%s\' was not set on \'%s/%s\'' %
106 ( name, self.type(), self.name() ) )
107
108 # Return the property value:
109 return self._props[ name ]
110

◆ __ne__()

python.PythonConfig.PythonConfig.__ne__ ( self,
other )
Check for an inequality with another object

This is just defined to make the '!=' operator of Python behave
consistently with the '==' operator for such objects.

Definition at line 152 of file PythonConfig.py.

152 def __ne__( self, other ):
153 """Check for an inequality with another object
154
155 This is just defined to make the '!=' operator of Python behave
156 consistently with the '==' operator for such objects.
157 """
158 return not self.__eq__( other )
159

◆ __setattr__()

python.PythonConfig.PythonConfig.__setattr__ ( self,
key,
value )
Set an algorithm property on an existing configuration object

This function allows us to set/override properties on an algorithm
configuration object. Allowing for the following syntax:

   alg = ...
   alg.IntProperty = 66
   alg.FloatProperty = 3.141592
   alg.StringProperty = "Foo"

Keyword arguments:
  key   -- The key/name of the property
  value -- The value to set for the property

Definition at line 111 of file PythonConfig.py.

111 def __setattr__( self, key, value ):
112 """Set an algorithm property on an existing configuration object
113
114 This function allows us to set/override properties on an algorithm
115 configuration object. Allowing for the following syntax:
116
117 alg = ...
118 alg.IntProperty = 66
119 alg.FloatProperty = 3.141592
120 alg.StringProperty = "Foo"
121
122 Keyword arguments:
123 key -- The key/name of the property
124 value -- The value to set for the property
125 """
126
127 # Private variables should be set directly:
128 if key[ 0 ] == '_':
129 return super( PythonConfig, self ).__setattr__( key, value )
130
131 # Set the property, and remember its value:
132 super( PythonConfig,
133 self ).setPropertyFromString( key, stringPropValue( value ) )
134 self._props[ key ] = copy.deepcopy( value )
135 pass
136

◆ __str__()

python.PythonConfig.PythonConfig.__str__ ( self)
Print the algorithm configuration in a user friendly way

This is just to help with debugging configurations, allowing
the user to get a nice printout of their job configuration.

Definition at line 160 of file PythonConfig.py.

160 def __str__( self ):
161 """Print the algorithm configuration in a user friendly way
162
163 This is just to help with debugging configurations, allowing
164 the user to get a nice printout of their job configuration.
165 """
166
167 name = 'PythonConfig %s/%s/%s' % ( self.componentType(),self.type(), self.name() )
168 result = PythonConfig._printHeader( name )
169 result += '\n'
170 for key, value in sorted( self._props.items() ):
171 if isinstance( value, str ):
172 printedValue = "'%s'" % value
173 else:
174 printedValue = value
175 pass
176 result += "|- %s: %s\n" % ( key, indentBy( printedValue, "| " ) )
177 pass
178 result += PythonConfig._printFooter( name )
179 return result
180

◆ _printFooter()

python.PythonConfig.PythonConfig._printFooter ( title)
staticprotected
Produce a nice footer when printing the configuration

This function is used for printing the footer of both algorithms
and tools.

Keyword arguments:
  indentString -- String used as indentation
  title        -- The title of the algorithm/tool

Definition at line 283 of file PythonConfig.py.

283 def _printFooter( title ):
284 """Produce a nice footer when printing the configuration
285
286 This function is used for printing the footer of both algorithms
287 and tools.
288
289 Keyword arguments:
290 indentString -- String used as indentation
291 title -- The title of the algorithm/tool
292 """
293
294 preLength = PythonConfig.printHeaderPre
295 postLength = PythonConfig.printHeaderWidth - 12 - preLength - \
296 len( title )
297 return '\\%s (End of %s) %s' % ( preLength * '-', title,
298 postLength * '-' )
299

◆ _printHeader()

python.PythonConfig.PythonConfig._printHeader ( title)
staticprotected
Produce a nice header when printing the configuration

This function is used for printing the header of both algorithms
and tools.

Keyword arguments:
  indentString -- String used as indentation
  title        -- The title of the algorithm/tool

Definition at line 266 of file PythonConfig.py.

266 def _printHeader( title ):
267 """Produce a nice header when printing the configuration
268
269 This function is used for printing the header of both algorithms
270 and tools.
271
272 Keyword arguments:
273 indentString -- String used as indentation
274 title -- The title of the algorithm/tool
275 """
276
277 preLength = PythonConfig.printHeaderPre
278 postLength = PythonConfig.printHeaderWidth - 3 - preLength - \
279 len( title )
280 return '/%s %s %s' % ( preLength * '*', title, postLength * '*' )
281

◆ addPrivateTool()

python.PythonConfig.PythonConfig.addPrivateTool ( self,
name,
type )
Create a private tool for the algorithm

This function is used in 'standalone' mode to declare a private tool
for the algorithm, or a private tool for an already declared private
tool.

Can be used like:
  config.addPrivateTool( 'tool1', 'ToolType1' )
  config.addPrivateTool( 'tool1.tool2', 'ToolType2' )

Keyword arguments:
  name -- The full name of the private tool
  type -- The C++ type of the private tool

Definition at line 181 of file PythonConfig.py.

181 def addPrivateTool( self, name, type ):
182 """Create a private tool for the algorithm
183
184 This function is used in 'standalone' mode to declare a private tool
185 for the algorithm, or a private tool for an already declared private
186 tool.
187
188 Can be used like:
189 config.addPrivateTool( 'tool1', 'ToolType1' )
190 config.addPrivateTool( 'tool1.tool2', 'ToolType2' )
191
192 Keyword arguments:
193 name -- The full name of the private tool
194 type -- The C++ type of the private tool
195 """
196
197 # And now set up the Python object that will take care of setting
198 # properties on this tool.
199
200 # Tokenize the tool's name. In case it is a subtool of a tool, or
201 # something possibly even deeper.
202 toolNames = name.split( '.' )
203
204 # Look up the component that we need to set up the private tool on.
205 component = self
206 for tname in toolNames[ 0 : -1 ]:
207 component = getattr( component, tname )
208 pass
209
210 # Check that the component doesn't have such a (tool) property yet.
211 if hasattr( component, toolNames[ -1 ] ):
212 raise RuntimeError( "Tool with name '%s' already exists" % name )
213 pass
214
215 # Now set up a smart object as a property on that component.
216 component._props[ toolNames[ -1 ] ] = PrivateToolConfig( self, name,
217 type )
218
219 # Finally, tell the C++ code what to do.
220 self.createPrivateTool( name, type ).ignore()
221
222 pass
223

◆ addPrivateToolInArray()

python.PythonConfig.PythonConfig.addPrivateToolInArray ( self,
name,
type )
Create a private tool in an array for the algorithm

This function is used in 'standalone' mode to declare a
private tool in a tool array for the algorithm, or a private
tool in a tool array for an already declared private tool.

Can be used like:
  tool = config.addPrivateToolInArray( 'tool1', 'ToolType1' )
  tool = config.addPrivateToolInArray( 'tool1.tool2', 'ToolType2' )

Keyword arguments:
  name -- The full name of the private tool
  type -- The C++ type of the private tool

Definition at line 224 of file PythonConfig.py.

224 def addPrivateToolInArray( self, name, type ):
225 """Create a private tool in an array for the algorithm
226
227 This function is used in 'standalone' mode to declare a
228 private tool in a tool array for the algorithm, or a private
229 tool in a tool array for an already declared private tool.
230
231 Can be used like:
232 tool = config.addPrivateToolInArray( 'tool1', 'ToolType1' )
233 tool = config.addPrivateToolInArray( 'tool1.tool2', 'ToolType2' )
234
235 Keyword arguments:
236 name -- The full name of the private tool
237 type -- The C++ type of the private tool
238 """
239
240 # And now set up the Python object that will take care of setting
241 # properties on this tool.
242
243 # Tokenize the tool's name. In case it is a subtool of a tool, or
244 # something possibly even deeper.
245 toolNames = name.split( '.' )
246
247 # Look up the component that we need to set up the private tool on.
248 component = self
249 for tname in toolNames[ 0 : -1 ]:
250 component = getattr( component, tname )
251 pass
252
253 # Finally, tell the C++ code what to do.
254 actualName = self.createPrivateToolInArray( name, type )
255
256 # Tokenize the actual tool's name. In case it is a subtool of
257 # a tool, or something possibly even deeper.
258 actualToolNames = actualName.split( '.' )
259
260 # Now set up a smart object as a property on that component.
261 config = PrivateToolConfig( self, actualName, type )
262 component._props[ actualToolNames[ -1 ] ] = config
263 return config
264

◆ addSelfToJob()

python.PythonConfig.PythonConfig.addSelfToJob ( self,
job )
add a copy of this config to the EventLoop job object

Keyword arguments:
  job      -- The job object to add ourself to

Definition at line 83 of file PythonConfig.py.

83 def addSelfToJob( self, job ):
84 """add a copy of this config to the EventLoop job object
85
86 Keyword arguments:
87 job -- The job object to add ourself to
88 """
89 job.algsAdd( self )
90 pass
91

◆ getName()

python.PythonConfig.PythonConfig.getName ( self)
Get the instance name of the algorithm

This is for compatibility with the getName() function of Athena
configurables.

Definition at line 65 of file PythonConfig.py.

65 def getName( self ):
66 """Get the instance name of the algorithm
67
68 This is for compatibility with the getName() function of Athena
69 configurables.
70 """
71
72 return self.name()
73

◆ getType()

python.PythonConfig.PythonConfig.getType ( self)
Get the type name of the algorithm

This is for compatibility with the getType() function of Athena
configurables.

Definition at line 74 of file PythonConfig.py.

74 def getType( self ):
75 """Get the type name of the algorithm
76
77 This is for compatibility with the getType() function of Athena
78 configurables.
79 """
80
81 return self.type()
82

Member Data Documentation

◆ _props

dict python.PythonConfig.PythonConfig._props = {}
protected

Definition at line 55 of file PythonConfig.py.

◆ printHeaderPre

int python.PythonConfig.PythonConfig.printHeaderPre = 3
static

Definition at line 35 of file PythonConfig.py.

◆ printHeaderWidth

int python.PythonConfig.PythonConfig.printHeaderWidth = 80
static

Definition at line 34 of file PythonConfig.py.


The documentation for this class was generated from the following file: