lib package

utils

Some utilities functions and classes that are used in the module.

class weatherDB.lib.utils.TimestampPeriod(start, end, tzinfo='UTC')[source]

Bases: object

A class to save a Timespan with a minimal and maximal Timestamp.

Initiate a TimestampPeriod.

Parameters:
  • start (pd.Timestamp or similar) – The start of the Period.

  • end (pd.Timestamp or similar) – The end of the Period.

  • tzinfo (str or datetime.timezone object or None, optional) – The timezone to set to the timestamps. If the timestamps already have a timezone they will get converted. If None, then the timezone is not changed or set. The default is “UTC”.

contains(other)[source]

Does this TimestampPeriod contain another TimestampPeriod?

Parameters:

other (Timestampperiod or tuple of 2 Timestamp or Timestamp strings) – The other Timestamp to test against. Test if this TimestampPeriod contains the other.

Returns:

True if this TimestampPeriod contains the other. Meaning that the start is smaller or equal than the others starts and the end is higher than the others end.

Return type:

bool

copy()[source]

Copy this TimestampPeriod.

Returns:

a new TimestampPeriod object that is equal to this one.

Return type:

TimestampPeriod

expand_to_timestamp()[source]
get_interval()[source]

Get the interval of the TimestampPeriod.

Returns:

The interval of this TimestampPeriod. E.G. Timedelta(2 days 12:30:12)

Return type:

pd.Timedelta

get_middle()[source]

Get the middle Timestamp of the TimestampPeriod.

Returns:

The middle Timestamp of this TimestampPeriod.

Return type:

Timestamp

get_period()[source]
get_sql_format_dict(format="'%Y%m%d %H:%M'")[source]

Get the dictionary to use in sql queries.

Parameters:

format (str, optional) – The Timestamp-format to use. The Default is “’%Y%m%d %H:%M’”

Returns:

a dictionary with 2 keys (min_tstp, max_tstp) and the corresponding Timestamp as formated string.

Return type:

dict

has_NaT()[source]

Has the TimestampPeriod at least one NaT.

This means that the start or end is not given. Normally this should never happen, because it makes no sense.

Returns:

True if the TimestampPeriod has at least on NaT. False if the TimestampPeriod has at least a start or a end.

Return type:

bool

has_only_NaT()[source]

Has the TimestampPeriod only NaT, meaning is empty.

This means that the start and end is not given.

Returns:

True if the TimestampPeriod is empty. False if the TimestampPeriod has a start and an end.

Return type:

bool

inside(other)[source]

Is the TimestampPeriod inside another TimestampPeriod?

Parameters:

other (Timestampperiod or tuple of 2 Timestamp or Timestamp strings) – The other Timestamp to test against. Test if this TimestampPeriod is inside the other.

Returns:

True if this TimestampPeriod is inside the other. Meaning that the start is higher or equal than the others starts and the end is smaller than the others end.

Return type:

bool

is_empty()[source]

Is the TimestampPeriod empty.

This means that the start and end is not given.

Returns:

True if the TimestampPeriod is empty. False if the TimestampPeriod has a start and an end.

Return type:

bool

strftime(format='%Y-%m-%d %H:%M:%S')[source]

Convert the TimestampPeriod to a list of strings.

Formates the Timestamp as a string.

Parameters:

format (str, optional) – The Timestamp-format to use. The Default is “%Y-%m-%d %H:%M:%S”

Returns:

A list of the start and end of the TimestampPeriod as formated string.

Return type:

list of 2 strings

union(other, how='inner')[source]

Unite 2 TimestampPeriods to one.

Compares the Periods and computes a new one.

Parameters:
  • other (TimestampPeriod) – The other TimestampPeriod with whome to compare.

  • how (str, optional) – How to compare the 2 TimestampPeriods. Can be “inner” or “outer”. “inner”: the maximal Timespan for both is computed. “outer”: The minimal Timespan for both is computed. The default is “inner”.

Returns:

A new TimespanPeriod object uniting both TimestampPeriods.

Return type:

TimestampPeriod

weatherDB.lib.utils.get_cdc_file_list(ftp_folders)[source]
weatherDB.lib.utils.get_ftp_file_list(ftp_conn, ftp_folders)[source]

Get a list of files in the folders with their modification dates.

Parameters:
  • ftp_conn (ftplib.FTP) – Ftp connection.

  • ftp_folders (list of str or pathlike object) – The directories on the ftp server to look for files.

Returns:

A list of Tuples. Every tuple stands for one file. The tuple consists of (filepath, modification date).

Return type:

list of tuples of strs

max_fun