zensols.util package¶
Submodules¶
zensols.util.clip module¶
Provides access to the system clipboard. Currently only macOS is supported.
zensols.util.executor module¶
Simplifies external process handling.
- class zensols.util.executor.ExecutableFinder(path_var=None, raise_on_missing=True)[source]¶
Bases:
object
Searches for an executable binary in the search path. The default search path (
path_var
) is set to the operating system’sPATH
environment variable.- __init__(path_var=None, raise_on_missing=True)¶
- find(name)[source]¶
Like
find_all()
, but returns only the first found executable.
- class zensols.util.executor.Executor(logger, dry_run=False, check_exit_value=0, timeout=None, async_proc=False, working_dir=None)[source]¶
Bases:
object
Run a process and log output. The process is run in the foreground by default, or background. If the later, a process object is returned from
run()
.- __init__(logger, dry_run=False, check_exit_value=0, timeout=None, async_proc=False, working_dir=None)¶
-
check_exit_value:
int
= 0¶ Compare and raise an exception if the exit value of the process is not this number, or
None
to not check.
zensols.util.fail module¶
General utility classes to measuring time it takes to do work, to logging and fork/exec operations.
- exception zensols.util.fail.APIError[source]¶
Bases:
Exception
Base exception from which almost all library raised errors extend.
- __annotations__ = {}¶
- __module__ = 'zensols.util.fail'¶
- __weakref__¶
list of weak references to the object
- class zensols.util.fail.Failure(exception=None, thrower=None, traceback=None, message=None)[source]¶
Bases:
object
Contains information for failures as caught exceptions used by programatic methods.
- __init__(exception=None, thrower=None, traceback=None, message=None)¶
- print_stack(writer=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]¶
Print the stack trace of the exception that caused the failure.
- property traceback_str: str¶
The stack trace as a string.
:see
print_stack()
- write(depth=0, writer=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>)[source]¶
Write the contents of this instance to
writer
using indentiondepth
.- Parameters:
depth (
int
) – the starting indentation depthwriter (
TextIOBase
) – the writer to dump the content of this writable
zensols.util.hasher module¶
Utilities for hashing text.
zensols.util.log module¶
Utility classes and context managers around logging.
- class zensols.util.log.LogConfigurer(logger=<RootLogger root (WARNING)>, log_format='%(asctime)s %(levelname)s %(message)s', level=None)[source]¶
Bases:
object
Configure logging to go to a file or Graylog.
- class zensols.util.log.LoggerStream(logger, log_level=20)[source]¶
Bases:
object
Each line of standard out/error becomes a logged line
- class zensols.util.log.StreamLogDumper(stream, logger, level)[source]¶
Bases:
Thread
Redirect stream output to a logger in a running thread.
- __init__(stream, logger, level)[source]¶
This constructor should always be called with keyword arguments. Arguments are:
group should be None; reserved for future extension when a ThreadGroup class is implemented.
target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.
name is the thread name. By default, a unique name is constructed of the form “Thread-N” where N is a small decimal number.
args is a list or tuple of arguments for the target invocation. Defaults to ().
kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.__init__()) before doing anything else to the thread.
- run()[source]¶
Method representing the thread’s activity.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.
- zensols.util.log.add_logging_level(level_name, level_num, method_name=None)[source]¶
Comprehensively adds a new logging level to the logging module and the currently configured logging class.
level_name becomes an attribute of the logging module with the value level_num. method_name becomes a convenience method for both logging itself and the class returned by logging.getLoggerClass() (usually just logging.Logger). If method_name is not specified, level_name.lower() is used.
To avoid accidental clobberings of existing attributes, this method will raise an AttributeError if the level name is already an attribute of the logging module or if the method name is already present.
See `Stack Overflow`_ for attribution.
- class zensols.util.log.loglevel(name='', level=10, init=None, enable=True)[source]¶
Bases:
object
Object used with a
with
scope that sets the logging level temporarily and sets it back.Example:
with loglevel(__name__): logger.debug('test') with loglevel(['zensols.persist', 'zensols.config'], init=True): logger.debug('test')
- __init__(name='', level=10, init=None, enable=True)[source]¶
Configure the temporary logging setup.
- Parameters:
name (
Union
[List
[str
],str
,None
]) – the name of the logger to set, or if a list is passed, configure all loggers in the list; if a string, configure all logger names split on spaces; ifNone
orFalse
, do not configure anything (handy for REPL prototyping); default to the root logger to log everythinglevel (
int
) – the logging level, which defaults tologging.DEBUG
init (
Union
[bool
,int
]) – if notNone
, initialize logging withlogging.basicConfig()
using the given level orTrue
to uselogging.WARNING
enable (
bool
) – ifFalse
, disable any logging configuration changes for the block
zensols.util.pkgres module¶
A convenience class around the pkg_resources
module.
- class zensols.util.pkgres.PackageResource(name, file_system_defer=True)[source]¶
Bases:
object
Contains resources of installed Python packages. It makes the
distribution
available and provides access to to resource files withget_path()
and as an index.- __init__(name, file_system_defer=True)¶
- property distribution: DistInfoDistribution | None¶
The package distribution.
- Returns:
the distribution or
None
if it is not installed
-
file_system_defer:
bool
= True¶ Whether or not to return resource paths that point to the file system when this package distribution does not exist.
- See:
- get_path(resource)[source]¶
Return a resource file name by name. Optionally return resource as a relative path if the package does not exist.
- Parameters:
resource (
str
) – a forward slash (/
) delimited path (i.e.resources/app.conf
) of the resource name- Return type:
- Returns:
a path to that resource on the file system or
None
if the package doesn’t exist, the resource doesn’t exist andfile_system_defer
isFalse
zensols.util.sci module¶
Scientific utilities.
- class zensols.util.sci.ScientificUtil[source]¶
Bases:
object
A class containing utility methods.
- static fixed_format(v, length=1, add_pad=False)[source]¶
Format a number to a width resorting to scientific notation where necessary. The returned string is left padded with space in cases where scientific notation is too wide for
v > 0
. The mantissa is cut off also forv > 0
when the string version of the number is too wide.
zensols.util.std module¶
Utility classes for system based functionality.
- class zensols.util.std.stdout(path=None, extension=None, recommend_name='unnamed', capture=False, logger=<Logger zensols.util.std (WARNING)>, open_args='w')[source]¶
Bases:
object
Write to a file or standard out. This is desigend to be used in command line application (CLI) applications with the
zensols.cli
module. Application class can pass apathlib.Path
to a method with this class.Example:
def write(self, output_file: Path = Path('-')): """Write data. :param output_file: the output file name, ``-`` for standard out, or ``+`` for a default """ with stdout(output_file, recommend_name='unknown-file-name', extension='txt', capture=True, logger=logger): print('write data')
- __init__(path=None, extension=None, recommend_name='unnamed', capture=False, logger=<Logger zensols.util.std (WARNING)>, open_args='w')[source]¶
Initailize where to write. If the path is
None
or its name isSTANDARD_OUT_PATH
, then standard out is used instead of opening a file. Ifpath
is set toFILE_RECOMMEND_NAME
, it is constructed fromrecommend_name
. If no suffix (file extension) is provided forpath
thenextesion
is used if given.- Parameters:
extension (
str
) – the extension (sans leading dot.
) to postpend to the path if one is not provied in the file namerecommend_name (
str
) – the name to use as the prefix ifpath
is not providedcapture (
bool
) – whether to redirect standard out (sys.stdout
) to the file provided bypath
if not already indicated to be standard outlogger (
Logger
) – used to log the successful output of the file, which defaults to this module’s loggeropen_args (
str
) – the arguments given toopen()
, which defaults tow
if none are given
- class zensols.util.std.stdwrite(stdout=None, stderr=None)[source]¶
Bases:
object
Capture standard out/error.
- __init__(stdout=None, stderr=None)[source]¶
Initialize.
- Parameters:
stdout (
TextIOBase
) – the data sink for stdout (i.e.io.StringIO
)stdout – the data sink for stderr
zensols.util.tempfile module¶
Classes to generate, track and clean up temporary files.
- class zensols.util.tempfile.TemporaryFileName(directory=None, file_fmt='{name}', create=False, remove=True)[source]¶
Bases:
object
Create a temporary file names on the file system. Note that this does not create the file object itself, only the file names. In addition, the generated file names are tracked and allow for deletion. Specified directories are also created, which is only needed for non-system temporary directories.
The object is iterable, so usage with
itertools.islice
can be used to get as many temporary file names as desired. Calling instances (no arguments) generate a single file name.- __init__(directory=None, file_fmt='{name}', create=False, remove=True)[source]¶
Initialize with file system data.
- Parameters:
directory (
str
) – the parent directory of the generated filefile_fmt (
str
) – a file format that substitutesname
for the create temporary file name; defaults to{name}
create (
bool
) – ifTrue
, createdirectory
if it doesn’t existremove (
bool
) – ifTrue
, remove tracked generated file names if they exist
- See tempfile:
- class zensols.util.tempfile.tempfile(*args, **kwargs)[source]¶
Bases:
object
Generate a temporary file name and return the name in the
with
statement. Arguments to the form are the same asTemporaryFileName
. The temporary file is deleted after completion (seeTemporaryFileName
).Example:
with tempfile('./tmp', create=True) as fname: print(f'writing to {fname}') with open(fname, 'w') as f: f.write('this file will be deleted, but left with ./tmp\n')
zensols.util.time module¶
Peformance measure convenience utils.
- exception zensols.util.time.TimeoutError[source]¶
Bases:
Exception
Raised when a time out even occurs in
timeout()
ortimeprotect
.- __module__ = 'zensols.util.time'¶
- __weakref__¶
list of weak references to the object
- class zensols.util.time.time(msg='finished', level=20, logger=None)[source]¶
Bases:
object
Used in a
with
scope that executes the body and logs the elapsed time.Format f-strings are supported as the locals are taken from the calling frame on exit. This means you can do things like:
- with time(‘processed {cnt} items’):
cnt = 5 tm.sleep(1)
which produeces:
processed 5 items
.See the initializer documentation about special treatment for global loggers.
- __init__(msg='finished', level=20, logger=None)[source]¶
Create the time object.
If a logger is not given, it is taken from the calling frame’s global variable named
logger
. If this global doesn’t exit it logs to standard out. Otherwise, standard out/error can be used if givensys.stdout
orsys.stderr
.- Parameters:
msg (
str
) – the message log when exiting the closurelogger (
Union
[Logger
,TextIOBase
]) – the logger to use for logging or a file like object (i.e.sys.stdout
) as a data synclevel – the level at which the message is logged
- zensols.util.time.timeout(seconds=10, error_message='STREAM ioctl timeout')[source]¶
This creates a decorator called @timeout that can be applied to any long running functions.
So, in your application code, you can use the decorator like so:
from timeout import timeout # Timeout a long running function with the default expiry of # TIMEOUT_DEFAULT seconds. @timeout def long_running_function1(): pass
This was derived from the David Narayan’s StackOverflow thread.
- class zensols.util.time.timeprotect(seconds=10, timeout_handler=None, context=None, error_message='STREAM ioctl timeout')[source]¶
Bases:
object
Invokes a block and bails if not completed in a specified number of seconds.
- Parameters:
seconds – the number of seconds to wait
timeout_handler – function that takes a single argument, which is this
timeprotect
object instance; ifNone
, then nothing is done if the block times outcontext – an object accessible from the
timeout_hander
viaself
, which defaults toNone
- See: