adaptix package#

Subpackages#

Submodules#

Module contents#

adaptix.TypeHint#

alias of Any

class adaptix.DebugTrail(
value,
names=None,
*,
module=None,
qualname=None,
type=None,
start=1,
boundary=None,
)#

Bases: Enum

DISABLE = 'DISABLE'#
FIRST = 'FIRST'#
ALL = 'ALL'#
adaptix.loader(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
func: adaptix.Loader,
chain: Chain | None = None,
) Provider#

Basic provider to define custom loader.

Parameters:
  • pred – Predicate specifying where loader should be used. See Predicate system for details.

  • func – Function that acts as loader. It must take one positional argument of raw data and return the processed value.

  • chain

    Controls how the function will interact with the previous loader.

    When None is passed, the specified function will fully replace the previous loader.

    If a parameter is Chain.FIRST, the specified function will take raw data and its result will be passed to previous loader.

    If the parameter is Chain.LAST, the specified function gets result of the previous loader.

Returns:

Desired provider

adaptix.dumper(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
func: adaptix.Dumper,
chain: Chain | None = None,
) Provider#

Basic provider to define custom dumper.

Parameters:
  • pred – Predicate specifying where dumper should be used. See Predicate system for details.

  • func – Function that acts as dumper. It must take one positional argument of raw data and return the processed value.

  • chain

    Controls how the function will interact with the previous dumper.

    When None is passed, the specified function will fully replace the previous dumper.

    If a parameter is Chain.FIRST, the specified function will take raw data and its result will be passed to previous dumper.

    If the parameter is Chain.LAST, the specified function gets result of the previous dumper.

Returns:

Desired provider

adaptix.as_is_dumper(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
) Provider#

Provider that creates dumper which does nothing with input data.

Parameters:

pred – Predicate specifying where dumper should be used. See Predicate system for details.

Returns:

Desired provider

adaptix.as_is_loader(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
) Provider#

Provider that creates loader which does nothing with input data.

Parameters:

pred – Predicate specifying where loader should be used. See Predicate system for details.

Returns:

Desired provider

adaptix.constructor(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
func: Callable,
) Provider#
adaptix.with_property(
pred: Pred,
prop: NameOrProp,
tp: Omittable[TypeHint] = Omitted(),
/,
*,
default: Default = NoDefault(),
access_error: Catchable | None = None,
metadata: Mapping[Any, Any] = mappingproxy({}),
) Provider#
adaptix.validator(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
func: Callable[[Any], bool],
error: str | Callable[[Any], LoadError] | None = None,
chain: Chain = Chain.LAST,
) Provider#
adaptix.bound(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
provider: Provider,
) Provider#
adaptix.enum_by_exact_value(
*preds: Any | str | EnumType | LocStackPattern,
) Provider#

Provider that represents enum members to the outside world by their value without any processing.

Parameters:

preds – Predicates specifying where the provider should be used. The provider will be applied if any predicates meet the conditions, if no predicates are passed, the provider will be used for all Enums. See Predicate system for details.

Returns:

Desired provider

adaptix.enum_by_name(
*preds: Any | str | EnumType | LocStackPattern,
name_style: NameStyle | None = None,
map: Mapping[str | Enum, str] | None = None,
) Provider#

Provider that represents enum members to the outside world by their name.

Parameters:
  • preds – Predicates specifying where the provider should be used. The provider will be applied if any predicates meet the conditions, if no predicates are passed, the provider will be used for all Enums. See Predicate system for details.

  • name_style – Name style for representing members to the outside world. If it is set, the provider will automatically convert the names of enum members to the specified convention.

  • map – Mapping for representing members to the outside world. If it is set, the provider will use it to rename members individually; its keys can either be member names as strings or member instances.

Returns:

Desired provider

adaptix.enum_by_value(
first_pred: Any | str | EnumType | LocStackPattern,
/,
*preds: Any | str | EnumType | LocStackPattern,
tp: Any,
) Provider#

Provider that represents enum members to the outside world by their value by loader and dumper of specified type. The loader will call the loader of the tp and pass it to the enum constructor. The dumper will get value from eum member and pass it to the dumper of the tp.

Parameters:
  • first_pred – Predicate specifying where the provider should be used. See Predicate system for details.

  • preds – Additional predicates. The provider will be applied if any predicates meet the conditions.

  • tp – Type of enum members. This type must cover all enum members for the correct operation of loader and dumper

Returns:

Desired provider

adaptix.flag_by_exact_value(
*preds: Any | str | EnumType | LocStackPattern,
) Provider#

