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 a boolean |
|
Convert the object |
|
Convert an integer |
|
Convert a unix timestamp into a |
-
privex.helpers.converters.CLEAN_OBJ_FALLBACK(ob, **kwargs)¶
-
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 becomefloat’s (orstr’s ifnumber_str=True),byteswill be decoded into astrif 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 toTrue, numbers will be converted to strings instead of int/float. :param bool fail: (Default:False) When set toTrue, will raise the exception thrown by the fallback converterif 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 whenfail=False(the default)- Return SIMPLE_TYPES|T res
A clean version of the object for serialisation - or
fallbackif something went wrong.
-
privex.helpers.converters.convert_bool_int(d, if_empty=0, fail_empty=False) → int[source]¶ Convert a boolean
dinto an integer (0forFalse,1forTrue)
-
privex.helpers.converters.convert_datetime(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime][source]¶ Convert the object
dinto adatetime.datetimeobject.If
dis a string or bytes, then it will be parsed usingdateutil.parser.parse()If
dis 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
dis empty / None, return this valuefail_empty (bool) – (Def:
False) If this is True, then ifdis empty, raisesAttributeError
- Key datetime.tzinfo tzinfo
(Default:
dateutil.tz.tzutc) If no timezone was detected by the parser, use this timezone. Set this toNoneto disable forcing timezone-aware dates.- Raises
AttributeError – When
dis empty andfail_emptyis set to True.dateutil.parser.ParserError – When
dcould not be parsed into a date.
- Return datetime converted
The converted
datetime.datetimeobject.
-
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.datetimeobject
-
privex.helpers.converters.convert_int_bool(d, if_empty=False, fail_empty=False) → bool[source]¶ Convert an integer
dinto a boolean (0forFalse,1forTrue)
-
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.datetimeobject
-
privex.helpers.converters.parse_date(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime]¶ Convert the object
dinto adatetime.datetimeobject.If
dis a string or bytes, then it will be parsed usingdateutil.parser.parse()If
dis 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
dis empty / None, return this valuefail_empty (bool) – (Def:
False) If this is True, then ifdis empty, raisesAttributeError
- Key datetime.tzinfo tzinfo
(Default:
dateutil.tz.tzutc) If no timezone was detected by the parser, use this timezone. Set this toNoneto disable forcing timezone-aware dates.- Raises
AttributeError – When
dis empty andfail_emptyis set to True.dateutil.parser.ParserError – When
dcould not be parsed into a date.
- Return datetime converted
The converted
datetime.datetimeobject.
-
privex.helpers.converters.parse_datetime(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime]¶ Convert the object
dinto adatetime.datetimeobject.If
dis a string or bytes, then it will be parsed usingdateutil.parser.parse()If
dis 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
dis empty / None, return this valuefail_empty (bool) – (Def:
False) If this is True, then ifdis empty, raisesAttributeError
- Key datetime.tzinfo tzinfo
(Default:
dateutil.tz.tzutc) If no timezone was detected by the parser, use this timezone. Set this toNoneto disable forcing timezone-aware dates.- Raises
AttributeError – When
dis empty andfail_emptyis set to True.dateutil.parser.ParserError – When
dcould not be parsed into a date.
- Return datetime converted
The converted
datetime.datetimeobject.
-
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.datetimeobject
-
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.datetimeobject