babilonia.tools.core#
Core constants and functions for the babilonia.tools package.
Functions
|
Build a subtotal row and an average row for a monthly table. |
|
Reads multiple CSV files and concatenates them into a single DataFrame. |
|
Extracts the account identifier from a formatted data type string. |
Parses command-line arguments for the Babilonia utilities. |
|
|
Extracts the bank name from a formatted data type string. |
|
|
|
Constructs a glob-style file path pattern for bank statement CSV files. |
|
Displays a preview of a DataFrame with a specified maximum number of rows. |
|
Temporarily duplicate everything printed to |
Classes
|
File-like object that duplicates writes across several streams. |
- class babilonia.tools.core.TeeWriter(*streams)[source]#
Bases:
objectFile-like object that duplicates writes across several streams.
- Parameters:
streams (TextIO) – Writable file-like objects to fan writes out to.
- babilonia.tools.core.tee_stdout(file_handle)[source]#
Temporarily duplicate everything printed to
stdoutintofile_handle.Used to produce a
.txtreport 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 displayedrow_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 tostrtype 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_columnsare summed (subtotal) and divided byn_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_columnon the subtotal row.average_label (str) – Label written to
label_columnon the average row.n_periods (int) – Divisor used for the average row.
- Returns:
Two-row DataFrame, ready to
pandas.concatonto a display-formatted version ofdf.- 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
yearis 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_arguments()[source]#
Parses command-line arguments for the Babilonia utilities.
- Returns:
An object containing the parsed arguments:
folder,type, andyear- 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 toNone).