10.9.4. Tools

PyTan tools module.

pytan3.utils.tools.STORAGE_DIR = 'pytan3_local'

str – Default sub-directory to use for STORAGE_PATH.

pytan3.utils.tools.STORAGE_PATH = '~/pytan3_local'

str – Default path to use for get_storage_dir() if path is None.

Will use OS Environment variable “PYTAN_STORAGE_PATH” if set.

pytan3.utils.tools.get_env(key, default)[source]

Get an environment variables value.

Parameters:
  • key (str) – Name of OS environment variable value to get.
  • default (str) – Default value to return if key is not set or is empty.
Returns:

str

pytan3.utils.tools.get_storage_dir(path=None, path_sub='', mkdir=False)[source]

Get the directory for PyTan to use for storing local state.

Parameters:
  • path (str or pathlib.Path, optional) –

    Path to use as base storage directory.

    If None, will use OS environment variable “PYTAN_STORAGE_PATH” if set.

    If None and “PYTAN_STORAGE_PATH” is not set, will use STORAGE_PATH.

    Defaults to: None.

  • path_sub (str or pathlib.Path, optional) –

    Subdirectory to get under path.

    Defaults to: “”.

  • mkdir (bool, optional) –

    Create path and path / sub with perms 0700.

    Defaults to: False.

Returns:

pathlib.Path

pytan3.utils.tools.str_to_intfloat(s)[source]

Convert str to int or float.

Parameters:s (str) – String to convert to float if it has a period or int if not.
Returns:int or float
pytan3.utils.tools.secs_ago(secs)[source]

Get a datetime object from N seconds ago.

Parameters:secs (int or float) – Number of seconds ago to get datetime object of.
Returns:datetime.datetime
pytan3.utils.tools.secs_age(dt)[source]

Get the number of seconds ago of a datetime object.

Parameters:dt (datetime.datetime) – Datetime object to calculate age in seconds of.
Returns:int
pytan3.utils.tools.b64_encode(s)[source]

Encode s as base64 using base64.b64encode().

Parameters:s (str) – Text to base64 encode.
Returns:str
pytan3.utils.tools.b64_decode(s)[source]

Decode text as base64 using base64.b64decode().

Parameters:s (str) – Text to base64 decode.
Returns:str
pytan3.utils.tools.trim_txt(txt, limit=10000)[source]

Trim a str if it is over n characters.

Parameters:
  • txt (str) – String to trim.
  • limit (int, optional) –

    Number of characters to trim txt to.

    Defaults to: 10000.

Returns:

str

pytan3.utils.tools.get_dict_path(obj, path)[source]

Traverse a dict using a / seperated string.

Parameters:
  • obj (dict) – Dictionary to traverse using path.
  • path (str) – Nested dictionary keys seperated by / to traverse in obj.
Raises:

ValueError – If a part of a path can not be found.

Returns:

object

pytan3.utils.tools.human_size(size)[source]

Format a byte count as human readable using humanfriendly.format_size().

Parameters:size (int or str) – Size to format as an int or as a human readable str.

Notes

Given “13”, “13b”, “13 bytes”, or 13 will return: “13 bytes”.

Returns:str
pytan3.utils.tools.human_delta(delta, ms=False, short=True)[source]

Format a delta as human readable using humanfriendly.format_timespan().

Parameters:
  • delta (int or float or str or datetime.timedelta) – Number of seconds to format.
  • ms (bool, optional) –

    Represent milliseconds separately instead of as fractional seconds.

    Defaults to: False.

  • short (bool, optional) –

    Return shortened version of minutes, seconds, milliseconds.

    Defaults to: True.

Notes

Given 300.1 or “300.1” or datetime.timedelta(seconds=300, microseconds=100000), will return: “5 minutes and 100 milliseconds” or “5m100ms”.

Returns:str
class pytan3.utils.tools.Timer[source]

Bases: object

Context manager to get a human readable elapsed timespan.

__init__()[source]

Constructor.

start = None

datetime.datetime – Start of timespan.

end = None

datetime.datetime – End of timespan.

__call__()[source]

Return the current time.

Returns:datetime.datetime
__enter__()[source]

Set the start time.

Returns:datetime.datetime
__exit__(exc_type, exc_value, exc_traceback)[source]

Set end to now.

__str__()[source]

Get elapsed details.

Returns:str
__repr__()[source]

Get elapsed details.

Returns:str
elapsed_delta

Get elapsed time since start.

Returns:datetime.timedelta
elapsed_int

Get elapsed seconds since start.

Returns:int
elapsed

Get elapsed seconds since start in human readable format.

ms (bool, optional):

Represent milliseconds separately instead of as fractional seconds.

Defaults to: True.

short (bool, optional):

Return shortened version of minutes, seconds, millseconds.

Defaults to: True.

Returns:str