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)
 __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().__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

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 136 of file PythonConfig.py.

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

◆ __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 88 of file PythonConfig.py.

88 def __getattr__( self, name ):
89 """Get a previously set property value from the configuration
90
91 This function allows us to retrieve the value of a property that was
92 already set for the algorithm, to possibly use it in some configuration
93 decisions in the Python code itself.
94
95 Keyword arguments:
96 name -- The name of the property
97 """
98
99 # Short-circuit internal/dunder attribute lookups, which are never
100 # properties (and would otherwise recurse if '_props' is missing):
101 if name.startswith( '_' ):
102 raise AttributeError( name )
103
104 # Fail if the property was not (yet) set:
105 if name not in self._props:
106 raise AttributeError( f"Property '{name}' was not set on "
107 f"'{self.type()}/{self.name()}'" )
108
109 # Return the property value:
110 return self._props[ name ]
111

◆ __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 112 of file PythonConfig.py.

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

◆ __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 151 of file PythonConfig.py.

151 def __str__( self ):
152 """Print the algorithm configuration in a user friendly way
153
154 This is just to help with debugging configurations, allowing
155 the user to get a nice printout of their job configuration.
156 """
157
158 name = f'PythonConfig {self.componentType()}/{self.type()}/{self.name()}'
159 result = PythonConfig._printHeader( name )
160 result += '\n'
161 for key, value in sorted( self._props.items() ):
162 if isinstance( value, str ):
163 printedValue = f"'{value}'"
164 else:
165 printedValue = value
166 result += f"|- {key}: {indentBy( printedValue, '| ' )}\n"
167 result += PythonConfig._printFooter( name )
168 return result
169

◆ _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 267 of file PythonConfig.py.

267 def _printFooter( title ):
268 """Produce a nice footer when printing the configuration
269
270 This function is used for printing the footer of both algorithms
271 and tools.
272
273 Keyword arguments:
274 indentString -- String used as indentation
275 title -- The title of the algorithm/tool
276 """
277
278 preLength = PythonConfig.printHeaderPre
279 postLength = PythonConfig.printHeaderWidth - 12 - preLength - \
280 len( title )
281 return f"\\{preLength * '-'} (End of {title}) {postLength * '-'}"
282
283

◆ _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 250 of file PythonConfig.py.

250 def _printHeader( title ):
251 """Produce a nice header when printing the configuration
252
253 This function is used for printing the header of both algorithms
254 and tools.
255
256 Keyword arguments:
257 indentString -- String used as indentation
258 title -- The title of the algorithm/tool
259 """
260
261 preLength = PythonConfig.printHeaderPre
262 postLength = PythonConfig.printHeaderWidth - 3 - preLength - \
263 len( title )
264 return f"/{preLength * '*'} {title} {postLength * '*'}"
265

◆ 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 170 of file PythonConfig.py.

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

◆ 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 209 of file PythonConfig.py.

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

◆ 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 80 of file PythonConfig.py.

80 def addSelfToJob( self, job ):
81 """add a copy of this config to the EventLoop job object
82
83 Keyword arguments:
84 job -- The job object to add ourself to
85 """
86 job.algsAdd( self )
87

◆ 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 62 of file PythonConfig.py.

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

◆ 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 71 of file PythonConfig.py.

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

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: