potc.translate.trans

Translator

class potc.translate.trans.Translator(extend_rules=None)[source]
Overview:

Common translator for the common translation cases.

This class is based on BlankTranslator, contains all the rules from the installed plugins. It is the best choice for being used directly.

__init__(extend_rules=None)[source]
Overview:

Constructor of Translator.

Arguments:
  • extend_rules: Rules for base translator.

transobj(obj)potc.translate.trans.ObjectTranslation
Overview:

Do translation to the given object obj.

Arguments:
  • obj: Object to be translated.

Returns:
transvars(vars_: Mapping[str, Any], reformat=None, isort: bool = True)potc.translate.trans.VarsTranslation
Overview:

Do translation to the given variables vars_, and create a runnable code.

Arguments:
  • vars_ (Mapping[str, Any]): Variables to be translated.

  • reformat: Reformatter for the final source code, default is None which means

    do not reformat the code.

  • isort (bool): Sort the import statements, default is True.

Returns:

TranslationFailed

class potc.translate.trans.TranslationFailed(obj)[source]
Overview:

Exception when translation is failed.

__init__(obj)[source]
Overview:

Constructor of TranslationFailed.

Arguments:
  • obj: Object which is failed to be translated.

property obj
Overview:

The object which is failed to be translated.

ObjectTranslation

class potc.translate.trans.ObjectTranslation(code: str, addon: potc.fixture.addons.Addons, trace: str)[source]
Overview:

Result of object translation.

__init__(code: str, addon: potc.fixture.addons.Addons, trace: str)[source]
Overview:

Constructor of ObjectTranslation.

Arguments:
  • code (str): String translated code.

  • addon (Addons): Addon object for this translation.

  • trace (str): Rule name for the translated object.

__iter__()[source]
Overview:

Iterate this object.

Examples::
>>> ot = ObjectTranslation(cc, aa, tt)
>>> c, a, t = ot
>>> assert c is cc
>>> assert a is aa
>>> assert t is tt
__str__()[source]
Overview:

Just get the string code, such as for the print() function.

property addon
Overview:

Addon object for this translation.

property code
Overview:

String translated code.

property imports
Overview:

Import statements of this translation.

property trace
Overview:

Rule name for the translated object.

VarsTranslation

class potc.translate.trans.VarsTranslation(code: str, addons: Mapping[str, potc.fixture.addons.Addons])[source]
Overview:

Result of vars translation.

__init__(code: str, addons: Mapping[str, potc.fixture.addons.Addons])[source]
Overview:

Constructor of VarsTranslation.

Arguments:
  • code (str): String translated code.

  • addons (Mapping[str, Addons]): Addon objects for the sub translations.

__iter__()[source]
Overview:

Iterate this object.

Examples::
>>> ot = VarsTranslation(cc, aa)
>>> c, a = ot
>>> assert c is cc
>>> assert a is aa
__str__() → str[source]
Overview:

Just get the string code, such as for the print() function.

property addons
Overview:

Addon objects for the sub translations.

property code
Overview:

String translated code.

BaseTranslator

class potc.translate.trans.BaseTranslator(extend_rules=None)[source]
Overview:

Base translator for the common translation cases.

This class is based on _AbstractTranslator, contains only system-leveled and default object-leveled translation rules.

__init__(extend_rules=None)[source]
Overview:

Constructor of BaseTranslator.

Arguments:
  • extend_rules: Rules for base translator.

transobj(obj)potc.translate.trans.ObjectTranslation
Overview:

Do translation to the given object obj.

Arguments:
  • obj: Object to be translated.

Returns:
transvars(vars_: Mapping[str, Any], reformat=None, isort: bool = True)potc.translate.trans.VarsTranslation
Overview:

Do translation to the given variables vars_, and create a runnable code.

Arguments:
  • vars_ (Mapping[str, Any]): Variables to be translated.

  • reformat: Reformatter for the final source code, default is None which means

    do not reformat the code.

  • isort (bool): Sort the import statements, default is True.

Returns:

BlankTranslator

class potc.translate.trans.BlankTranslator(extend_rules=None)[source]
Overview:

Blank translator for the common translation cases.

This class is based on BaseTranslator, contains all the native rules in this project. May be the best choice for DIY and testings.

Note

The rules from the plugins will be not be here, and will certainly not be applied when translation is processed.

__init__(extend_rules=None)[source]
Overview:

Constructor of BlankTranslator.

Arguments:
  • extend_rules: Rules for base translator.

transobj(obj)potc.translate.trans.ObjectTranslation
Overview:

Do translation to the given object obj.

Arguments:
  • obj: Object to be translated.

Returns:
transvars(vars_: Mapping[str, Any], reformat=None, isort: bool = True)potc.translate.trans.VarsTranslation
Overview:

Do translation to the given variables vars_, and create a runnable code.

Arguments:
  • vars_ (Mapping[str, Any]): Variables to be translated.

  • reformat: Reformatter for the final source code, default is None which means

    do not reformat the code.

  • isort (bool): Sort the import statements, default is True.

Returns:

autotrans

potc.translate.trans.autotrans(trans: Union[potc.translate.trans.BaseTranslator, Type[potc.translate.trans.BaseTranslator], list, tuple])potc.translate.trans.BaseTranslator[source]
Overview:

Turn the given object trans to a BaseTranslator instance.

Arguments:
  • trans (Union[BaseTranslator, Type[BaseTranslator], list, tuple]): Any object to be transformed to BaseTranslator instance.

Returns:
  • translator (BaseTranslator): Translator instance which can be directly used.