flib.base.literal module

Data type dependent string representation of objects.

as_datetime(text: str, fmt: Optional[str] = None) → datetime.datetime

Convert text to datetime.

Parameters:
  • text – String representation of datetime
  • fmt – Optional string parameter, that specifies the format, which is used to decode the text to datetime. The default format is the [ISO 8601]_ format, given by the string %Y-%m-%d %H:%M:%S.%f.
Returns:

Value of the text as datetime.

as_dict(text: str, delim: str = ', ') → dict

Convert text into dictionary.

Parameters:
  • text

    String representing a dictionary. Valid representations are: Python format: Allows keys and values of arbitrary types:

    Example: “{‘a’: 2, 1: True}”
    Delimiter separated expressions: Allow string keys and values:
    Example (Variant A): “<key> = <value><delim> …” Example (Variant B): “’<key>’: <value><delim> …”
  • delim – A string, which is used as delimiter for the separatation of the text. This parameter is only used in the DSV format.
Returns:

Value of the text as dictionary.

as_list(text: str, delim: str = ', ') → list

Convert text into list.

Parameters:
  • text

    String representing a list. Valid representations are: Python format: Allows elements of arbitrary types:

    Example: “[‘a’, ‘b’, 3]”
    Delimiter separated values (DSV): Allows string elements:
    Example: “a, b, c”
  • delim – A string, which is used as delimiter for the separatation of the text. This parameter is only used in the DSV format.
Returns:

Value of the text as list.

as_path(text: str, expand: bool = True) → pathlib.Path

Convert text into list.

Parameters:
  • text – String representing a path.
  • expand – Boolen value, whoch determines, if variables in environmental path variables are expanded.
Returns:

Value of the text as Path.

as_set(text: str, delim: str = ', ') → set

Convert text into set.

Parameters:
  • text

    String representing a set. Valid representations are: Python format: Allows elements of arbitrary types:

    Example: “{‘a’, ‘b’, 3}”
    Delimiter separated values (DSV): Allows string elements:
    Example: “a, b, c”
  • delim – A string, which is used as delimiter for the separatation of the text. This parameter is only used in the DSV format.
Returns:

Value of the text as set.

as_tuple(text: str, delim: str = ', ') → tuple

Convert text into tuple.

Parameters:
  • text

    String representing a tuple. Valid representations are: Python format: Allows elements of arbitrary types:

    Example: “(‘a’, ‘b’, 3)”
    Delimiter separated values (DSV): Allows string elements:
    Example: “a, b, c”
  • delim – A string, which is used as delimiter for the separatation of the text. This parameter is only used in the DSV format.
Returns:

Value of the text as tuple.

decode(text: str, target: Optional[type] = None, undef: Optional[str] = 'None', **kwds) → Any

Decode text representation of object to object.

Parameters:
  • text – String representing the value of a given type in it’s respective syntax format. The standard format corresponds to the standard Python representation if available. Some types also accept further formats, which may use additional keywords.
  • target – Target type, in which the text is to be converted.
  • undef – Optional string, which respresents an undefined value. If undef is a string, then the any text, the matches the string is decoded as None, independent from the given target type.
  • **kwds – Supplementary parameters, that specify the encoding format of the target type.
Returns:

Value of the text in given target format or None.

encode(obj: object, **kwds) → str

Encode object to literal text representation.

Parameters:obj – Simple object
Returns:Literal text representation of given object.
estimate(text: str) → Optional[type]

Estimate type of text by using literal_eval().

Parameters:text – String representation of python object.
Returns:Type of text or None, if the type could not be determined.
from_str(text: str, charset: Optional[str] = None, spacer: Optional[str] = None) → str

Filter text to given character set.

Parameters:
  • text
  • charset

    Name of used character set. Supportet options are:

    printable:Printable characters
    UAX-31:ASCII identifier characters as defined in [UAX31]
Returns:

String, which is filtered to the chiven character set.