berhoel.helper package

Misc helper

class berhoel.helper.SwirlSelect(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

LINES = 1
DOTS = 2
berhoel.helper.swirl(style=SwirlSelect.LINES)[source]

Generator to show a swirling life indicator.

>>> sw = swirl()
>>> a = [_ for _ in zip(range(5), sw)]
\...|.../...-...\...
>>> sw = swirl(SwirlSelect.DOTS)
>>> a = [_ for _ in zip(range(8), sw)]
⠇...⡆...⣄...⣠...⢰...⠸...⠙...⠋...
Returns:

printing running indicator.

Return type:

generator

berhoel.helper.count_with_msg(msg='loop', start=0)[source]

Counting variable with start value and message.

>>> c = count_with_msg("msg", 5)
>>> print([i for _, i in zip(range(5),c)] == [5, 6, 7, 8, 9])
msg 1 ...msg 2 ...msg 3 ...msg 4 ...msg 5 ...True
>>>
Parameters:
  • msg (str) – base message

  • start (int) – counter start_time

Returns:

counter with message.

Return type:

generator

berhoel.helper.process_msg_context(msg)[source]

Provides a context for calling routines and reporting entering and exit.

>>> with process_msg_context("do something"):
...     pass
do something......do something...done
>>>
Parameters:

msg (str) – message for bracing process.

Returns:

bracing message.

Return type:

contextmanager

berhoel.helper.timed_process_msg_context(msg, time_form=None)[source]

Provides a context for calling routines and reporting entering and exit. Report spent time.

>>> with timed_process_msg_context("do something"):
...     time.sleep(1)
do something......do something...done (0:00:01)
>>> with timed_process_msg_context("do something", lambda t:"{:d}s".format(int(t))):
...     time.sleep(1)
do something......do something...done (1s)
>>>
Parameters:
  • msg (str) – message for bracing process.

  • time_form (func) – function formatting druntime.

Returns:

bracing message.

Return type:

contextmanager

Submodules

berhoel.helper.check_args module

Proccess arguments flexible.

This module provides a class named CheckArgs and a function named check_args.

The class CheckArgs is ment to be used as base class for own classes. After the __init__ method is called a dictionary args is added to the local namespace.

The init mathod has four arguments.

The first is, as usual, the self handler. The second is a list or tuples holding the allowed keyword arguments as first tuple element and their default values as remaining tuple elements.

The third and forth argument are the *data and **kw arguments from the implemented class.

Example:

>>> from .check_args import CheckArgs
>>> class samp(CheckArgs):
...     default = (('A', 'A'), ('B', 'B'), ('C', ('C', 1, 2)))
...     def __init__(self, *data, **kw):
...         CheckArgs.__init__(self, *data, **kw)
...         print(self.args['A'], self.args['B'], self.args['C'])
...
>>> A = samp(A = 34)
34 B ('C', 1, 2)
>>> A = samp(34, "HALLO")
34 HALLO ('C', 1, 2)
>>>

The function check_args provides similar funtionality for functions. It also takes four arguments.

The first to third argument are simlar to the second to forth element of the class. The forth element of the function is the functions name.

Example:

>>> from .check_args import check_args
>>> def tst(*data, **kw):
...     default = (('A', 'A'), ('B', 'B'), ('C', ('C', 1, 2)))
...     return check_args(default, data, kw, 'tst')
...
>>> tst(A = 34) == {'A': 34, 'B': 'B', 'C': ('C', 1, 2)}
True
>>> tst(34) == {'A': 34, 'B': 'B', 'C': ('C', 1, 2)}
True
>>>
exception berhoel.helper.check_args.ArgError[source]

Bases: TypeError

Error from the argument checking.

class berhoel.helper.check_args.CheckArgs(*data, **kw)[source]

Bases: object

A wrapper around the check_args function.

Parameters:
  • default (tuple[tuple[str, object]]) – argument names and default values for __init__ method.

  • data (tuple) – arguments to __init__ method.

  • kw (dict) – keyword arguments to __init__ method.

Returns:

argument name, value pairs.

Return type:

dict

__init__(*data, **kw)[source]
berhoel.helper.check_args.check_args(default, data, kw, name)[source]

Check arguments for function.

Parameters:
  • default (tuple[tuple[str, object]]) – argument names and default values for function call.

  • data (tuple) – arguments to function call.

  • kw (dict) – keyword arguments to function call.

  • name (str) – name for reporting.

Returns:

argument name, value pairs.

Return type:

dict

berhoel.helper.machar module

Determine machine-specific parameters affecting floating-point arithmetic.

Function to determine machine-specific parameters affecting floating-point arithmetic.

  • This is build after “NUMERICAL RECIPES in C”, second edition, Reprinted 1996, pp. 889.

berhoel.helper.machar.machar()[source]

Determines and returns machine-specific parameters affecting floating-point arithmetic.

Returns:

Values in result dictionary are:

ibeta (int)

The radix in which numbers are represented, almost always 2, but occasionally 16, or even 10

it (int)

The number of base-ibeta digits in the floating point mantissa

machep (int)

Is the exponent of the smallest (most negative) power if ibeta that, added to 1.0 gives something different from 1.0.

eps (float)

Is the floating-point number ibeta ** machdep, loosly referred to as the floating-point precision.

negep (int)

Is the exponenet of the smalles power of ibeta that, subtracted from 1.0, gives something different from 1.0.

epsneg (float)

Is ibeta ** negep, another way of defining floating-point precision. Not infrequently epsneg is 0.5 times eps; occasionally eps and epsneg are equal.

iexp (int)

Is the number of bits in the exponent (including its sign or bias)

minexp (int)

Is the smallest (most negative) power if ibeta consistent with there no leading zeros in the mantissa.

xmin (float)

Is the floating-point number ibeta ** minexp, generally the smallest (in magnitude) usable floating value

maxexp (int)

Is the smallest (positive) power of ibeta that causes overflow.

xmax (float)

Is (1 - epsneg) x ibeta ** maxexp, generally the largest (in magnitude) usable floating value.

irnd (int)

Returns a code in the range 0…5, giving information on what kind of rounding is done in addition, and on how underflow is handled.

If irnd returns 2 or 5, then your computer is compilant with the IEEE standard for rounding. If it returns 1 or 4, then it is doing some kind of rounding, but not the IEEE standard. If irnd returns 0 or 3, then it is truncating the result, not rounding it.

ngrd (int)

Is the number of guard digits used when truncating the product of two mantissas to fit the representation

Return type:

dict

This is taken from “NUMERICAL RECIPES in C”, second edition, Reprinted 1996.

berhoel.helper.machar.ibeta = 2

ibeta (int) The radix in which numbers are represented, almost always 2, but occasionally 16, or even 10

berhoel.helper.machar.it = 53

it (int) The number of base-ibeta digits in the floating point mantissa

berhoel.helper.machar.machdep = -52

machep (int) Is the exponent of the smallest (most negative) power if ibeta that, added to 1.0 gives something different from 1.0.

berhoel.helper.machar.eps = 2.220446049250313e-16

eps (float) Is the floating-point number ibeta ** machdep, loosly referred to as the floating-point precision.

berhoel.helper.machar.negep = -53

negep (int) Is the exponenet of the smalles power of ibeta that, subtracted from 1.0, gives something different from 1.0.

berhoel.helper.machar.epsneg = 1.1102230246251565e-16

epsneg (float) Is ibeta ** negep, another way of defining floating-point precision. Not infrequently epsneg is 0.5 times eps; occasionally eps and epsneg are equal.

berhoel.helper.machar.iexp = 11

iexp (int) Is the number of bits in the exponent (including its sign or bias)

berhoel.helper.machar.minexp = -1022

minexp (int) Is the smallest (most negative) power if ibeta consistent with there no leading zeros in the mantissa.

berhoel.helper.machar.xmin = 2.2250738585072014e-308

xmin (float) Is the floating-point number ibeta ** minexp, generally the smallest (in magnitude) usable floating value

berhoel.helper.machar.maxexp = 1024

maxexp (int) Is the smallest (positive) power of ibeta that causes overflow.

berhoel.helper.machar.xmax = 1.7976931348623157e+308

xmax (float) Is (1 - epsneg) x ibeta ** maxexp, generally the largest (in magnitude) usable floating value.

berhoel.helper.machar.irnd = 5

irnd (int) Returns a code in the range 0…5, giving information on what kind of rounding is done in addition, and on how underflow is handled.

If irnd returns 2 or 5, then your computer is compilant with the IEEE standard for rounding. If it returns 1 or 4, then it is doing some kind of rounding, but not the IEEE standard. If irnd returns 0 or 3, then it is truncating the result, not rounding it.

berhoel.helper.machar.ngrd = 0

ngrd (int) Is the number of guard digits used when truncating the product of two mantissas to fit the representation”

berhoel.helper.set_version module

Set lib version from pyproject.toml.

Note

Deprecated since version 1.3.6: Use:

__version__ = __import__("importlib.metadata", fromlist=["version"]).version(
    <package name>
)

instead

This library allows for setting the version number for a library from the pyproject.toml file.

Add to your pyproject.toml add a new section:

[tool.berhoel.helper.set_version]
version_files = ["berhoel/helper/_version.py"]

Generate the version file:

> poetry run set_lib_version
writing berhoel/helper/_version.py

In the library __init__.py just use:

try:
    from ._version import __version__
except ImportError:
    __version__ = "0.0.0.invalid0"
berhoel.helper.set_version.build_parser()[source]

Build cli parser.

berhoel.helper.set_version.build()[source]

Create version files with version number from pyproject.toml.