Provider that represents flag members to the outside world by their value without any processing. It does not support flags with skipped bits and negative values (it is recommended to use enum.auto() to define flag values instead of manually specifying them).

Parameters:

preds – Predicates specifying where the provider should be used. The provider will be applied if any predicates meet the conditions, if no predicates are passed, the provider will be used for all Flags. See Predicate system for details.

Returns:

Desired provider

adaptix.flag_by_member_names(
*preds: Any | str | EnumType | LocStackPattern,
allow_single_value: bool = False,
allow_duplicates: bool = True,
allow_compound: bool = True,
name_style: NameStyle | None = None,
map: Mapping[str | Enum, str] | None = None,
) Provider#

Provider that represents flag members to the outside world by list of their names.

Loader takes a flag members name list and returns united flag member (given members combined by operator |, namely bitwise or).

Dumper takes a flag member and returns a list of names of flag members, included in the given flag member.

Parameters:
  • preds – Predicates specifying where the provider should be used. The provider will be applied if any predicates meet the conditions, if no predicates are passed, the provider will be used for all Flags. See Predicate system for details.

  • allow_single_value – Allows calling the loader with a single value. If this is allowed, singlular values are treated as one element list.

  • allow_duplicates – Allows calling the loader with a list containing non-unique elements. Unless this is allowed, loader will raise DuplicatedValuesLoadError in that case.

  • allow_compound – Allows the loader to accept names of compound members (e.g. WHITE = RED | GREEN | BLUE) and the dumper to return names of compound members. If this is allowed, dumper will use compound members names to serialize value.

  • name_style – Name style for representing members to the outside world. If it is set, the provider will automatically convert the names of all flag members to the specified convention.

  • map – Mapping for representing members to the outside world. If it is set, the provider will use it to rename members individually; its keys can either be member names as strings or member instances.

Returns:

Desired provider

adaptix.name_mapping(
pred: Omittable[Pred] = Omitted(),
*,
skip: Omittable[Iterable[Pred] | Pred] = Omitted(),
only: Omittable[Iterable[Pred] | Pred] = Omitted(),
map: Omittable[NameMap] = Omitted(),
as_list: Omittable[bool] = Omitted(),
trim_trailing_underscore: Omittable[bool] = Omitted(),
name_style: Omittable[NameStyle | None] = Omitted(),
omit_default: Omittable[Iterable[Pred] | Pred | bool] = Omitted(),
extra_in: Omittable[ExtraIn] = Omitted(),
extra_out: Omittable[ExtraOut] = Omitted(),
chain: Chain | None = Chain.FIRST,
) Provider#

A name mapping decides which fields will be presented to the outside world and how they will look.

The mapping process consists of two stages: 1. Determining which fields are presented 2. Mutating names of presented fields

skip parameter has higher priority than only.

Mutating parameters works in that way: Mapper tries to use the value from the map. If the field is not presented in the map, trim trailing underscore and convert name style.

The field must follow snake_case to could be converted.

Parameters:
adaptix.default_dict(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
default_factory: Callable,
) Provider#

DefaultDict provider with overriden default_factory parameter

Parameters:
  • pred – Predicate specifying where the provider should be used. See Predicate system for details.

  • default_factory – default_factory parameter of the defaultdict instance to be created by the loader

class adaptix.AdornedRetort(
*,
recipe: Iterable[Provider] = (),
strict_coercion: bool = True,
debug_trail: DebugTrail = DebugTrail.ALL,
)#

Bases: OperatingRetort

A retort implementing high-level user interface

replace(
*,
strict_coercion: bool | None = None,
debug_trail: DebugTrail | None = None,
) AR#
extend(
*,
recipe: Iterable[Provider],
) AR#
get_loader(
tp: Type[T],
) Callable[[Any], T]#
get_dumper(
tp: Type[T],
) Callable[[T], Any]#
load(
data: Any,
tp: Type[T],
/,
) T#
load(data: Any, tp: Any, /) Any
recipe: ClassVar[Iterable[Provider]]#
dump(
data: T,
tp: Type[T],
/,
) Any#
dump(data: Any, tp: Any | None = None, /) Any
class adaptix.FilledRetort(
recipe: Iterable[Provider] = (),
)#

Bases: OperatingRetort, ABC

A retort contains builtin providers

recipe: ClassVar[Iterable[Provider]]#
class adaptix.Retort(
*,
recipe: Iterable[Provider] = (),
strict_coercion: bool = True,
debug_trail: DebugTrail = DebugTrail.ALL,
)#

