Casts and other utils#

Casting is essential in programming. With Pydicts I try to put together main castings, to work easyly with all kind of data structures.

ignore_exception#

Morever, this module casting function has two additional parameters, that are useful for rapid development:

  • ignore_exception. By default False. If you set this parameter to True, casting with return ignore_exception_value if a CastException happens.

  • ignore_exception_value. By default None. It’s the value casting will return if a CastException happens

For example, this code casts.str2bool(None) will raise a CastException, due to None is not a valid value.

But this code will return None

from pydicts import casts
casts.str2bool(None, ignore_exception=True)

And this code will return False

from pydicts import casts
casts.str2bool(None, ignore_exception=True,ignore_exception_value=False)
False

Casts#

base64bytes2bytes#

Help on function base64bytes2bytes in module pydicts.casts:

base64bytes2bytes(value, ignore_exception=False, ignore_exception_value=None)
    Bytes 2 string

bytes2base64bytes#

Help on function bytes2base64bytes in module pydicts.casts:

bytes2base64bytes(value, ignore_exception=False, ignore_exception_value=None)
    String 2 bytes

bytes2str#

Help on function bytes2str in module pydicts.casts:

bytes2str(value, code='UTF-8', ignore_exception=False, ignore_exception_value=None)
    Bytes 2 string

dtaware2dtnaive#

Help on function dtaware2dtnaive in module pydicts.casts:

dtaware2dtnaive(dtaware)

dtaware2epochmicros#

Help on function dtaware2epochmicros in module pydicts.casts:

dtaware2epochmicros(d)
    ## epoch is the time from 1,1,1970 in UTC
    ## return now(timezone(self.name))

dtaware2epochms#

Help on function dtaware2epochms in module pydicts.casts:

dtaware2epochms(d)
    ## epoch is the time from 1,1,1970 in UTC
    ## return now(timezone(self.name))

dtaware2str#

Help on function dtaware2str in module pydicts.casts:

dtaware2str(value, format='JsUtcIso', ignore_exception=False, ignore_exception_value=None)
    ## Returns a formated string of a dtaware string formatting with a zone name
    ## @param dt datetime aware object
    ## @return String

dtnaive2dtaware#

Help on function dtnaive2dtaware in module pydicts.casts:

dtnaive2dtaware(dtnaive, tz_name)

dtnaive2str#

Help on function dtnaive2str in module pydicts.casts:

dtnaive2str(value, format='JsIso', ignore_exception=False, ignore_exception_value=None)
    ## Returns a formated string of a dtaware string formatting with a zone name
    ## @param dt datetime aware object
    ## @param format String in ["%Y-%m-%d", "%Y-%m-%d %H:%M:%S", "%Y%m%d %H%M", "%Y%m%d%H%M"]
    ## @return String

epochmicros2dtaware#

Help on function epochmicros2dtaware in module pydicts.casts:

epochmicros2dtaware(n, tz='UTC')
    ## Return a UTC datetime aware

epochms2dtaware#

Help on function epochms2dtaware in module pydicts.casts:

epochms2dtaware(n, tz='UTC')
    ## Return a UTC datetime aware

none2alternative#

Help on function none2alternative in module pydicts.casts:

none2alternative(value, alternative)
    If a value is None, returns an alternative

str2bool#

Help on function str2bool in module pydicts.casts:

str2bool(value, ignore_exception=False, ignore_exception_value=None)
    Converts strings True or False to boolean
    @param value String
    @return Boolean
from pydicts import casts
casts.str2bool("true")
True
from pydicts import casts
casts.str2bool("0")
False

These calls will raise CastException:

  • casts.str2bool(None)

  • casts.str2bool(True)

  • casts.str2bool("Verdadero")

str2bytes#

Help on function str2bytes in module pydicts.casts:

str2bytes(value, code='UTF8', ignore_exception=False, ignore_exception_value=None)
    String 2 bytes

str2date#

Help on function str2date in module pydicts.casts:

str2date(value, format='YYYY-MM-DD', ignore_exception=False, ignore_exception_value=None)

str2decimal#

Help on function str2decimal in module pydicts.casts:

str2decimal(value, ignore_exception=False, ignore_exception_value=None)
    Converts a string  to a decimal
    Parameters:
        - decimal_separator. Symbol to separate decimals. For example 12.121212 (decimal_separator=".") 12.122,1223 (decimal_separator=",")

