hbutils.model.raw

This module provides support for wrapping and unwrapping raw values based on conditions.

The module offers a factory function to create raw/unraw functions and a proxy class that can conditionally wrap values. This is useful for marking certain values as “raw” to prevent further processing or transformation.

raw_support

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

Get raw support wrapper function and class.

This function creates a set of utilities for conditionally wrapping values in a proxy class. Values that satisfy the given condition can be wrapped to mark them as “raw”, preventing further processing. The wrapped values can later be unwrapped to retrieve the original value.

Parameters:
  • condition (callable) – Condition function or callable that determines when a value should be wrapped. The value will be wrapped only when condition returns True.

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

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

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

Returns:

A tuple containing (raw_func, unraw_func, proxy_class) where: - raw_func: Function to wrap values that satisfy the condition - unraw_func: Function to unwrap proxy objects back to original values - proxy_class: The proxy class used for wrapping

Return type:

tuple

Examples::
>>> from hbutils.model 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(1)
1
>>> unraw({'a': 1, 'b': 2})
{'a': 1, 'b': 2}
>>> unraw(raw({'a': 1, 'b': 2}))
{'a': 1, 'b': 2}