ucbreq module

Mostly useless serialization format.

This is a format based on a series of keys and values, and optional list values.

Effectively, the format is a list of key|value lines, where keys may be made into a list by either repeating them or suffixing them with an _1 index, incrementing.

class ucbreq.WorkValue(plain: Optional[MutableSequence] = None, indexed: Optional[List[Tuple[int, str]]] = None)

Bases: object

A working parsing list for actively parsing values in.

This is used for splitting plain and indexed values, and elegantly handling multiple plain values, or plain and indexed values appearing together with the same key.

These conditions are technically illegal, but we don’t enforce them.

value(list_class: ~typing.Callable[[], ~typing.MutableSequence[str]] = <class 'list'>) Union[str, MutableSequence[str]]

If there is just a single plain value, return that, otherwise return a list of all the plain values followed by all the indexed values sorted by index.

ucbreq.dump(obj: Mapping[str, Union[str, Sequence[str]]], fp: TextIO, startindex: int = 1, separator: str = '|', index_separator: str = '_')

Dump an object in req format to the fp given.

If there are redundant elements (like in {'Foo': ['Bar'], 'Foo_1': 'Bar'}}) the first encountered equivalent key will always be the only one written.

Parameters
  • obj – The object to serialize.

  • fp – A writable that can accept all the types given.

  • separator – The separator between key and value.

  • index_separator – The separator between key and index.

ucbreq.dumps(obj: Mapping[str, Union[str, Sequence[str]]], startindex: int = 1, separator: str = '|', index_separator: str = '_') str

Dump an object in req format to a string.

Parameters
  • obj – The object to serialize.

  • separator – The separator between key and value.

  • index_separator – The separator between key and index.

ucbreq.load(fp: ~typing.TextIO, keep_originals: bool = False, separator: str = '|', index_separator: str = '_', dict_class: ~typing.Callable[[], ~typing.MutableMapping[str, ~typing.Union[str, ~typing.MutableSequence[str]]]] = <class 'dict'>, list_class: ~typing.Callable[[], ~typing.MutableSequence[str]] = <class 'list'>, work_dict_class: ~typing.Callable[[], ~typing.MutableMapping[str, ~ucbreq.WorkValue]] = <class 'dict'>) Mapping[str, Union[str, Sequence[str]]]

Load an object from the file pointer.

Parameters
  • fp – A readable filehandle.

  • keep_originals – If True, keep the original indexed lines as well.

  • separator – The separator between key and value.

  • index_separator – The separator between key and index.

ucbreq.loads(s: str, keep_originals: bool = False, separator: str = '|', index_separator: str = '_', dict_class: ~typing.Callable[[], ~typing.MutableMapping[str, ~typing.Union[str, ~typing.MutableSequence[str]]]] = <class 'dict'>, list_class: ~typing.Callable[[], ~typing.MutableSequence[str]] = <class 'list'>, work_dict_class: ~typing.Callable[[], ~typing.MutableMapping[str, ~ucbreq.WorkValue]] = <class 'dict'>) Mapping[str, Union[str, Sequence[str]]]

Loads an object from a string.

Parameters
  • s – An object to parse

  • keep_originals – If True, keep the original indexed lines as well.

  • separator – The separator between key and value.

  • index_separator – The separator between key and index.