str2dtaware#

Help on function str2dtaware in module pydicts.casts:

str2dtaware(value, format='JsUtcIso', tz_name='UTC', ignore_exception=False, ignore_exception_value=None)

str2dtnaive#

Help on function str2dtnaive in module pydicts.casts:

str2dtnaive(value, format='JsIso', ignore_exception=False, ignore_exception_value=None)

str2time#

Help on function str2time in module pydicts.casts:

str2time(value, format='JsIso', ignore_exception=False, ignore_exception_value=None)

str2timedelta#

Help on function str2timedelta in module pydicts.casts:

str2timedelta(value, ignore_exception=False, ignore_exception_value=None)
    Converts a string in ISO_8601 in a timedelta or duration

time2str#

Help on function time2str in module pydicts.casts:

time2str(value, format='JsIso', ignore_exception=False, ignore_exception_value=None)
    ## Converts a time to a string

timedelta2str#

Help on function timedelta2str in module pydicts.casts:

timedelta2str(value, ignore_exception=False, ignore_exception_value=None)
    Converts a timedelta object into a string in ISO_8601

Date and time utils#

date_first_of_the_month#

Help on function date_first_of_the_month in module pydicts.casts:

date_first_of_the_month(year, month)
    ## Returns a date with the first date of the month
    ## @param year Year to search fist day
    ## @param month Month to search first day

date_first_of_the_next_x_months#

Help on function date_first_of_the_next_x_months in module pydicts.casts:

date_first_of_the_next_x_months(year, month, x)
    ## Returns a date with the first date of the month after x months
    ## @param year Year to search  day
    ## @param month Month to search day
    ## @param x Number of months after parameters. Cab be positive to add months or negative to substract months

date_first_of_the_year#

Help on function date_first_of_the_year in module pydicts.casts:

date_first_of_the_year(year)
    ## Returns a date with the first date of the year
    ## @param year Year to search first day

date_last_of_the_month#

Help on function date_last_of_the_month in module pydicts.casts:

date_last_of_the_month(year, month)
    ## Returns a date with the last date of the month
    ## @param year Year to search last day
    ## @param month Month to search last day

date_last_of_the_next_x_months#

Help on function date_last_of_the_next_x_months in module pydicts.casts:

date_last_of_the_next_x_months(year, month, x)
    ## Returns a date with the last date of the month after x months
    ## @param year Year to search  day
    ## @param month Month to search day
    ## @param x Number of months after parameters. Cab be positive to add months or negative to substract months

date_last_of_the_year#

Help on function date_last_of_the_year in module pydicts.casts:

date_last_of_the_year(year)
    ## Returns a date with the last date of the year
    ## @param year Year to search last day

dtaware#

Help on function dtaware in module pydicts.casts:

dtaware(date_, time_, tz_name)
    ## Function to create a datetime aware object
    ## @param date datetime.date object
    ## @param hour hour object
    ## @param tz_name String with datetime tz_name name. For example "Europe/Madrid"
    ## @return datetime aware

dtaware_changes_tz#

Help on function dtaware_changes_tz in module pydicts.casts:

dtaware_changes_tz(dt, tzname)
    ## Changes zoneinfo from a dtaware object
    ## For example:
    ## - datetime.datetime(2018, 5, 18, 8, 12, tzinfo=<DstTzInfo 'Europe/Madrid' CEST+2:00:00 DST>)
    ## - libcaloriestrackerfunctions.dtaware_changes_tz(a,"Europe/London")
    ## - datetime.datetime(2018, 5, 18, 7, 12, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)
    ## @param dt datetime aware object
    ## @param tzname String with datetime zone. For example: "Europe/Madrid"
    ## @return datetime aware object

dtaware_day_end#

Help on function dtaware_day_end in module pydicts.casts:

dtaware_day_end(dt, tz_name)
    Returns the last  datetime (microsecond  level) of the  day in tz_name zone

dtaware_day_end_from_date#

Help on function dtaware_day_end_from_date in module pydicts.casts:

dtaware_day_end_from_date(date, tz_name)
    ## Returns the end of the day dtaware in utc format

dtaware_day_start#

Help on function dtaware_day_start in module pydicts.casts:

dtaware_day_start(dt, tz_name)
    ## Returns a dtnaive or dtawre (as parameter) with the end of the day in zone tz_name

