jztools.py#

Functions

class_from_name(in_class_name[, ...])

param in_class_name:

The representation produced by class_name()

class_name(in_class[, stripped_modules])

param in_class:

The class to represent as a string.

cutoff_str(in_str[, length, suffix])

Converts in_str to string and cuts off + appends suffix if too long.

dict_prune(x[, prune_val])

Prunes entries from a dictionary with the specified value (None by default).

display(val)

Produces a string representation of the input.

entity_from_name(in_name)

entity_name(in_entity[, stripped_modules])

Works for a module or any object with a __qualname__ (e.g., a class), but inversion will fail for 1) bound instance methods, 2) object instances and local variables.

exception_string(exc)

Returns the full string produced when the exception is raised.

filelike_open(filelike, *args, **kwargs)

Takes 1) a file path specification and opens with the specified arguments, or 2) an open IO object (e.g., a file pointer) and returns that.

get_caller([offset])

get_caller_name([offset, stack_entry])

Returns the fully qualified name of the calling method or function.

get_nested_keys(d, criterion)

Traverses the input container hierarchy tree, and returns the sequence of keys with values that satisfy the specified criterion.

import_from_string(path)

Example: ClassA = import_from_string('module.submodule.ClassA')

not_implemented_property(name)

obj_from_string(class_path, *args, **kwargs)

Given a module and class path of the form module.submodule.ClassName, returns an instance of that object with *args and **kwargs used to instantiate it.

parent_entity(in_entity)

setattrs(obj, **kwargs)

Sets the attributes specified as keywords.

setdefaultattr(obj, name, value)

strict_chunks(L, N)

Returns tuples of N items from L in each iteration.

strict_zip(*args)

Raises an exception if the input iterators do not have the same length.

Classes

ErrorEncoder()

Encodes/decodes exceptions as traceback strings.

LazyObject(object_constructor)

ReadableMultiline()

Encoder/decoder that encodes a multi-line string as a single-line string, attempting to maintain readability.

Exceptions

StrictZipException()

jztools.py.dict_prune(x: Dict, prune_val=None)#

Prunes entries from a dictionary with the specified value (None by default).

jztools.py.get_caller_name(offset=1, stack_entry=None)#

Returns the fully qualified name of the calling method or function.

if stack_entry is provided, offset is ignored.

jztools.py.setattrs(obj, **kwargs)#

Sets the attributes specified as keywords. The returned object is the same as the input object, with attributes modified.

Example:

obj = Rectangle()
attr_update(obj, width=10, height=20)
exception jztools.py.StrictZipException#

Bases: Exception

jztools.py.strict_zip(*args)#

Raises an exception if the input iterators do not have the same length.

jztools.py.import_from_string(path)#

Example: ClassA = import_from_string(‘module.submodule.ClassA’)

jztools.py.obj_from_string(class_path, *args, **kwargs)#

Given a module and class path of the form module.submodule.ClassName, returns an instance of that object with *args and **kwargs used to instantiate it.

jztools.py.strict_chunks(L, N)#

Returns tuples of N items from L in each iteration. If some chunks remain, raises an exception.

jztools.py.exception_string(exc: Exception)#

Returns the full string produced when the exception is raised.

jztools.py.get_nested_keys(d: Dict[str, Any] | List, criterion: Callable)#

Traverses the input container hierarchy tree, and returns the sequence of keys with values that satisfy the specified criterion.

Traveral is halted for a given branch if the criterion returns True.

jztools.py.entity_name(in_entity, stripped_modules=None)#

Works for a module or any object with a __qualname__ (e.g., a class), but inversion will fail for 1) bound instance methods, 2) object instances and local variables.

Parameters:
  • in_class – The class to represent as a string.

  • stripped_modules – A list of modules objects to strip in the string representation. If module X is in the list, X.MyClass is represented as ‘MyClass’ instead of ‘X.MyClass’. Currently defaults to an empty list. In the future, it will contain module ‘builtins’ by default.

jztools.py.class_name(in_class, stripped_modules=None)#
Parameters:
  • in_class – The class to represent as a string.

  • stripped_modules – A list of modules objects to strip in the string representation. If module X is in the list, X.MyClass is represented as ‘MyClass’ instead of ‘X.MyClass’. Currently defaults to an empty list. In the future, it will contain module ‘builtins’ by default.

jztools.py.class_from_name(in_class_name, stripped_modules=())#
Parameters:
  • in_class_name – The representation produced by class_name()

  • stripped_modules – The same value passed to class_name() when building in_class_name.

jztools.py.filelike_open(filelike: str | IOBase | PurePath, *args, **kwargs)#

Takes 1) a file path specification and opens with the specified arguments, or 2) an open IO object (e.g., a file pointer) and returns that.

jztools.py.cutoff_str(in_str: Any, length: int = 2000, suffix: str = ' ...')#

Converts in_str to string and cuts off + appends suffix if too long.

jztools.py.display(val: Any)#

Produces a string representation of the input. For string, in particular, the string representation includes single quotes.

class jztools.py.ReadableMultiline#

Bases: object

Encoder/decoder that encodes a multi-line string as a single-line string, attempting to maintain readability.

class jztools.py.ErrorEncoder#

Bases: ReadableMultiline

Encodes/decodes exceptions as traceback strings. Decoding can optionally (and by default) colorize the string for terminal display.