babilonia.tools.core#

Core constants and functions for the babilonia.tools package.

Functions

build_subtotal_rows(df, columns, ...[, ...])

Build a subtotal row and an average row for a monthly table.

concat_dfs(ls_files)

Reads multiple CSV files and concatenates them into a single DataFrame.

get_account(data_type)

Extracts the account identifier from a formatted data type string.

get_arguments()

Parses command-line arguments for the Babilonia utilities.

get_bank(data_type)

Extracts the bank name from a formatted data type string.

get_file_pattern_cashflow_daily(data_type, ...)

get_file_pattern_statement_t0(data_type, folder)

Constructs a glob-style file path pattern for bank statement CSV files.

preview_df(df[, row_max])

Displays a preview of a DataFrame with a specified maximum number of rows.

tee_stdout(file_handle)

Temporarily duplicate everything printed to stdout into file_handle.

Classes

TeeWriter(*streams)

File-like object that duplicates writes across several streams.

class babilonia.tools.core.TeeWriter(*streams)[source]#

Bases: object

File-like object that duplicates writes across several streams.

Parameters:

streams (TextIO) – Writable file-like objects to fan writes out to.

write(data)[source]#

Write data to every stream.

Parameters:

data (str) – Text to write.

Returns:

None

Return type:

None

flush()[source]#

Flush every stream.

Returns:

None

Return type:

None

babilonia.tools.core.tee_stdout(file_handle)[source]#

Temporarily duplicate everything printed to stdout into file_handle.

Used to produce a .txt report that is guaranteed to match the terminal output for the same block of code, without reformatting anything a second time.

Parameters:

file_handle (TextIO) – Writable text file object to receive a copy of everything printed while the context is active.

Returns:

None

Return type:

None

babilonia.tools.core.preview_df(df, row_max=20)[source]#

Displays a preview of a DataFrame with a specified maximum number of rows.

Parameters:
  • df (pandas.DataFrame) – The dataset to be displayed

  • row_max (int) – The maximum number of rows to show in the console. Default value = 20

Returns:

None

Return type:

None

babilonia.tools.core.concat_dfs(ls_files)[source]#

Reads multiple CSV files and concatenates them into a single DataFrame.

Parameters:

ls_files (list) – A list of file paths to be read

Returns:

A unified DataFrame containing data from all provided files

Return type:

pandas.DataFrame

Note

This function reads each file using ; as a separator and forces all columns to str type to avoid type inference issues during concatenation. The resulting index is reset and the old index is dropped.

babilonia.tools.core.build_subtotal_rows(df, columns, sum_columns, label_column, formatters=None, subtotal_label='SUBTOTAL', average_label='MEDIA', n_periods=12)[source]#

Build a subtotal row and an average row for a monthly table.

Only sum_columns are summed (subtotal) and divided by n_periods (average); every other column is left blank on both rows. This is the right default for a running/cumulative column (e.g. a balance), where a sum across periods has no real meaning.

Parameters:
  • df (pandas.DataFrame) – Monthly table to summarize, one row per period, with raw (not yet display-formatted) numeric values.

  • columns (list) – Column order for the returned rows – normally df.columns, kept explicit so the caller controls it.

  • sum_columns (list) – Columns to sum and average.

  • label_column (str) – Column that carries the row label.

  • formatters (dict, optional) – Optional mapping of column name to a formatting callable, applied to that column’s subtotal/average value. Columns not listed fall back to str.

  • subtotal_label (str) – Label written to label_column on the subtotal row.

  • average_label (str) – Label written to label_column on the average row.

  • n_periods (int) – Divisor used for the average row.

Returns:

Two-row DataFrame, ready to pandas.concat onto a display-formatted version of df.

Return type:

pandas.DataFrame

babilonia.tools.core.get_bank(data_type)[source]#

Extracts the bank name from a formatted data type string.

Parameters:

data_type (str) – The string containing bank and account info separated by a hyphen

Returns:

The extracted bank name

Return type:

str

babilonia.tools.core.get_account(data_type)[source]#

Extracts the account identifier from a formatted data type string.

Parameters:

data_type (str) – The string containing bank and account info separated by a hyphen

Returns:

The extracted account identifier

Return type:

str

babilonia.tools.core.get_file_pattern_statement_t0(data_type, folder, year=None)[source]#

Constructs a glob-style file path pattern for bank statement CSV files.

Parameters:
  • data_type (str) – The string containing bank and account info separated by a hyphen

  • folder (str) – The base directory path where files are located

  • year (int) – [optional] The specific year to filter files. Default value = None

Returns:

A formatted raw string representing the file search pattern

Return type:

str

Note

If year is not provided, the function uses a wildcard * to match all available year directories. The resulting pattern follows the naming convention: EXTRATO_{BANK}_{ACCOUNT}_*_T0.csv.

babilonia.tools.core.get_file_pattern_cashflow_daily(data_type, folder, year=None)[source]#
babilonia.tools.core.get_arguments()[source]#

Parses command-line arguments for the Babilonia utilities.

Returns:

An object containing the parsed arguments: folder, type, and year

Return type:

argparse.Namespace

The function handles the following arguments: * -f / --folder: Path to the target processing directory. * -t / --type: The specific account type string. * -y / --year: The integer year to filter processing (defaults to None).