breos.utils#

Utility functions for breos library.

Functions

count_leap_years(start_year, num_years)

Count the number of leap years in a range.

get_hours_per_step(freq)

Get the number of hours per timestep based on frequency.

get_steps_per_day(freq)

Get the number of timesteps per day based on frequency.

get_steps_per_year(freq[, leap_year])

Get the number of timesteps per year based on frequency.

is_leap_year(year)

Check if a year is a leap year.

number_of_cores()

Get the number of available CPU cores for parallel processing.

remap_datetime_index_years(obj, year_offset)

Shift a DatetimeIndex-bearing object by whole years without Feb.

safe_path_slug(name)

Validate name as a safe filename component and return it lower-cased.

breos.utils.count_leap_years(start_year, num_years)[source]#

Count the number of leap years in a range.

Parameters:
  • start_year (int) – Starting year

  • num_years (int) – Number of years to count

Return type:

int

Returns:

Number of leap years in the range

breos.utils.get_hours_per_step(freq)[source]#

Get the number of hours per timestep based on frequency.

Parameters:

freq (str) – Frequency string (‘h’ for hourly, ‘15min’ for 15-minute)

Return type:

float

Returns:

Hours per timestep (1.0 for hourly, 0.25 for 15-min)

Raises:

ValueError – If freq is not recognized

breos.utils.get_steps_per_day(freq)[source]#

Get the number of timesteps per day based on frequency.

Parameters:

freq (str) – Frequency string (‘h’ for hourly, ‘15min’ for 15-minute)

Return type:

int

Returns:

Steps per day (24 for hourly, 96 for 15-min)

breos.utils.get_steps_per_year(freq, leap_year=False)[source]#

Get the number of timesteps per year based on frequency.

Parameters:
  • freq (str) – Frequency string (‘h’ for hourly, ‘15min’ for 15-minute)

  • leap_year (bool) – Whether to account for leap year (366 days)

Return type:

int

Returns:

Steps per year (8760/8784 for hourly, 35040/35136 for 15-min)

breos.utils.is_leap_year(year)[source]#

Check if a year is a leap year.

Parameters:

year (int) – Year to check

Return type:

bool

Returns:

True if leap year, False otherwise

breos.utils.number_of_cores()[source]#

Get the number of available CPU cores for parallel processing.

Return type:

int

Returns:

Number of CPU cores (leaves 1 core free for system)

breos.utils.remap_datetime_index_years(obj, year_offset)[source]#

Shift a DatetimeIndex-bearing object by whole years without Feb. 29 crashes.

Feb. 29 entries whose target year is not a leap year are dropped. That avoids both Timestamp.replace failures and duplicate Feb. 28 labels that would later make reindex fail.

Parameters:

year_offset (int)

breos.utils.safe_path_slug(name)[source]#

Validate name as a safe filename component and return it lower-cased.

Input is lower-cased, then validated: allowed characters are ASCII letters, digits, _, -. The result must start with an alphanumeric character and be at most 64 characters. Anything else (path separators, .., NUL bytes, spaces, dots) is rejected so the value cannot be used to escape an intended output directory when interpolated into a filename.

Return type:

str

Parameters:

name (str)