pyprec.utils package

Submodules

pyprec.utils.ask_question module

exception pyprec.utils.ask_question.TimeOutError[source]

Bases: Exception

Class for run-time error

pyprec.utils.ask_question.ask_question(logger: Logger, question: str, default: str, timeout: float = 10) str[source]

Asks question to user.

User has only timeout seconds to answer, then the default is returned.

Parameters
  • logger (Logger) – The logger instance.

  • question (str) – The question to ask.

  • default (str) – The default answer.

  • timeout (float) – Time limit to answer in seconds.

Returns

The user answer to the question.

Return type

str

pyprec.utils.ask_question.question_instance(logger: Logger, question: str, default: str) str[source]

Asks for user input.

Parameters
  • logger (Logger) – The logger instance.

  • question (str) – The question to ask.

  • default (str) – The default answer.

Returns

The user answer to the question.

Return type

str

pyprec.utils.ask_question.timed_input(logger: Logger, question: str, default: str, timeout: Optional[float] = None, noerror: bool = True, fct: Optional[Callable] = None) str[source]

Poses a question with a maximal time to answer.

Default answer is taken if maximal time is reached.

Parameters
  • logger (Logger) – The logger instance.

  • question (str) – The question to ask.

  • default (str) – The default answer.

  • timeout (float) – Time limit to answer in seconds.

  • noerror (bool) – Wether to raise error on TimeOutError exception.

  • fct (Callable) – The callable effectively asking the question.

Returns

The user answer to the question.

Return type

str

pyprec.utils.configlog module

pyprec.utils.utils module

This module implements general utility functions.

class pyprec.utils.utils.CaseSensitiveTemplate(*args, **kws)[source]

Bases: Template

Class re-defining Template for case sensitive string substitution.

The substitute and safe_substitute methods treat differently the placeholders with different capitalization in the template file.

Example

>>> template = '''
... ${prefix} is lower-case
... ${Prefix} is capitalized
... ${PREFIX} is uppercase
... '''
>>> t = CaseSensitiveTemplate(template)
>>> replace_dict = {"prefix": "foo"}
>>> subs = t.substitute(replace_dict)
>>> print(subs)
foo is lower-case
Foo is capitalized
FOO is uppercase
do_template_based_capitalization(mapping: dict)[source]

Adds capitalized or full uppercase keys to replace dictionary.

Parameters

mapping (dict) – The key-value pairs to be inserted in the template fields.

pattern = re.compile('\n            \\$(?:\n              (?P<escaped>\\$)  |   # Escape sequence of two delimiters\n              (?P<named>(?a:[_a-z][_a-z0-9]*))       |   # delimiter and a Python identifier\n          , re.IGNORECASE|re.VERBOSE)
safe_substitute(mapping={}, /, **kwds) str[source]

Safe substitution methods with case sensitive capitalization.

Refer to docs for function documentation.

substitute(mapping={}, /, **kwds) str[source]

Safe substitution methods with case sensitive capitalization.

Refer to docs for function documentation.

pyprec.utils.utils.boldface(text: str) str[source]

Returns input boldface version.

Parameters

text (str) – The test to be boldfaced.

Returns

The boldface version of the input.

Return type

str

pyprec.utils.utils.colorize(text: str, color: str) str[source]

Changes the printed color of the input string.

If the color name does not exists, returns default color.

Parameters
  • text (str) – The text to be colored.

  • color (str) – The color name.

pyprec.utils.utils.get_color_to_sgr_codes() dict[source]

Return Select Graphic Rendiction (SGR) sequence in dictionary form.

Note

Check out this link for more information about terminal colors and SGR codes.

Returns

codes – The terminal colorcodes

Return type

dict

pyprec.utils.utils.get_mapping_from_args(arg: dict = {}, /, **kwargs) dict[source]

Unifies the parameters in a unique dictionary map.

If there are duplicates between arg dictionary keys and any parameter in **kwargs, keyword arguments have always the returned dictionary.

Parameters
  • arg (dict) – The input dictionary.

  • **kwargs – Optional keyword arguments.

Returns

mapping – The unique output mapping.

Return type

dict

pyprec.utils.utils.get_template_path(template_name: str) Path[source]

Returns the query template as a Path.

This implementation retrieves the query path independently of the directory where the Python interpreter has been run from.

Parameters

template_name (str) – The name of the template to be found in the source file.

Returns

The path to the query template.

Return type

Path

pyprec.utils.utils.load_runcard(runcard_file: Path) dict[source]

Load runcard from yaml file.

Parameters

runcard_file (Path) – The yaml to load the dictionary from.

Returns

runcard – The loaded settings dictionary.

Return type

dict

Note

The pathlib.Path objects are automatically loaded if they are encoded with the following syntax:

path: !Path 'path/to/file'
pyprec.utils.utils.path_constructor(loader, node)[source]

PyYaml utility function.

pyprec.utils.utils.path_representer(dumper, data)[source]

PyYaml utility function.

pyprec.utils.utils.save_runcard(fname: Path, setup: dict)[source]

Save runcard to yaml file.

Parameters
  • fname (Path) – The yaml output file.

  • setup (Path) – The settings dictionary to be dumped.

Note

pathlib.PosixPath objects are automatically loaded.

Module contents