hbutils.model.raw

Conditional raw value wrapping utilities.

This module provides a factory function that builds a raw-value wrapper, an unwrapping helper, and a proxy class for marking values as “raw”. Wrapped values are intended to bypass additional processing stages and can be unwrapped later to restore the original value.

The module contains the following public component:

  • raw_support() - Factory for creating raw/unraw functions and a proxy class

Example:

>>> from hbutils.model.raw import raw_support
>>> raw, unraw, RawProxy = raw_support(lambda x: isinstance(x, dict))
>>> raw(1)
1
>>> raw({'a': 1, 'b': 2})
<RawProxy value: {'a': 1, 'b': 2}>
>>> unraw(raw({'a': 1, 'b': 2}))
{'a': 1, 'b': 2}

Note

The proxy class returned by raw_support() is dynamically created and named according to the proxy_name argument.

__all__

hbutils.model.raw.__all__ = ['raw_support']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

raw_support

hbutils.model.raw.raw_support(condition: Callable[[...], bool], raw_name: str = 'raw', unraw_name: str = 'unraw', proxy_name: str = 'RawProxy') Tuple[Callable[[Any], Any], Callable[[Any], Any], Type[Any]][source]

Create raw/unraw helpers and a proxy class based on a condition.

This factory function builds a set of utilities for conditionally wrapping values in a proxy class. When a value satisfies the provided condition, it will be wrapped in the dynamically created proxy class. Wrapped values can later be restored with the unwrapping function.

The condition is passed through hbutils.reflection.dynamic_call() to allow flexible signatures and to ignore extra arguments when necessary.

Parameters:
  • condition (Callable[..., bool]) – Predicate callable that determines whether a value should be wrapped. The value is wrapped only when the predicate returns True.

  • raw_name (str, optional) – Name for the raw wrapping function, defaults to 'raw'.

  • unraw_name (str, optional) – Name for the unwrapping function, defaults to 'unraw'.

  • proxy_name (str, optional) – Name for the proxy class, defaults to 'RawProxy'.

Returns:

A tuple of (raw_func, unraw_func, proxy_class) where:

  • raw_func wraps values that satisfy the condition.

  • unraw_func unwraps proxy objects back to original values.

  • proxy_class is the dynamically generated proxy class.

Return type:

tuple[Callable[[Any], Any], Callable[[Any], Any], type]

Example:

>>> raw, unraw, RawProxy = raw_support(lambda x: isinstance(x, dict))
>>> raw(1)
1
>>> raw({'a': 1, 'b': 2})
<RawProxy value: {'a': 1, 'b': 2}>
>>> unraw(raw({'a': 1, 'b': 2}))
{'a': 1, 'b': 2}