order.util#

Helpful utilities.

Data:

no_value

Unique object denoting no value.

Functions:

has_attr(obj, attr[, placeholder])

Safer version of hasattr() that includes attributes dynamically provided by hooks such as __getattr__.

colored(msg[, color, background, style, force])

Return the colored version of a string msg.

maybe_colored(msg, *args, **kwargs)

Returns a colored representation of msg using colored() if the global settings allow coloring, and the unchanged msg otherwise.

uncolored(s)

Removes all terminal style codes from a string s and returns it.

create_hash(inp[, l, algo, to_int])

Takes an arbitrary input inp and creates a hexadecimal string hash based on an algorithm algo.

Classes:

validated(fvalidate, ...)

Shorthand for a property definition that can be used as a decorator to wrap around a validation function.

DotAccessProxy(getter[, setter, dir])

Proxy object that provides simple attribute access to values that are retrieved by a getter and optionally set through a setter.

Repr(value)

Factory for objects with a configurable representation.

no_value = <object object>#

Unique object denoting no value.

has_attr(obj: ~typing.Any, attr: str, placeholder: ~typing.Any = <object object>) bool[source]#

Safer version of hasattr() that includes attributes dynamically provided by hooks such as __getattr__.

colored(msg: str, color: str | int | None = None, background: str | int | None = None, style: str | int | None = None, force: bool = False) str[source]#

Return the colored version of a string msg. For color, background and style options, see https://misc.flogisoft.com/bash/tip_colors_and_formatting. They can also be explicitely set to "random" to get a random value. Unless force is True, the msg string is returned unchanged in case the output is neither a tty nor an IPython output stream.

maybe_colored(msg: str, *args, **kwargs) str[source]#

Returns a colored representation of msg using colored() if the global settings allow coloring, and the unchanged msg otherwise.

uncolored(s: str) str[source]#

Removes all terminal style codes from a string s and returns it.

create_hash(inp: Any, l: int = 10, algo: str = 'sha256', to_int: bool = False) str | int[source]#

Takes an arbitrary input inp and creates a hexadecimal string hash based on an algorithm algo. For valid algorithms, see python’s hashlib. l corresponds to the maximum length of the returned hash and is limited by the length of the hexadecimal representation produced by the hashing algorithm. When to_int is True, the decimal integer representation is returned.

class validated(fvalidate: ~typing.Callable[[~typing.Any], ~typing.Any] | None = None, setter: bool = True, deleter: bool = True, default: ~typing.Any = <object object>, attr: str | None = None)[source]#

Shorthand for a property definition that can be used as a decorator to wrap around a validation function. Example:

class MyClass(object):

    def __init__(self):
        self._foo: str = None

    @validate
    def foo(self, foo: str) -> str:
        if not isinstance(foo, str):
            raise TypeError(f"not a string: '{str}'")
        return foo

myInstance = MyClass()
myInstance.foo = 123   -> TypeError
myInstance.foo = "bar" -> ok
print(myInstance.foo)  -> prints "bar"

In the exampe above, set/get calls target the instance member _foo, i.e. “_<function_name>”. The attribute name can be configured by setting attr.

If setter (deleter) is True (the default), a setter (deleter) method is booked as well. Prior to updating the member when the setter is called, fvalidate is invoked which may implement validation checks.

In case the attribute has not been added before the getter is called, default is returned if provided. Otherwise, an AttributeError might be raised.

class DotAccessProxy(getter: Callable[[str], Any], setter: Callable[[str, Any], None] | None = None, dir: Callable[[], dict[str, Any]] | None = None)[source]#

Proxy object that provides simple attribute access to values that are retrieved by a getter and optionally set through a setter. Example:

my_dict = {"foo": 123}

proxy = DotAccessProxy(my_dict.__getattr__)
proxy.foo
# -> 123
proxy.bar
# -> AttributeError

proxy = DotAccessProxy(my_dict.get)
proxy.foo
# -> 123
proxy.bar
# -> None

proxy = DotAccessProxy(my_dict.get, my_dict.__setitem__)
proxy.foo
# -> 123
proxy.bar
# -> None
proxy.bar = 99
proxy.bar
# -> 99

Additionally, if a dir callable is provided, it is invoked by the internal __dir__() method.

class Repr(value: Any)[source]#

Factory for objects with a configurable representation.