formelsammlung package

Submodules

formelsammlung.env_exe_runner module

formelsammlung.env_exe_runner.env_exe_runner(venv_runner, envs, tool, tool_args=None)[source]

Call given tool from given tox or nox or virtual env considering OS.

Parameters
  • venv_runner (List[str]) – List containing: ‘nox’ and/or ‘tox’ and/or one or more ‘virtual environment’s

  • envs (List[str]) – List of environments to search the tool in when ‘tox’ or ‘nox’ is in venv_runner. If neither ‘tox’ nor ‘nox’ is in venv_runner you may pass an empty list.

  • tool (str) – Name of the executable to run.

  • tool_args (Optional[List[str]]) – Arguments to give to the tool.

Return type

int

Returns

Exit code 127 if no executable is found or the exit code of the called cmd.

formelsammlung.env_exe_runner.cli_caller()[source]

Warp env_exe_runner to be callable by cli.

Script to call executables in tox/nox/virtual envs considering OS.

The script takes three mandatory arguments:

  1. A string with comma separated runner (tox and/or nox) and/or virtual envs.

  2. A string with comma separated tox/nox envs to check for the executable. The envs are checked in given order. If tox/nox are not part of the first arg you may pass a ‘-‘ as second arg.

  3. The executable to call like e.g. pylint.

All other arguments after are passed to the tool on call.

Return type

int

Returns

Exit code from env_exe_runner

formelsammlung.envvar module

formelsammlung.envvar.TRUE_BOOL_VALUES = ('1', 'y', 'yes', 't', 'True')

Default values to convert to True for environment variables.

formelsammlung.envvar.FALSE_BOOL_VALUES = ('0', 'n', 'no', 'f', 'False')

Default values to convert to False for environment variables.

formelsammlung.envvar.INT_REGEX = '^[\\d]+(_\\d+)*$'

Default regex to check if a string could be an int.

formelsammlung.envvar.FLOAT_REGEX = '^[\\d]+(_\\d+)*\\.\\d+$'

Default regex to check if a string could be an float.

class formelsammlung.envvar.EnvVarGetter(*, raise_error_if_no_value=False, true_bool_values=None, false_bool_values=None, int_regex=None, float_regex=None)[source]

Bases: object

Class containing the config for EnvVarGetter.getenv_typed().

Initialize EnvVarGetter with config values.

Use the EnvVarGetter instance to call EnvVarGetter.getenv_typed() with the set config.

Note

Parameters below are all keyword only.

Parameters
  • raise_error_if_no_value (bool) –

    If True raises an KeyError when no value is found by EnvVarGetter.getenv_typed() for var_name and default is None.

    Default: False

  • true_bool_values (Optional[Iterable]) –

    Set of objects whose string representations are matched case-insensitive against the environment variable’s value to check if the value is a True bool.

    Default: TRUE_BOOL_VALUES

  • false_bool_values (Optional[Iterable]) –

    Set of objects whose string representations are matched case-insensitive against the environment variable’s value to check if the value is a False bool.

    Default: FALSE_BOOL_VALUES

  • int_regex (Optional[str]) –

    Regex string to identify integers.

    Default: INT_REGEX

  • float_regex (Optional[str]) –

    Regex string to identify floats.

    Default: FLOAT_REGEX

Return type

None

property true_bool_values

Set of objects to identify a True boolean.

See parameters of EnvVarGetter.

Return type

Set[str]

property false_bool_values

Set of objects to identify a False boolean.

See parameters of EnvVarGetter.

Return type

Set[str]

property int_regex

Regex string used for checking if a string is an int.

See parameters of EnvVarGetter.

Return type

str

property int_regex_pattern

Regex pattern of EnvVarGetter.int_regex().

Cannot be set. Set via EnvVarGetter.int_regex().

Return type

Pattern[str]

property float_regex

Regex string used for checking if a string is a float.

See parameters of EnvVarGetter.

Return type

str

property float_regex_pattern

