Files Module¶
This module manages file operations: CSV read/write and file selection. All credentials (Binance,
PostgreSQL, …) are managed by panzer (~/.panzer_creds); see Secrets Module.
Functions:
|
Create a folder at the designated path if it doesn't exist. |
|
Save a dataframe in a csv with a separator between columns. |
|
Add lines to a csv with separator to choose, a dataframe. |
|
Locate all files with desired extension. |
|
Move all existing files in a path with desired extension to a folder called "old". |
|
Extract metadata from a filename. |
|
Creates a csv file from a dataframe. |
|
Read a file to a list of strings each line. |
|
Save a new file from a list of lists each line. |
|
Selects from files in the path with the extension passed. |
Get the PostgreSQL password in plain text from panzer (encrypted on disk). |
|
|
Store any credential in panzer's CredentialManager ( |
- create_dir(my_path: str)[source]¶
Create a folder at the designated path if it doesn’t exist. No error if it already exists
- Parameters:
my_path (str) – Path for the new directory.
- save_dataframe_to_csv(filename: str, data: DataFrame, col_sep=',', index=False, timestamp=True) None[source]¶
Save a dataframe in a csv with a separator between columns. Each field of each column will be enclosed in double quotes.
- append_dataframe_to_csv(filename: str, data: DataFrame, col_sep: str = ',', index: bool = False, header=True) None[source]¶
Add lines to a csv with separator to choose, a dataframe. Each field of each column will be enclosed in quotes
- find_csvs_in_path(files_path: str = '.', extension='csv', name_filter: str = None)[source]¶
Locate all files with desired extension.
- move_old_csvs(files_path: str = '.', extension='csv')[source]¶
Move all existing files in a path with desired extension to a folder called “old”.
- extract_filename_metadata(filename: str, expected_data_type: str, expected_symbol: str = None, expected_interval: str = None, expected_timezone: str = None) tuple[source]¶
Extract metadata from a filename. Symbol, tick interval, timezone and start timestamp and end timestamp. Also data type like atomic trades or klines.
- Parameters:
filename – A filename with expected format. Example: LTCBTC 1m Europe-Madrid optional comment klines 1691332020000 1691391959999.csv
expected_data_type (str) – Expected data type. Example: “klines” or “aggTrades” or “atomicTrades”
expected_interval (str) – Expected tick interval. Example: “1m” or “1h”.
expected_symbol (str) – Expected symbol. Example: “LTCBTC”
expected_timezone (str) – Expected timezone. Example: “Europe/Madrid”
- Returns:
A dict with metadata.
- read_csv_to_dataframe(filename: str, col_sep: str = ',', index_col: str = None, index_time_zone: str = None, symbol: str = None, secondary_index_col: str = None) DataFrame[source]¶
Creates a csv file from a dataframe.
- Parameters:
filename (str) – The file name with or without path.
col_sep (str) – Column separator. Default is “,”
index_col (bool) – Name of the column to use as index. Default is None.
secondary_index_col (str) – In case of duplicated index, it will be used as secondary criterio for sorting data.
index_time_zone (str) – Time zone to use in index. Default is None.
symbol (str) – Symbol to use in index name. Default is None.
- Return pd.DataFrame:
A dataframe with data in columns using file rows header.
- read_file(filename: str) list[source]¶
Read a file to a list of strings each line.
- Return list:
list with a string each row in the file.
- save_file(filename: str, data: list, mode='w') None[source]¶
Save a new file from a list of lists each line.
- select_file(path='.', extension='csv', symbol: str = None, tick_interval: str = None, name_filter: str = None) str[source]¶
Selects from files in the path with the extension passed.
- Parameters:
path (str) – Path to search files.
extension (str) – Extension of interesting files to select.
symbol (str) – Symbol to filter files just to alert if file has other symbol.
tick_interval (str) – Tick interval to filter files just to alert if file has other tick interval.
name_filter (str) – Filter to apply to files. Example: “klines” or “aggTrades” or “atomicTrades”.
- Return str:
a filename.
- get_database_password() str[source]¶
Get the PostgreSQL password in plain text from panzer (encrypted on disk).
- Return str:
The PostgreSQL password.
- add_any_key(key: str, key_name: str) None[source]¶
Store any credential in panzer’s CredentialManager (
~/.panzer_creds).Sensitive names (containing
secret,api_key,passwordor_id) are encrypted automatically; the rest are stored as plain text.Example to add a database password:
from binpan.storage.files import add_any_key add_any_key(key="xxxxxxxxxx", key_name="postgresql_password")