jztools.py#
Functions
|
|
|
|
|
Converts |
|
Prunes entries from a dictionary with the specified value ( |
|
Produces a string representation of the input. |
|
|
|
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. |
|
Returns the full string produced when the exception is raised. |
|
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. |
|
|
|
Returns the fully qualified name of the calling method or function. |
|
Traverses the input container hierarchy tree, and returns the sequence of keys with values that satisfy the specified criterion. |
|
Example: ClassA = import_from_string('module.submodule.ClassA') |
|
|
|
Given a module and class path of the form |
|
|
|
Sets the attributes specified as keywords. |
|
|
|
Returns tuples of N items from L in each iteration. |
|
Raises an exception if the input iterators do not have the same length. |
Classes
Encodes/decodes exceptions as traceback strings. |
|
|
|
Encoder/decoder that encodes a multi-line string as a single-line string, attempting to maintain readability. |
Exceptions
- jztools.py.dict_prune(x: Dict, prune_val=None)#
Prunes entries from a dictionary with the specified value (
Noneby default).
- jztools.py.get_caller_name(offset=1, stack_entry=None)#
Returns the fully qualified name of the calling method or function.
if
stack_entryis provided,offsetis 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 buildingin_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_strto 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:
objectEncoder/decoder that encodes a multi-line string as a single-line string, attempting to maintain readability.
- class jztools.py.ErrorEncoder#
Bases:
ReadableMultilineEncodes/decodes exceptions as traceback strings. Decoding can optionally (and by default) colorize the string for terminal display.