Regex pattern of EnvVarGetter.float_regex().

Cannot be set. Set via EnvVarGetter.float_regex().

Return type

Pattern[str]

getenv_typed(var_name, default=None, rv_type=None)[source]

Wrap os.getenv() to adjust the type of the return values.

Instead of returning the environments variable’s value as str like os.getenv() you can set rv_type to a type to convert the value into. If rv_type is not set the type gets guessed and used for conversion.

Guessable types are (checked in this order):

For bool guessing the value returned by os.getenv() is compared against EnvVarGetter.true_bool_values() and EnvVarGetter.false_bool_values() and if a match is found returns the corresponding boolean.

For int guessing the value returned by os.getenv() is checked by the regex EnvVarGetter.int_regex_pattern() which can be changed by setting EnvVarGetter.int_regex().

For float guessing the value returned by os.getenv() is checked by the regex EnvVarGetter.float_regex_pattern() which can be changed by setting EnvVarGetter.float_regex().

Warning

Because bool is guessed before int 0 and 1 are converted into bool instead of int when rv_type is not set.

How to use:

>>> os.environ["TEST_ENV_VAR"] = "2"
>>> getter = EnvVarGetter()
>>> getter.getenv_typed("TEST_ENV_VAR", 1, int)
2
Parameters
  • var_name (str) – Name of the environment variable.

  • default (Optional[Any]) –

    Default value if no value is found for var_name.

    Default: None.

  • rv_type (Optional[type]) –

    Type the value of the environment variable should be changed into. If not set or set to None the type gets guessed.

    Default: None.

Raises
  • KeyError – If raise_error_if_no_value is True and no value is found for var_name and default is None.

  • KeyError – If rv_type is set to bool and value from var_name or default is not found in true_bool_values or false_bool_values.

Return type

Any

Returns

Value for var_name or default converted to rv_type or guessed type.

formelsammlung.envvar.getenv_typed(var_name, default=None, rv_type=None, **kwargs)[source]

Shortcut for EnvVarGetter(...).getenv_typed(...).

How to use:

>>> os.environ["TEST_ENV_VAR"] = "2"
>>> getenv_typed("TEST_ENV_VAR", 1, int)
2
Parameters
Return type

Any

Returns

Return value of EnvVarGetter.getenv_typed().

formelsammlung.flask_sphinx_docs module

class formelsammlung.flask_sphinx_docs.SphinxDocServer(app=None, *, doc_dir=None, index_file='index.html')[source]

Bases: object

Serve your sphinx docs under /docs/ on your own flask app.

Init SphinxDocServer class.

You can invoke it in your app factory:

sds = SphinxDocServer()

def create_app():
    app = Flask(__name__)
    sds.init_app(app)
    return app

or you can include the plugin directly without setting a doc_dir:

app = Flask(__name__)
SphinxDocServer(app)

or with setting a doc_dir:

app = Flask(__name__)
SphinxDocServer(app, doc_dir="../../docs/build/html")
Parameters
Return type

None

init_app(app, doc_dir=None, index_file='index.html')[source]

Add the /docs/ route to the app object.

Parameters
  • app (Flask) – Flask object to add the route to.

  • doc_dir (Optional[str]) – The base directory holding the sphinx docs to serve. If not set the doc_dir is guessed up to 3 directories above.

  • index_file (str) – The html file containing the base toctree. Default: “index.html”

Return type

None

formelsammlung.nox_session module

formelsammlung.nox_session.session_w_poetry(session_func)

Decorator function for nox session fcuntions.

The decorator replaces nox’s own Session class with formelsammlung’s Session class. The later introduces the Session.poetry_install() method for easier usage of nox with poetry managed projects.

The decorator must come after nox’s session decorator as it takes the Session object given by session as its argument to change it.

Example:

import nox
from formelsammlung.nox_session import session_w_poetry

@nox.session
@session_w_poetry
def some_nox_session(session):
    ...
Parameters

session_func (Callable) – decorated function with commands for nox session. This argument is automatically provided by python. You must not provide it yourself.

