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
),bytes
will be decoded into astr
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 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
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
forFalse
,1
forTrue
)
-
privex.helpers.converters.
convert_datetime
(d, if_empty=None, fail_empty=False, **kwargs) → Optional[datetime.datetime][source]¶ Convert the object
d
into adatetime.datetime
object.If
d
is a string or bytes, then it will be parsed usingdateutil.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 valuefail_empty (bool) – (Def:
False
) If this is True, then ifd
is empty, raisesAttributeError
- Key datetime.tzinfo tzinfo
(Default:
dateutil.tz.tzutc
) If no timezone was detected by the parser, use this timezone. Set this toNone
to disable forcing timezone-aware dates.- Raises
AttributeError – When
d
is empty andfail_empty
is set to True.dateutil.parser.ParserError – When
d
could not be parsed into a date.
- 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
forFalse
,1
forTrue
)
-
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 adatetime.datetime
object.If
d
is a string or bytes, then it will be parsed usingdateutil.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 valuefail_empty (bool) – (Def:
False
) If this is True, then ifd
is empty, raisesAttributeError
- Key datetime.tzinfo tzinfo
(Default:
dateutil.tz.tzutc
) If no timezone was detected by the parser, use this timezone. Set this toNone
to disable forcing timezone-aware dates.- Raises
AttributeError – When
d
is empty andfail_empty
is set to True.dateutil.parser.ParserError – When
d
could not be parsed into a date.
- 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 adatetime.datetime
object.If
d
is a string or bytes, then it will be parsed usingdateutil.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 valuefail_empty (bool) – (Def:
False
) If this is True, then ifd
is empty, raisesAttributeError
- Key datetime.tzinfo tzinfo
(Default:
dateutil.tz.tzutc
) If no timezone was detected by the parser, use this timezone. Set this toNone
to disable forcing timezone-aware dates.- Raises
AttributeError – When
d
is empty andfail_empty
is set to True.dateutil.parser.ParserError – When
d
could not be parsed into a date.
- 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