Bases: FilledRetort, AdornedRetort

recipe: ClassVar[Iterable[Provider]]#
exception adaptix.TypedDictAt38Warning#

Bases: UserWarning

Runtime introspection of TypedDict at python3.8 does not support inheritance. Please update python or consider limitations suppressing this warning

class adaptix.Omitted#

Bases: object

exception adaptix.CannotProvide(
message: str = '',
*,
is_terminal: bool = False,
is_demonstrative: bool = False,
)#

Bases: Exception

exception adaptix.AggregateCannotProvide(
message: str,
exceptions: Sequence[CannotProvide],
*,
is_terminal: bool = False,
is_demonstrative: bool = False,
)#

Bases: ExceptionGroup[CannotProvide], CannotProvide

derive(
excs: Sequence[CannotProvide],
) AggregateCannotProvide#
derive_upcasting(
excs: Sequence[CannotProvide],
) CannotProvide#

Same as method derive but allow passing an empty sequence

classmethod make(
message: str,
exceptions: Sequence[CannotProvide],
*,
is_terminal: bool = False,
is_demonstrative: bool = False,
) CannotProvide#
class adaptix.Chain(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)#

Bases: Enum

FIRST = 'FIRST'#
LAST = 'LAST'#
class adaptix.ExtraCollect#

Bases: object

Collect extra data and pass it to object

class adaptix.ExtraForbid#

Bases: object

Raise error if extra data would be met

class adaptix.ExtraKwargs#

Bases: object

class adaptix.ExtraSkip#

Bases: object

Ignore any extra data

class adaptix.Mediator#

Bases: ABC, Generic[V]

Mediator is an object that gives provider access to other providers and that stores the state of the current search.

Mediator is a proxy to providers of retort.

abstract provide(
request: Request[T],
) T#

Get response of sent request.

Parameters:

request – A request instance

Returns:

Result of the request processing

Raises:

CannotProvide – A provider able to process the request does not be found

abstract provide_from_next() V#

Forward current request to providers that placed after current provider at the recipe.

final delegating_provide(
request: Request[T],
error_describer: Callable[[CannotProvide], str] | None = None,
) T#
final mandatory_provide(
request: Request[T],
error_describer: Callable[[CannotProvide], str] | None = None,
) T#
final mandatory_provide_by_iterable(
requests: Iterable[Request[T]],
error_describer: Callable[[], str] | None = None,
) Iterable[T]#
class adaptix.NameStyle(
value,
names=None,
*,
module=None,
qualname=None,
type=None,
start=1,
boundary=None,
)#

Bases: Enum

An enumeration of different naming conventions

LOWER_SNAKE = 'lower_snake'#
CAMEL_SNAKE = 'camel_Snake'#
PASCAL_SNAKE = 'Pascal_Snake'#
UPPER_SNAKE = 'UPPER_SNAKE'#
LOWER_KEBAB = 'lower-kebab'#
CAMEL_KEBAB = 'camel-Kebab'#
PASCAL_KEBAB = 'Pascal-Kebab'#
UPPER_KEBAB = 'UPPER-KEBAB'#
LOWER = 'lowercase'#
CAMEL = 'camelCase'#
PASCAL = 'PascalCase'#
UPPER = 'UPPERCASE'#
LOWER_DOT = 'lower.dot'#
CAMEL_DOT = 'camel.Dot'#
PASCAL_DOT = 'Pascal.Dot'#
UPPER_DOT = 'UPPER.DOT'#
class adaptix.LocStackPattern(
stack: Tuple[LocStackChecker, ...],
)#

Bases: object

property ANY: AnyLocStackChecker#
generic_arg(
pos: int,
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
) Pat#
build_loc_stack_checker() LocStackChecker#
adaptix.create_loc_stack_checker(
pred: str | Pattern | type | Any | LocStackChecker | LocStackPattern,
) LocStackChecker#
class adaptix.Provider#

Bases: ABC

An object that can process Request instances

abstract apply_provider(
mediator: Mediator[T],
request: Request[T],
) T#

Handle request instance and return a value of type required by request. Behavior must be the same during the provider object lifetime

Raises:

CannotProvide – provider cannot process passed request

exception adaptix.NoSuitableProvider(message: str)#

Bases: Exception

class adaptix.Request#

Bases: Generic[T]

An object that contains data to be processed by Provider.

Generic argument indicates which object should be returned after request processing.

Request must always be a hashable object

adaptix.load(data: Any, tp: Any, /)#
adaptix.dump(data: Any, tp: Any | None = None, /) Any#