Return type

Callable

class formelsammlung.nox_session.Session(runner)[source]

Bases: Session

Subclass of nox’s Session class to add poetry_install method.

Parameters

runner (SessionRunner) –

Return type

None

poetry_install(extras=None, *, no_root=False, no_dev=False, pip_require_venv=False, **kwargs)[source]

Wrap poetry install command for usage in nox sessions.

Parameters
  • extras (Optional[str]) – string of space separated extras to install

  • no_dev (bool) –

    if --no-dev should be set

    Default: False

  • no_root (bool) –

    if --no-root should be set

    Default: False

  • pip_require_venv (bool) – if True sets environment variable PIP_REQUIRE_VIRTUALENV to true for this call. pip then requires to be run inside a venv. pip is used to install poetry inside the venv, if no poetry executable can be found.

  • kwargs (Any) – same kwargs as Session.install() (see nox docs)

Return type

None

formelsammlung.strcalc module

exception formelsammlung.strcalc.StringCalculatorError[source]

Bases: Exception

Exception for the StringCalculator.

formelsammlung.strcalc.calculate_string(expression)[source]

Calculate the given expression.

The given arithmetic expression string is parsed as an ast and then handled by the ast.NodeVisitor.

Python exceptions are risen like with normal arithmetic expression e.g. ZeroDivisionError.

Supported number types:

Warning

On PyPy3 only: When working with complex numbers containing or resulting with float numbers be aware that the result of calculate_string() and the equivalent arithmetic expression can divert in the decimals. The result from calculate_string() is then less precise.

Supported mathematical operators:

How to use:

>>> calculate_string("(1+2)/3")
1.0
Parameters

expression (str) – String with arithmetic expression.

Raises

StringCalculatorError – if given expression cannot be calculated.

Return type

Union[int, float, complex, None]

Returns

Result or None

formelsammlung.venv_utils module

formelsammlung.venv_utils.get_venv_path()[source]

Get path to the venv from where the python executable runs.

Raises

FileNotFoundError – when no calling venv can be detected.

Return type

Path

Returns

Return venv path

formelsammlung.venv_utils.get_venv_bin_dir(venv_path)[source]

Return path to bin/Scripts dir of given venv.

Parameters

venv_path (Union[str, Path]) – Path to venv

Raises

FileNotFoundError – when no bin/Scripts dir can be found for given venv.

Return type

Path

Returns

Path to bin/Scripts dir

formelsammlung.venv_utils.get_venv_tmp_dir(venv_path, search_tmp_dirs=None, create_if_missing=False, create_dir_name=None)[source]

Return path to tmp/temp dir of given venv.

Parameters
  • venv_path (Union[str, Path]) – Path to venv

  • search_tmp_dirs (Optional[Tuple[str]]) – List of temp dir names to look for; defaults to (“tmp”, “temp”, “.tmp”, “.temp”)

  • create_if_missing (bool) – Create a temp dir in the given venv if non exists; defaults to False

  • create_dir_name (Optional[str]) – Name of the venv to create; defaults to tmp

Raises

FileNotFoundError – when no tmp/temp dir can be found for given venv.

Return type

Path

Returns

Path to tmp/temp dir

formelsammlung.venv_utils.get_venv_site_packages_dir(venv_path)[source]

Return path to site-packages dir of given venv.

Parameters

venv_path (Union[str, Path]) – Path to venv

Raises

FileNotFoundError – when no site-packages dir can be found for given venv.

Return type

Path

Returns

Path to site-packages dir

formelsammlung.venv_utils.where_installed(program)[source]

Find installation locations for given program.

Return exit code and locations based on found installation places. Search in current venv and globally.

Exit codes:

  • 0 = nowhere

  • 1 = venv

  • 2 = global

  • 3 = both

Parameters

program (str) – Program to search

Return type

Tuple[int, Optional[str], Optional[str]]

Returns

Exit code, venv executable path, glob executable path