WorktimeCalculator

class WorktimeCalculator(user_config_path='.user_config.ini')[source]

Class to calculate the worktime.

Parameters

user_config_path (str, optional) – Path to the user specific config, which will overwrite default settings. by default “.user_config.ini”

Attributes Summary

default_config_path

Methods Summary

add_time_columns

Add Year, Month, Week and Day columns to an existing Dataframe.

calc_file_hashes

Calculate hashvalues for files.

clean_db

Remove rows where the session work was less than 1min.

generate_contract_worktime_df

Generate a Dataframe with 'worktime' column.

get_daily_worktime

Calculate the mean daily work time based on the contract details.

get_datetime_now

Helpermethod for mocking of datetime.datetime.now() in unittests.

get_holiday_df

Generate a Dataframe containing all needed holidays.

get_manual_df_with_workime

Read the manual_db file.

get_pandas_now

Return datetime.now as pd.Timestamp.

get_plot_df

Return a Dataframe prepared for plotting.

get_remote_db

Download the db_file to db_path_online from the SFTP server.

get_total_df

Calculate total Dataframe.

init_holidays

Initialize the holidays with the custom holidays from the config.

load_config

Load the config files and sets all necessary properties.

load_db

Load Database remote or locally.

merge_dbs

Merge local db with remote db.

push_remote_db

Push the db_file from db_path_offline to the SFTP server.

split_date_overlap_session

Split sessions which contain midnight to to sessions.

Methods Documentation

classmethod add_time_columns(df: DataFrame, date_time_column_name: str = 'start') DataFrame[source]

Add Year, Month, Week and Day columns to an existing Dataframe.

Parameters
  • df (pd.DataFrame) – Dataframe the columns should be added to.

  • date_time_column (str) – Name of the column containing the used date

Returns

Dataframe with added Year, Month, Week and Day columns.

Return type

pd.DataFrame

calc_file_hashes() DataFrame

Calculate hashvalues for files.

Returns

Dataframe with file hashes.

Return type

pd.DataFrame

clean_db() None

Remove rows where the session work was less than 1min.

generate_contract_worktime_df() DataFrame[source]

Generate a Dataframe with ‘worktime’ column.

Generate a template like DataFrame containing the mean daily work time for each day counting as workday based on the contract details. This Template will be used later on to generate the manual_df and holiday_df.

Returns

contract_worktime_df – Dataframe containing all workdays and their mean daily working time, since the 1st contract started until now

Return type

pandas.DataFrame

get_daily_worktime(frequenzy: str, worktime: Union[int, float], weekmask: str) float[source]

Calculate the mean daily work time based on the contract details.

Parameters
  • frequenzy (str) – Timeperiod in which the the user is supposed to to work worktime*1h Supported values are: monthly, weekly

  • worktime (int, float) – Time in hours that the user is supposed to work in frequenzy

  • weekmask (str) – Listing of the abbreviated weekdays the user is supposed to work i.e.: “Mon Tue Wed Thu Fri Sat”

Returns

daily_worktime

Return type

float

get_datetime_now() datetime

Helpermethod for mocking of datetime.datetime.now() in unittests.

Returns

datetime.now()

Return type

datetime.datetime

get_holiday_df() DataFrame[source]

Generate a Dataframe containing all needed holidays.

They begin at the start of the first contract until now, as well as work time based on the contract parameters for those days.

Returns

holiday_df – Dataframe containing all holidays from the start of the first contract until now, as well as work time based on the contract parameters for those days.

Return type

pandas.DataFrame

get_manual_df_with_workime() DataFrame[source]

Read the manual_db file.

That file contains information like sick time | vacation, specified by manual_db in the config. Then expands the given time periods to date_ranges on a daily base, drops the none workdays and adds the daily work time based on the contract parameters for that day.

Returns

Dataframe containing the to a daily base expanded entry’s of manual_df.

Return type

pandas.DataFrame

get_pandas_now() Timestamp

Return datetime.now as pd.Timestamp.

Returns

current time as timestamp: pd.Timestamp

Return type

pd.Timestamp

get_plot_df(rule='D', date_time_column='start') DataFrame[source]

Return a Dataframe prepared for plotting.

Dataframe with a DateTimeIndex, columns named by occupation and containing the worked time of that occupation for the given samplingrate

Parameters

rule (str) – Resampling rule see pandas.DataFrame.resample

Returns

Dataframe with a DateDimeIndex, columns named by occupation and containing the worktime of that occupation for the samplingrate

Return type

pandas.DataFrame

get_remote_db() bool

Download the db_file to db_path_online from the SFTP server.

This uses the values specified at [“login”][“db_path”] in the config file.

Returns

Whether database retrieval succeeded or not.

Return type

bool

get_total_df() DataFrame[source]

Calculate total Dataframe.

Returns

Dataframe with ‘worktime’ and time columns

Return type

pd.DataFrame

See also

add_time_columns

init_holidays() Union[HolidayBase, Dict][source]

Initialize the holidays with the custom holidays from the config.

Returns the class instance from holiday, which matches the country and province given in the config and updates it with the special holidays, also given in the config

Returns

HolidayBase object for the given country and province or an empty dict.

Return type

Union[HolidayBase, Dict]

load_config()[source]

Load the config files and sets all necessary properties.

load_db() DataFrame[source]

Load Database remote or locally.

Tries to load the database directly from the server if possible, else it loads the local database or throws an exception that isn’t possible either.

Returns

db – Database with the actually worked time

Return type

pandas.DataFrame

merge_dbs() DataFrame

Merge local db with remote db.

The overlap (same start) is replaced with the max value of end.

Returns

Local db merged with remote db, with striped overlap.

Return type

pd.Dataframe

push_remote_db() bool

Push the db_file from db_path_offline to the SFTP server.

This uses the values specified at [“login”][“db_path”] in the config file.

Returns

Whether database upload succeeded or not.

Return type

bool

split_date_overlap_session() None[source]

Split sessions which contain midnight to to sessions.

The first lasting until midnight and the second starting at midnight.

df before:
start   end
1.1.1970 21:00:00   2.1.1970 02:00:00

df after:
start   end
1.1.1970 21:00:00   2.1.1970 00:00:00
2.1.1970 00:00:00   2.1.1970 02:00:00