privex.helpers.converters

Various functions/classes which convert/parse objects from one type into another.

Copyright:

    +===================================================+
    |                 © 2019 Privex Inc.                |
    |               https://www.privex.io               |
    +===================================================+
    |                                                   |
    |        Originally Developed by Privex Inc.        |
    |        License: X11 / MIT                         |
    |                                                   |
    |        Core Developer(s):                         |
    |                                                   |
    |          (+)  Chris (@someguy123) [Privex]        |
    |          (+)  Kale (@kryogenic) [Privex]          |
    |                                                   |
    +===================================================+

Copyright 2019     Privex Inc.   ( https://www.privex.io )

Functions

convert_bool_int(d[, if_empty, fail_empty])

Convert a boolean d into an integer (0 for False, 1 for True)

convert_datetime(d[, if_empty, fail_empty])

Convert the object d into a datetime.datetime object.

convert_int_bool(d[, if_empty, fail_empty])

Convert an integer d into a boolean (0 for False, 1 for True)

convert_unixtime_datetime(d[, if_empty, …])

Convert a unix timestamp into a datetime.datetime object

privex.helpers.converters.CLEAN_OBJ_FALLBACK(ob, **kwargs)
privex.helpers.converters.clean_dict(data: dict, **kwargs)dict[source]
privex.helpers.converters.clean_list(ld: list, **kwargs)list[source]
privex.helpers.converters.clean_obj(ob: Any, number_str: bool = False, fail=False, fallback: T = None) → Union[list, dict, str, float, int, T][source]

Cleans an object by converting it / it’s contents into basic, simple, JSON-compatible types.

For example, Decimal’s will become float’s (or str’s if number_str=True), bytes will be decoded into a str if possible, :param Any ob: An object to clean - making it safe for use with JSON/YAML etc. :param bool number_str: (Default: False) When set to True, numbers will be converted to strings instead of int/float. :param bool fail: (Default: False) When set to True, will raise the exception thrown by the fallback converter

if an error occurs, instead of returning fallback

Parameters

fallback (Any) – (Default: None) The value to return if all matchers/converters fail to handle the object, only used when fail=False (the default)

Return SIMPLE_TYPES|T res

A clean version of the object for serialisation - or fallback if something went wrong.

privex.helpers.converters.convert_bool_int(d, if_empty=0, fail_empty=False)int[source]

Convert a boolean d into an integer (0 for False, 1 for True)

privex.helpers.converters.convert_datetime(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime][source]

Convert the object d into a datetime.datetime object.

If d is a string or bytes, then it will be parsed using dateutil.parser.parse()

If d is an int/float/Decimal, then it will be assumed to be a unix epoch timestamp.

Examples:

>>> convert_datetime("2019-01-01T00:00:00Z")          # ISO date/time
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime("01/JAN/2019 00:00:00.0000")     # Human date/time with month name
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime(1546300800)                      # Unix timestamp as integer
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime(1546300800000)                   # Unix timestamp (milliseconds) as integer
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())
Parameters
  • d – Object to convert into a datetime

  • if_empty – If d is empty / None, return this value

  • fail_empty (bool) – (Def: False) If this is True, then if d is empty, raises AttributeError

Key datetime.tzinfo tzinfo

(Default: dateutil.tz.tzutc) If no timezone was detected by the parser, use this timezone. Set this to None to disable forcing timezone-aware dates.

Raises
Return datetime converted

The converted datetime.datetime object.

privex.helpers.converters.convert_epoch_datetime(d: Union[str, int, float, decimal.Decimal], if_empty=None, fail_empty=False)datetime.datetime

Convert a unix timestamp into a datetime.datetime object

privex.helpers.converters.convert_int_bool(d, if_empty=False, fail_empty=False)bool[source]

Convert an integer d into a boolean (0 for False, 1 for True)

privex.helpers.converters.convert_unixtime_datetime(d: Union[str, int, float, decimal.Decimal], if_empty=None, fail_empty=False)datetime.datetime[source]

Convert a unix timestamp into a datetime.datetime object

privex.helpers.converters.parse_date(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime]

Convert the object d into a datetime.datetime object.

If d is a string or bytes, then it will be parsed using dateutil.parser.parse()

If d is an int/float/Decimal, then it will be assumed to be a unix epoch timestamp.

Examples:

>>> convert_datetime("2019-01-01T00:00:00Z")          # ISO date/time
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime("01/JAN/2019 00:00:00.0000")     # Human date/time with month name
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime(1546300800)                      # Unix timestamp as integer
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime(1546300800000)                   # Unix timestamp (milliseconds) as integer
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())
Parameters
  • d – Object to convert into a datetime

  • if_empty – If d is empty / None, return this value

  • fail_empty (bool) – (Def: False) If this is True, then if d is empty, raises AttributeError

Key datetime.tzinfo tzinfo

(Default: dateutil.tz.tzutc) If no timezone was detected by the parser, use this timezone. Set this to None to disable forcing timezone-aware dates.

Raises
Return datetime converted

The converted datetime.datetime object.

privex.helpers.converters.parse_datetime(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime]

Convert the object d into a datetime.datetime object.

If d is a string or bytes, then it will be parsed using dateutil.parser.parse()

If d is an int/float/Decimal, then it will be assumed to be a unix epoch timestamp.

Examples:

>>> convert_datetime("2019-01-01T00:00:00Z")          # ISO date/time
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime("01/JAN/2019 00:00:00.0000")     # Human date/time with month name
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime(1546300800)                      # Unix timestamp as integer
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())

>>> convert_datetime(1546300800000)                   # Unix timestamp (milliseconds) as integer
datetime.datetime(2019, 1, 1, 0, 0, tzinfo=tzutc())
Parameters
  • d – Object to convert into a datetime

  • if_empty – If d is empty / None, return this value

  • fail_empty (bool) – (Def: False) If this is True, then if d is empty, raises AttributeError

Key datetime.tzinfo tzinfo

(Default: dateutil.tz.tzutc) If no timezone was detected by the parser, use this timezone. Set this to None to disable forcing timezone-aware dates.

Raises
Return datetime converted

The converted datetime.datetime object.

privex.helpers.converters.parse_epoch(d: Union[str, int, float, decimal.Decimal], if_empty=None, fail_empty=False)datetime.datetime

Convert a unix timestamp into a datetime.datetime object

privex.helpers.converters.parse_unixtime(d: Union[str, int, float, decimal.Decimal], if_empty=None, fail_empty=False)datetime.datetime

Convert a unix timestamp into a datetime.datetime object