embutils.utils.parsed module

Parsed object implementation. In this context parse is converting data to/from text.

date

2022

author

Christian Wiche

contact

cwichel@gmail.com

license

The MIT License (MIT)

class embutils.utils.parsed.ParseModel[source]

Bases: object

Parseable object base implementation. This class gives parsing capabilities for an object.

CONVERTER = <cattr.converters.Converter object>

Class to object converter

PROTOCOL = ParseProtocolItem(parse=functools.partial(<function ParseProtocol.<lambda>>), export=functools.partial(<function ParseProtocol.<lambda>>), suffixes=['.yaml', '.yml'])

Default parse protocol

dict(exc_none: bool = True, exc_empty: bool = True) dict[source]

Generate a dictionary representation of the model.

Parameters
  • exc_none (bool) – True to exclude None values.

  • exc_empty (bool) – True to exclude empty values: “”, [], {}.

Returns

Object as dictionary.

Return type

dict

export(path: Optional[Any] = None, protocol: Optional[embutils.utils.parsed.ParseProtocol] = None, exc_none: bool = True, exc_empty: bool = True, **kwargs) str[source]

Export the model to a text/file using a protocolized representation.

Parameters
  • path (TPAny) – Path to store the exported object.

  • protocol (ParseProtocol) – Representation protocol.

  • exc_none (bool) – True to exclude None values.

  • exc_empty (bool) – True to exclude empty values: “”, [], {}.

Returns

Object converted to protocol.

Return type

str

classmethod obj_exclude(obj: embutils.utils.parsed.ParseModel.dict, keys: Optional[List[str]] = None, values: Optional[List[Any]] = None, exc_none: bool = True, exc_empty: bool = True) embutils.utils.parsed.ParseModel.dict[source]

Dictionary exclusion utility.

Note
  • The exclusion is applied recursively only on dictionaries.

  • If a list or tuple is encountered the filter will be applied only on dictionary items.

Parameters
  • obj (dict) – Object to be filtered.

  • keys (tp.List[str]) – List of keys to exclude.

  • values (tp.List[str]) – List of values to exclude.

  • exc_none (bool) – True to exclude None values.

  • exc_empty (bool) – True to exclude empty values: “”, [], {}.

Returns

Filtered dictionary.

Return type

dict

classmethod parse_file(path: Any, encoding: str = 'utf-8', protocol: Optional[embutils.utils.parsed.ParseProtocol] = None, exc_none: bool = True, exc_empty: bool = True, **kwargs) embutils.utils.parsed.ParseModel[source]

Parses the model from file.

Parameters
  • path (TPAny) – Path to source file.

  • encoding (str) – Data encoding.

  • protocol (ParseProtocol) – Representation protocol.

  • exc_none (bool) – True to exclude None values.

  • exc_empty (bool) – True to exclude empty values: “”, [], {}.

Returns

Parsed object.

Return type

ParseModel

Raises

ValueError – Provided path has an unsupported type.

classmethod parse_obj(obj: Any, exc_none: bool = True, exc_empty: bool = True) embutils.utils.parsed.ParseModel[source]

Parses the model from a dictionary-like object.

Parameters
  • obj (TPAny) – Data to be parsed.

  • exc_none (bool) – True to exclude None values.

  • exc_empty (bool) – True to exclude empty values: “”, [], {}.

Returns

Parsed object.

Return type

ParseModel

Raises

ValueError – Provided object cant be converted to dictionary.

classmethod parse_raw(data: Union[bytes, bytearray, str], encoding: str = 'utf-8', protocol: Optional[embutils.utils.parsed.ParseProtocol] = None, exc_none: bool = True, exc_empty: bool = True, **kwargs) embutils.utils.parsed.ParseModel[source]

Parses the model from raw data.

Parameters
  • data (TPText) – Data to be parsed.

  • encoding (str) – Data encoding.

  • protocol (ParseProtocol) – Representation protocol.

  • exc_none (bool) – True to exclude None values.

  • exc_empty (bool) – True to exclude empty values: “”, [], {}.

Returns

Parsed object.

Return type

ParseModel

Raises

ValueError – Provided data has an unsupported type.

class embutils.utils.parsed.ParseProtocol(value)[source]

Bases: enum.Enum

Available parse protocol definitions.

JSON = ParseProtocolItem(parse=functools.partial(<function ParseProtocol.<lambda>>), export=functools.partial(<function ParseProtocol.<lambda>>), suffixes=['.json', '.js'])
YAML = ParseProtocolItem(parse=functools.partial(<function ParseProtocol.<lambda>>), export=functools.partial(<function ParseProtocol.<lambda>>), suffixes=['.yaml', '.yml'])
class embutils.utils.parsed.ParseProtocolItem(parse: Callable[[...], dict], export: Callable[[...], str], suffixes: List[str])[source]

Bases: object

Parse protocol item.

Method generated by attrs for class ParseProtocolItem.

export: Callable[[...], str]

Exporting method (dict to string…)

parse: Callable[[...], dict]

Parsing method (string to dict…)

suffixes: List[str]

Allowed file suffixes