Modules
CustomLogger
¶
Class to create custom loggers using the Loguru package.
This class should not be instantiated directly. Use the get_logger class method
to retrieve the logger instance.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Raised if the class constructor is called directly. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
__init__()
¶
Initialize the custom logger instance.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
Raised if the class constructor is called directly. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
28 29 30 31 32 33 34 35 | |
get_logger()
classmethod
¶
Retrieve an instance of the logger with customised settings.
The logger is configured to output logs to stderr with a specified format and
log level set by the environment variable LOG_LEVEL, defaults to INFO.
Returns:
| Name | Type | Description |
|---|---|---|
Logger |
Logger
|
An instance of the customised Loguru logger. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
logger_patch(record)
classmethod
¶
Customises the log record format for the Loguru logger.
This method is used to patch the logger and serialize the log record data into JSON format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
record
|
dict[str, Any]
|
Dictionary containing log record data. |
required |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
get_aws_context_value(list_of_args, key)
¶
Retrieve a specific value from the AWS context argument based on the provided key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_args
|
list[tuple[str, Any]]
|
List of arguments from the function. |
required |
key
|
str
|
The key to retrieve from the context argument. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Optional[str]: The value associated with the key in the AWS context, or None if not found. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
get_func_signature(list_of_args)
¶
Generate a string representation of the function's signature, with parameters filtered and masked as needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_args
|
list[tuple[str, Any]]
|
List of arguments passed to the function. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
String representation of the function's signature. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
142 143 144 145 146 147 148 149 150 151 152 153 154 | |
is_primitive(obj)
¶
Check if an object is an instance of a primitive type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
Any
|
Standard Python object. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the object is a primitive type, False otherwise. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
108 109 110 111 112 113 114 115 116 117 118 119 | |
loggable(_func=None, *, log_params=True, log_primitive_params_only=True, log_response=False, params_to_mask=None)
¶
Log function execution details.
Includies start/end time,parameters, responses, and execution time. By default, only primitive parameters (bool, str, int, float, None) are logged and response values are suppressed unless specified.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
_func
|
Callable
|
Function to wrap with the decorator. Defaults to None. |
None
|
log_params
|
bool
|
Whether to log function parameters. Defaults to True. |
True
|
log_primitive_params_only
|
bool
|
Whether to log only primitive parameters. Defaults to True. |
True
|
log_response
|
bool
|
Whether to log function responses. Defaults to False. |
False
|
params_to_mask
|
Optional[List[str]]
|
List of parameter names to mask in logs. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Callable |
Callable
|
A wrapped function with logging functionality. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | |
suppress_args_to_mask(list_of_args, params_to_mask)
¶
Filter out arguments to be masked, replacing them with 'suppressed'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_args
|
list[tuple[str, Any]]
|
list of function arguments. |
required |
params_to_mask
|
list[str]
|
List of string representations of params to mask. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, Any]]
|
list[tuple[str, Any]]: List of arguments with non-primitives suppressed. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
suppress_non_primitive_args(list_of_args)
¶
Filter out non-primitive arguments, replacing them with 'suppressed'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_of_args
|
list[tuple[str, Any]]
|
List of function arguments. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[str, Any]]
|
list[tuple[str, Any]]: List of arguments with non-primitives suppressed. |
Source code in src/monzo_api_wrapper/utils/custom_logger.py
95 96 97 98 99 100 101 102 103 104 105 | |
Db
¶
Class to manage connection to a database and perform SQL operations.
Source code in src/monzo_api_wrapper/utils/db.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
__init__()
¶
Initialize the Db class and set up a database engine.
Initializes the SQLAlchemy engine and logs the connection status.
Source code in src/monzo_api_wrapper/utils/db.py
42 43 44 45 46 47 48 49 | |
create_db_engine()
¶
Create an SQLAlchemy engine for the database connection.
Returns:
| Name | Type | Description |
|---|---|---|
Engine |
Engine
|
An SQLAlchemy Engine instance configured with database connection details. |
Source code in src/monzo_api_wrapper/utils/db.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
delete(table, data)
¶
Delete data from a table in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
The name of the database table from which data will be deleted. |
required |
data
|
str
|
Condition to specify which rows to delete, formatted as a SQL condition. |
required |
Source code in src/monzo_api_wrapper/utils/db.py
122 123 124 125 126 127 128 129 130 131 132 | |
insert(table, df=None, sql=None)
¶
Insert data into a table in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table
|
str
|
The name of the target database table. |
required |
df
|
Optional[DataFrame]
|
DataFrame containing data to insert. Defaults to None. |
None
|
sql
|
Optional[str]
|
Custom SQL insert statement. Defaults to None. |
None
|
Raises:
| Type | Description |
|---|---|
InsertArgumentError
|
If both |
Source code in src/monzo_api_wrapper/utils/db.py
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
query(sql, return_data=True)
¶
Execute an SQL query against the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sql
|
str
|
The SQL query to be executed. |
required |
return_data
|
bool
|
If True, returns query data as a DataFrame. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
Optional[DataFrame]
|
Optional[pd.DataFrame]: DataFrame with query results if return_data=True, else None. |
Source code in src/monzo_api_wrapper/utils/db.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
InsertArgumentError
¶
Bases: ValueError
Custom exception raised when both DataFrame and SQL string arguments are missing for an insert operation.
This exception is used to ensure that at least one valid data source (a DataFrame or an SQL string) is provided when attempting to insert data into the database.
Source code in src/monzo_api_wrapper/utils/db.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
__init__()
¶
Initialize the InsertArgumentError exception with a specific error message.
This constructor sets the error message that indicates that either a DataFrame or an SQL string must be provided for an insert operation.
The error message is passed to the base ValueError class.
Source code in src/monzo_api_wrapper/utils/db.py
25 26 27 28 29 30 31 32 33 34 35 36 | |
get_transactions_df(monzo_auth, account_id, account_name, days_lookback=30)
¶
Fetch recent transactions from Monzo API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monzo_auth
|
Authentication
|
Monzo authentication object. |
required |
account_id
|
str
|
Monzo account ID. |
required |
account_name
|
str
|
Monzo account name. |
required |
days_lookback
|
int
|
Number of days to look back, defaults to 30. |
30
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame of fetched transactions. |
Source code in src/monzo_api_wrapper/get_transactions.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | |
get_balances(monzo_auth, source_account)
¶
Fetch and process balances from Monzo pots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monzo_auth
|
object
|
Monzo authentication object. |
required |
source_account
|
str
|
Monzo source account ID. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame containing the balances and details of pots. |
Source code in src/monzo_api_wrapper/get_balances.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
NoTransactionsFoundError
¶
Bases: Exception
Exception raised when no transactions are found in the database.
Source code in src/monzo_api_wrapper/upload_transactions.py
10 11 12 13 14 15 16 17 18 19 20 21 22 | |
__init__(message='No transactions found in database.')
¶
Initializes the NoTransactionsFoundError exception.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
A custom error message to describe the exception. Defaults to "No transactions found in database." |
'No transactions found in database.'
|
Source code in src/monzo_api_wrapper/upload_transactions.py
13 14 15 16 17 18 19 20 21 22 | |
get_changed_transaction_ids(db, table, fetched_transactions)
¶
Identify transactions that have changed based on a set of columns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Db
|
Database connection object. |
required |
table
|
str
|
Name of the table to check transactions against. |
required |
fetched_transactions
|
DataFrame
|
DataFrame containing fetched transactions. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Optional[List[str]]: List of transactions IDs that have changed, or None if no changes are found. |
Raises:
| Type | Description |
|---|---|
NoTransactionsFoundError
|
If no transactions are found in the database. |
Source code in src/monzo_api_wrapper/upload_transactions.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 | |
get_db_transactions(db, table)
¶
Fetch transactions from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Db
|
Database connection object. |
required |
table
|
str
|
Name of the table to fetch transactions from. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame | None
|
pd.DataFrame | None: DataFrame containing the transactions fetched from the database. |
Source code in src/monzo_api_wrapper/upload_transactions.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
get_new_transactions(db, table, fetched_transactions)
¶
Identify new transactions that are not present in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Db
|
Database connection object. |
required |
table
|
str
|
Name of the table to check transactions against. |
required |
fetched_transactions
|
DataFrame
|
DataFrame containing fetched transactions. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame containing new transactions to be uploaded. |
Source code in src/monzo_api_wrapper/upload_transactions.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
get_changed_balances(database, table_name, current_balances)
¶
Identify balances that have changed compared to the ones in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Db
|
Database connection object. |
required |
table_name
|
str
|
Name of the table to check for existing balances. |
required |
current_balances
|
DataFrame
|
DataFrame containing the current balances. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame | Any
|
pd.DataFrame: DataFrame containing the balances that have changed. |
Source code in src/monzo_api_wrapper/upload_balances.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | |
get_new_balances(database, table_name, current_balances)
¶
Identify new balances that are not yet uploaded to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Db
|
Database connection object. |
required |
table_name
|
str
|
Name of the table to check for existing balances. |
required |
current_balances
|
DataFrame
|
DataFrame containing the current balances. |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: DataFrame containing the new balances to be uploaded. |
Source code in src/monzo_api_wrapper/upload_balances.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
update_changed_balances(database, table_name, current_balances, updated_balances)
¶
Update the balances in the database by deleting and reinserting the changed balances.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database
|
Db
|
Database connection object. |
required |
table_name
|
str
|
Name of the table to update balances. |
required |
current_balances
|
DataFrame
|
DataFrame containing the current balances. |
required |
updated_balances
|
DataFrame
|
DataFrame containing the balances that have changed. |
required |
Source code in src/monzo_api_wrapper/upload_balances.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |