berhoel.helper package

Misc helper

class berhoel.helper.SwirlSelect(value)[source]

Bases: enum.Enum

An enumeration.

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

Counting variable with start value and message.

Parameters
  • msg (str) – base message

  • start (int) – counter start_time

Returns

counter with message.

Return type

generator

>>> 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
>>>
berhoel.helper.process_msg_context(msg)[source]

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

Parameters

msg (str) – message for bracing process.

Returns

bracing message.

Return type

contextmanager

>>> with process_msg_context("do something"):
...     pass
do something...
do something...done
>>>
berhoel.helper.swirl(style=SwirlSelect.LINES)[source]

Generator to show a swirling life indicator.

Returns

printing running indicator.

Return type

generator

>>> sw = swirl()
>>> a = [_ for _ in zip(range(5), sw)]
\
|
/
-
\
>>> sw = swirl(SwirlSelect.DOTS)
>>> a = [_ for _ in zip(range(8), sw)]








>>>
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.

Parameters
  • msg (str) – message for bracing process.

  • time_form (func) – function formatting druntime.

Returns

bracing message.

Return type

contextmanager

>>> 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)
>>>

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

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. Returned values include:

ibeta – The radix in which numbers are represented, almost

always 2, but occasionally 16, or even 10

it – The number of base-ibeta digits in the floating point

mantissa

machep – Is the exponent of the smallest (most negative) power if

ibeta that, added to 1.0 gives something different from 1.0.

eps – Is the floating-point number ibeta ** machdep, loosly

referred to as the floating-point precision.

negep – Is the exponenet of the smalles power of ibeta that,

subtracted from 1.0, gives something different from 1.0.

epsneg – 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 – Is the number of bits in the exponent (including its

sign or bias)

minexp – Is the smallest (most negative) power if ibeta

consistent with there no leading zeros in the mantissa.

xmin – Is the floating-point number ibeta ** minexp, generally

the smallest (in magnitude) usable floating value

maxexp – Is the smallest (positive) power of ibeta that causes

overflow.

xmax – Is (1 - epsneg) x ibeta ** maxexp, generally the largest

(in magnitude) usable floating value.

irnd – 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 – Is the number of guard digits used when truncating

the ploduct of two mantissas to fit the representation

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

berhoel.helper.set_version module

Set lib version from pyproject.toml.

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()[source]

Create version files with version number from pyproject.toml.