3.1.8.2. c3po.services.NameChanger module
Contain the class wrapper NameChanger.
- class c3po.services.NameChanger.NameChanger(physics, nameMappingValue={}, nameMappingField={}, wildcard=None, exclusive=False)
Bases:
PhysicsDriverWrapperNameChangerwraps aPhysicsDriverobject and changes the names of the values and fields quantities that can be get/set through ICoCo methods.This allows to improve the genericity of coupling scripts by using generic variable names without modifying the
PhysicsDriver“by hand”.- __init__(physics, nameMappingValue={}, nameMappingField={}, wildcard=None, exclusive=False)
Build a
NameChangerobject.Note
nameMappingValueandnameMappingFieldare copied.- Parameters:
physics (PhysicsDriver) – The
PhysicsDriverto wrap.nameMappingValue (dict) – A Python dictionary with the mapping from the new names (the generic ones) to the old ones (the names used by the code) for values. Names that are not in this mapping dictionary can still be used if
exclusive=False.nameMappingField (dict) – A Python dictionary with the mapping from the new names (the generic ones) to the old ones (the names used by the code) for fields. Names that are not in this mapping dictionary can still be used if
exclusive=False.wildcard (str) – A Python string. If both new and old names terminate by this wildcard (a wildcard in the middle of the names is not taken into account),
NameChangerwill substitute only the preceding part. For example :c3po.NameChanger(nameMappingValue={"newName*" : "oldName*"}, wildcard="*")will substitutenewNameA05inoldNameA05andnewNameB9inoldNameB9.exclusive (bool) – Set True to limit available names to the ones provided in the mapping.
- getFieldType(name)
- getFieldUnit(name)
- getInputFieldsNames()
- getInputMEDDoubleFieldTemplate(name)
See
c3po.DataAccessor.DataAccessor.getInputMEDDoubleFieldTemplate().
- getInputMEDIntFieldTemplate(name)
See
c3po.DataAccessor.DataAccessor.getInputMEDIntFieldTemplate().
- getInputValuesNames()
- getOutputDoubleValue(name)
- getOutputFieldsNames()
- getOutputIntValue(name)
- getOutputMEDDoubleField(name)
See
c3po.DataAccessor.DataAccessor.getOutputMEDDoubleField().
- getOutputMEDIntField(name)
- getOutputStringValue(name)
- getOutputValuesNames()
- getValueType(name)
- getValueUnit(name)
- setInputDoubleValue(name, value)
- setInputIntValue(name, value)
- setInputMEDDoubleField(name, field)
See
c3po.DataAccessor.DataAccessor.setInputMEDDoubleField().
- setInputMEDIntField(name, field)
- setInputStringValue(name, value)
- updateNameMappingField(nameMappingField, variableTypes=(0, 1))
Update (with the
update()method ofdict) the dictionarynameMappingFieldpreviously provided.
- updateNameMappingValue(nameMappingValue, variableTypes=(0, 1))
Update (with the
update()method ofdict) the dictionarynameMappingValuepreviously provided.
- updateOutputMEDDoubleField(name, field)
See
c3po.DataAccessor.DataAccessor.updateOutputMEDDoubleField().
- updateOutputMEDIntField(name, field)
See
c3po.DataAccessor.DataAccessor.updateOutputMEDIntField().
- class c3po.services.NameChanger.NameChangerMeta(clsname, superclasses, attributedict)
Bases:
typeMetaclass related to the use of
nameChanger().- __init__(clsname, superclasses, attributedict)
- static __new__(cls, clsname, superclasses, attributedict)
- c3po.services.NameChanger.nameChanger(nameMapping, wildcard=None)
nameChanger()is a class wrapper that allows to change the names of the variables used by the base class (usually aPhysicsDriver).This allows to improve the genericity of coupling scripts by using generic variable names without modifying the
PhysicsDriver“by hand”.When a method of the base class is called there is two possibilities :
The call uses a named argument “name” (for example
myObject.setInputDoubleValue(name="myName", value=0.)). In this case, the value passed to the argument “name” is modified (if this is a key ofnameMapping).There is no named argument “name” (for example
myObject.setInputDoubleValue("myName", 0.)). In this case, the value of all arguments of type “str” is modified (if the value used is a key ofnameMapping).
In both cases, nothing is done (no error) if the initial value is not in the keys of
nameMapping.The parameters of
nameChanger()are added to the class (“static” attributes) with the namesstatic_nameMappingandstatic_wildcard. A new method is also added for internal use:_getChangedName(self, name).nameChanger()can be used either as a python decorator (where the class is defined) in order to modify the class definition everywhere. For example:@c3po.nameChanger({"newName" : "oldName", "newName2*" : "oldName2*"}, "*") class MyClass(...): ...
or it can be used in order to redefined only locally the class like that:
MyNewClass = c3po.nameChanger({"newName" : "oldName", "newName2*" : "oldName2*"}, "*")(MyClass)
afterward “newName” can be used in place of “oldName” everywhere with
MyNewClass. “oldName” is still working.Note
nameMappingis copied.Warning
nameChanger()looks for ICoCo methods (the methods to implement in order to define aPhysicsDriver) in base classes and redefine them. Other inherited methods are invisible tonameChanger().A class that inherits from a class wrapped by nameChanger will be wrapped as well, with the same parameters. It may be a workaround for the previous warning. The definition of the daughter class (”
class Daughter(Mother): ...”) must be done after the application ofnameChanger()on Mother. Otherwise it will result inTypeErrorwhen the daughter class will try to call mother methods (since its mother class does not exist anymore!). As a consequence, ifnameChanger()is to be applied to C3PO classes, it is recommended to change their name “(MyNewClass = c3po.nameChanger(...)(MyClass)”).- Parameters:
nameMapping (dict) – A Python dictionary with the mapping from the new names (the generic ones) to the old ones (the names used by the code).
wildcard (str) – A Python string. If both new and old names terminate by this wildcard (a wildcard in the middle of the names is not taken into account),
nameChanger()will substitute only the preceding part. For example :c3po.nameChanger({"newName*" : "oldName*"}, "*")will substitutenewNameA05inoldNameA05andnewNameB9inoldNameB9.
- Raises:
Exception – If applied to a class already modified by
nameChanger(), because it could result in an unexpected behavior.