dtaware_day_start_from_date#

Help on function dtaware_day_start_from_date in module pydicts.casts:

dtaware_day_start_from_date(date, tz_name)
    ## Returns the end of the day dtaware of the tz_name timezone from a date

dtaware_month_end#

Help on function dtaware_month_end in module pydicts.casts:

dtaware_month_end(year, month, tz_name)

dtaware_month_start#

Help on function dtaware_month_start in module pydicts.casts:

dtaware_month_start(year, month, tz_name)
    ## Returns the start of a month in utc format

dtaware_now#

Help on function dtaware_now in module pydicts.casts:

dtaware_now(tzname=None)
    If tzname is None: returns UTC dtaware

Returns a aware datetime object of current moment. By default returns UTC timezone

from pydicts import casts
print(casts.dtaware_now())
print(casts.dtaware_now("Europe/Madrid"))
2024-07-10 05:47:40.085243+00:00
2024-07-10 07:47:40.086027+02:00

dtaware_year_end#

Help on function dtaware_year_end in module pydicts.casts:

dtaware_year_end(year, tz_name)
    ## Returns an aware datetime with the last of year

dtaware_year_start#

Help on function dtaware_year_start in module pydicts.casts:

dtaware_year_start(year, tz_name)
    ## Returns an aware datetime with the start of year

dtnaive#

Help on function dtnaive in module pydicts.casts:

dtnaive(date_, hour)
    ## Function to create a datetime aware object
    ## @param date datetime.date object
    ## @param hour hour object
    ## @return datetime naive

dtnaive_day_end#

Help on function dtnaive_day_end in module pydicts.casts:

dtnaive_day_end(dt)
    Returns the last  datetime (microsecond  level) of the  day in naive format

dtnaive_day_end_from_date#

Help on function dtnaive_day_end_from_date in module pydicts.casts:

dtnaive_day_end_from_date(date_)
    ## Returns the end of the day dtnaive from a date

dtnaive_day_start#

Help on function dtnaive_day_start in module pydicts.casts:

dtnaive_day_start(dt)
    ## Returns a dtnaive or dtawre (as parameter) with the end of the day

dtnaive_day_start_from_date#

Help on function dtnaive_day_start_from_date in module pydicts.casts:

dtnaive_day_start_from_date(date_)
    ## Returns the end of the day dtnaive from a date

dtnaive_now#

Help on function dtnaive_now in module pydicts.casts:

dtnaive_now()

Returns a naive datetime object of current moment. By default returns UTC timezone

from pydicts import casts
casts.dtnaive_now()
datetime.datetime(2024, 7, 10, 5, 47, 40, 133739)

is_aware#

Help on function is_aware in module pydicts.casts:

is_aware(dt)
    ### Returns if a datetime is aware

Returns if a datetime object is aware (with timezone)

from pydicts import casts
print(casts.is_aware(casts.dtaware_now()))
print(casts.is_aware(casts.dtnaive_now()))
True
False

is_naive#

Help on function is_naive in module pydicts.casts:

is_naive(dt)
    ## Returns if a datetime is naive
from pydicts import casts
print(casts.is_naive(casts.dtaware_now()))
print(casts.is_naive(casts.dtnaive_now()))
False
True

months#

Help on function months in module pydicts.casts:

months(year_from, month_from, year_to=None, month_to=None)
    ## Returns a list of tuples (year, month) from a month to another month, both included
    ## @param year_from Integer
    ## @param month_from Integer
    ## @param year_to Integer If none uses current year
    ## @param month_to Integer If none uses current month

Other utils#

is_email#

Help on function is_email in module pydicts.casts:

is_email(value)
    Returns if a string is a valid email using a regular expression
from pydicts import casts
print(casts.is_email("hi.hi.com"))
print(casts.is_email("hi@hi.com"))
print(casts.is_email("hi@hi.com."))
False
True
False

is_noe#

Help on function is_noe in module pydicts.casts:

is_noe(value)
    Returns a boolean. True if value is None or an empty string.
from pydicts import casts
print(casts.is_noe(None))
print(casts.is_noe(""))
print(casts.is_noe(1))
True
True
False

object_or_empty#

Help on function object_or_empty in module pydicts.casts:

object_or_empty(v)
    Returns and empty string if None, else return value