egrecho.utils.patch.simple_parse_patch._utils#
Utility functions used in various parts of the simple_parsing package.
- egrecho.utils.patch.simple_parse_patch._utils.str2bool(raw_value)[source]#
Taken from https://stackoverflow.com/questions/15008758/parsing-boolean-values-with-argparse
- Return type:
bool
- egrecho.utils.patch.simple_parse_patch._utils.is_literal(t)[source]#
Returns True with t is a Literal type.
>>> from typing_extensions import Literal >>> from typing import * >>> is_literal(list) False >>> is_literal("foo") False >>> is_literal(Literal[True, False]) True >>> is_literal(Literal[1,2,3]) True >>> is_literal(Literal["foo", "bar"]) True :rtype: :sphinx_autodoc_typehints_type:`\:py\:class\:\`bool\``
>>> is_literal(Optional[Literal[1,2]]) False
- egrecho.utils.patch.simple_parse_patch._utils.is_list(t)[source]#
returns True when t is a List type.
- Parameters:
t (Type) -- a type.
- Returns:
True if t is list or a subclass of list.
- Return type:
bool
>>> from typing import * >>> is_list(list) True >>> is_list(tuple) False >>> is_list(List) True >>> is_list(List[int]) True >>> is_list(List[Tuple[int, str, None]]) True >>> is_list(Optional[List[int]]) False >>> class foo(List[int]): ... pass ... >>> is_list(foo) True
- egrecho.utils.patch.simple_parse_patch._utils.is_tuple(t)[source]#
returns True when t is a tuple type.
- Parameters:
t (Type) -- a type.
- Returns:
True if t is tuple or a subclass of tuple.
- Return type:
bool
>>> from typing import * >>> is_tuple(list) False >>> is_tuple(tuple) True >>> is_tuple(Tuple) True >>> is_tuple(Tuple[int]) True >>> is_tuple(Tuple[int, str, None]) True >>> class foo(tuple): ... pass ... >>> is_tuple(foo) True >>> is_tuple(List[int]) False
- egrecho.utils.patch.simple_parse_patch._utils.is_dict(t)[source]#
returns True when t is a dict type or annotation.
- Parameters:
t (Type) -- a type.
- Returns:
True if t is dict or a subclass of dict.
- Return type:
bool
>>> from typing import * >>> from collections import OrderedDict >>> is_dict(dict) True >>> is_dict(OrderedDict) True >>> is_dict(tuple) False >>> is_dict(Dict) True >>> is_dict(Dict[int, float]) True >>> is_dict(Dict[Any, Dict]) True >>> is_dict(Optional[Dict]) False >>> is_dict(Mapping[str, int]) True >>> class foo(Dict): ... pass ... >>> is_dict(foo) True
- egrecho.utils.patch.simple_parse_patch._utils.is_set(t)[source]#
returns True when t is a set type or annotation.
- Parameters:
t (Type) -- a type.
- Returns:
True if t is set or a subclass of set.
- Return type:
bool
>>> from typing import * >>> is_set(set) True >>> is_set(Set) True >>> is_set(tuple) False >>> is_set(Dict) False >>> is_set(Set[int]) True >>> is_set(Set["something"]) True >>> is_set(Optional[Set]) False >>> class foo(Set): ... pass ... >>> is_set(foo) True
- egrecho.utils.patch.simple_parse_patch._utils.is_union(t)[source]#
Returns whether or not the given Type annotation is a variant (or subclass) of typing.Union
- Parameters:
t (Type) -- some type annotation
- Returns:
Whether this type represents a Union type.
- Return type:
bool
>>> from typing import * >>> is_union(Union[int, str]) True >>> is_union(Union[int, str, float]) True >>> is_union(Tuple[int, str]) False
egrecho.utils.patch.simple_parse_patch.decoding#
Functions for decoding dataclass fields from “raw” values (e.g. from json).
- egrecho.utils.patch.simple_parse_patch.decoding.register_decoding_fn(some_type, function, overwrite=False)[source]#
Register a decoding function for the type some_type.
- egrecho.utils.patch.simple_parse_patch.decoding.decoding_fn_for_type(some_type)[source]#
Registers a function to be used to convert a serialized value to the given type.
The function should accept one argument (the serialized value) and return the decoded value.
- Return type:
Callable
[[TypeVar
(C
, bound=Callable
[[Any
],Any
])],TypeVar
(C
, bound=Callable
[[Any
],Any
])]
- egrecho.utils.patch.simple_parse_patch.decoding.decode_field(field, raw_value, containing_dataclass=None, drop_extra_fields=None)[source]#
Converts a “raw” value (e.g. from json file) to the type of the field.
When serializing a dataclass to json, all objects are converted to dicts. The values which have a special type (not str, int, float, bool) are converted to string or dict. Hence this function allows us to recover the original type of pretty much any field which is of type Serializable, or has a registered decoding function (either through register_decoding_fn or through having decoding_fn passed directly to the field function.
- Parameters:
field (Field) -- Field object from a dataclass.
raw_value (Any) -- The raw value from deserializing the dataclass.
- Returns:
The “raw” value converted to the right type.
- Return type:
Any
- egrecho.utils.patch.simple_parse_patch.decoding.get_decoding_fn(type_annotation)[source]#
Fetches/Creates a decoding function for the given type annotation.
This decoding function can then be used to create an instance of the type when deserializing dicts (which could have been obtained with JSON or YAML).
This function inspects the type annotation and creates the right decoding function recursively in a “dynamic-programming-ish” fashion. NOTE: We cache the results in a functools.lru_cache decorator to avoid wasteful calls to the function. This makes this process pretty efficient.
- Parameters:
t (Type[T]) --
A type or type annotation. Can be arbitrarily nested. For example:
List[int]
Dict[str, Foo]
Tuple[int, str, Any],
Dict[Tuple[int, int], List[str]]
List[List[List[List[Tuple[int, str]]]]]
etc.
- Returns:
A function that decodes a ‘raw’ value to an instance of type t.
- Return type:
Callable[[Any], T]
- egrecho.utils.patch.simple_parse_patch.decoding.try_functions(*funcs)[source]#
Tries to use the functions in succession, else returns the same value unchanged.
- Return type:
Callable[[Any], T | Any]
- egrecho.utils.patch.simple_parse_patch.decoding.decode_tuple(*tuple_item_types)[source]#
Makes a parsing function for creating tuples.
Can handle tuples with different item types, for instance: - Tuple[int, Foo, str, float, …].
- Returns:
A parsing function for creating tuples.
- Return type:
Callable[[List[T]], Tuple[T, …]]
- egrecho.utils.patch.simple_parse_patch.decoding.decode_set(item_type)[source]#
Makes a parsing function for creating sets with items of type item_type.
- Parameters:
item_type (Type[T]) -- the type of the items in the set.
- Returns:
[description]
- Return type:
Callable[[List[T]], Set[T]]
- egrecho.utils.patch.simple_parse_patch.decoding.decode_dict(K_, V_)[source]#
Creates a decoding function for a dict type. Works with OrderedDict too.
- Parameters:
K (Type[K]) -- The type of the keys.
V (Type[V]) -- The type of the values.
- Returns:
A function that parses a dict.
- egrecho.utils.patch.simple_parse_patch.decoding.decode_enum(item_type)[source]#
Creates a decoding function for an enum type.
- Parameters:
item_type (Type[Enum]) -- the type of the items in the set.
- Returns:
A function that returns the enum member for the given name.
- Return type:
Callable[[str], Enum]
- egrecho.utils.patch.simple_parse_patch.decoding.decode_literal(*possible_vals)[source]#
Creates a decoding function for a Literal type.
- Parameters:
*possible_vals (Any) -- The permissible values for the Literal type.
- Returns:
- A function that checks if a given value is one of the
permissible values for the Literal. If not, raises a TypeError.
- Return type:
Callable[[Any], Any]
egrecho.utils.patch.simple_parse_patch.serializable#
- egrecho.utils.patch.simple_parse_patch.serializable.default_value(field)[source]#
Returns the default value of a field in a dataclass, if available. When not available, returns
dataclasses.MISSING
.- Parameters:
field (dataclasses.Field) -- The dataclasses.Field to get the default value of.
- Returns:
The default value for that field, if present, or None otherwise.
- Return type:
Union[T, _MISSING_TYPE]
- egrecho.utils.patch.simple_parse_patch.serializable.asdict_filt(obj, *, dict_factory=<class 'dict'>, filt_type='none', init_field_only=False, save_dc_types=False)[source]#
Recursively converts a dataclass/dict object into a filtered dictionary representation.
- Parameters:
obj -- The dataclass or dictionary object to convert.
dict_factory -- The factory function to create the resulting dictionary (default: dict).
filt_type (
Literal
['default'
,'none'
,'orig'
]) --The type of filtering to apply (default: ‘none’).
’default’: Filters out default values in the dataclass object.
’none’: Filters out None values in the dataclass/dict object.
’orig’: Original dataclasses.asdict() behavior without filtering.
init_field_only -- If True, considers only fields with
init == True
in dataclasses (default: False).save_dc_types -- If True, saves the type information of dataclasses in the serialized dictionary (default: False).
- Return type:
dict
- Returns:
A filtered dictionary representation of the input object.
- Raises:
TypeError -- If the input object is not a dictionary or a dataclass.
Note
This function is intended to be used as a replacement for the
dataclasses.asdict()
function.The
init_field_only
parameter is only applicable when the input object is a dataclass, and it controls whether only fields withinit == True
are considered.The
save_dc_types
parameter is used to include the type information of dataclasses in the serialized dictionary.
- egrecho.utils.patch.simple_parse_patch.serializable.from_dict(cls, d, drop_extra_fields=None)[source]#
Parses an instance of the dataclass
cls
from the dictd
.- Parameters:
cls (Type[Dataclass]) -- A
dataclass
type.d (Dict[str, Any]) -- A dictionary of raw values, obtained for example when deserializing a json file into an instance of class
cls
.drop_extra_fields (bool, optional) --
Whether or not to drop extra dictionary keys (dataclass fields) when encountered. There are three options:
- True: The extra keys are dropped, and this function returns an
instance of
cls
.
- False: The extra keys (if any) are kept, and we search through the
subclasses of
cls
for the first dataclass which has all the required fields.
None (default):
drop_extra_fields = not cls.decode_into_subclasses
.
- Raises:
RuntimeError -- If an error is encountered while instantiating the class.
- Returns:
An instance of the dataclass
cls
.- Return type:
Dataclass