From e19e1e1030189afc0138d7f83d3a411b559aa501 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 4 Jun 2023 06:47:28 +0200 Subject: [PATCH] Continued: - there are no private variables or methods, all is public - there is however a notation, that a leading _ indicates private access and that you should search for getters/setters - so when you see some code like `module._foo()` then notify them about this --- fba/cache.py | 12 ++++++------ fba/config.py | 10 +++++----- fba/instances.py | 28 ++++++++++++++-------------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/fba/cache.py b/fba/cache.py index 068fd35..843e353 100644 --- a/fba/cache.py +++ b/fba/cache.py @@ -16,12 +16,12 @@ # Cache for redundant SQL queries -cache = {} +_cache = {} ##### Cache ##### def is_cache_initialized(key: str) -> bool: - return key in cache + return key in _cache def set_all_cache_key(key: str, rows: list, value: any): # NOISY-DEBUG: print(f"DEBUG: key='{key}',rows()={len(rows)},value[]={type(value)} - CALLED!") @@ -29,13 +29,13 @@ def set_all_cache_key(key: str, rows: list, value: any): raise ValueError("Parameter key[]='{type(key)}' is not 'str'") elif not is_cache_initialized(key): # NOISY-DEBUG: print(f"DEBUG: Cache for key='{key}' not initialized.") - cache[key] = {} + _cache[key] = {} for sub in rows: # NOISY-DEBUG: print(f"DEBUG: Setting key='{key}',sub[{type(sub)}]='{sub}'") if isinstance(sub, tuple): - cache[key][sub[0]] = value + _cache[key][sub[0]] = value else: print(f"WARNING: Unsupported type row[]='{type(row)}'") @@ -50,7 +50,7 @@ def set_cache_key(key: str, sub: str, value: any): print(f"WARNING: Bad method call, key='{key}' is not initialized yet.") raise Exception(f"Cache for key='{key}' is not initialized, but function called") - cache[key][sub] = value + _cache[key][sub] = value def is_cache_key_set(key: str, sub: str) -> bool: if type(key) != str: @@ -61,4 +61,4 @@ def is_cache_key_set(key: str, sub: str) -> bool: print(f"WARNING: Bad method call, key='{key}' is not initialized yet.") raise Exception(f"Cache for key='{key}' is not initialized, but function called") - return sub in cache[key] + return sub in _cache[key] diff --git a/fba/config.py b/fba/config.py index 2b41be2..dc5dd22 100644 --- a/fba/config.py +++ b/fba/config.py @@ -17,7 +17,7 @@ import json with open("config.json") as f: - config = json.loads(f.read()) + _config = json.loads(f.read()) def get(key: str) -> any: # DEBUG: print(f"DEBUG: key[{type(key)}]={key} - CALLED!") @@ -25,8 +25,8 @@ def get(key: str) -> any: raise ValueError(f"Parameter key[]='{type(key)}' is not 'str'") elif key == "": raise ValueError("Parameter 'key' cannot be empty") - elif not key in config: - raise KeyError(f"key='{key}' does not exist in config array") + elif not key in _config: + raise KeyError(f"key='{key}' does not exist in _config array") - # DEBUG: print(f"DEBUG: config[{key}]={config[key]} - EXIT!") - return config[key] \ No newline at end of file + # DEBUG: print(f"DEBUG: _config[{key}]={_config[key]} - EXIT!") + return _config[key] diff --git a/fba/instances.py b/fba/instances.py index 9b0323a..2b507fe 100644 --- a/fba/instances.py +++ b/fba/instances.py @@ -20,7 +20,7 @@ import sys # Found info from node, such as nodeinfo URL, detection mode that needs to be # written to database. Both arrays must be filled at the same time or else # update_instance_data() will fail -instance_data = { +_pending = { # Detection mode: 'AUTO_DISCOVERY', 'STATIC_CHECKS' or 'GENERATOR' # NULL means all detection methods have failed (maybe still reachable instance) "detection_mode" : {}, @@ -52,13 +52,13 @@ def set_instance_data(key: str, domain: str, value: any): raise ValueError("Parameter domain[]='{type(domain)}' is not 'str'") elif domain == "": raise ValueError(f"Parameter 'domain' cannot be empty") - elif not key in instance_data: - raise ValueError(f"key='{key}' not found in instance_data") + elif not key in _pending: + raise ValueError(f"key='{key}' not found in _pending") elif not fba.is_primitive(value): raise ValueError(f"value[]='{type(value)}' is not a primitive type") # Set it - instance_data[key][domain] = value + _pending[key][domain] = value # DEBUG: print("DEBUG: EXIT!") @@ -70,9 +70,9 @@ def has_pending_instance_data(domain: str) -> bool: raise ValueError(f"Parameter 'domain' cannot be empty") has_pending = False - for key in instance_data: - # DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',instance_data[key]()='{len(instance_data[key])}'") - if domain in instance_data[key]: + for key in _pending: + # DEBUG: print(f"DEBUG: key='{key}',domain='{domain}',_pending[key]()='{len(_pending[key])}'") + if domain in _pending[key]: has_pending = True break @@ -91,11 +91,11 @@ def update_instance_data(domain: str): # DEBUG: print(f"DEBUG: Updating nodeinfo for domain='{domain}' ...") sql_string = '' fields = list() - for key in instance_data: + for key in _pending: # DEBUG: print("DEBUG: key:", key) - if domain in instance_data[key]: - # DEBUG: print(f"DEBUG: Adding '{instance_data[key][domain]}' for key='{key}' ...") - fields.append(instance_data[key][domain]) + if domain in _pending[key]: + # DEBUG: print(f"DEBUG: Adding '{_pending[key][domain]}' for key='{key}' ...") + fields.append(_pending[key][domain]) sql_string += f" {key} = ?," fields.append(domain) @@ -119,11 +119,11 @@ def update_instance_data(domain: str): # DEBUG: print("DEBUG: Committing changes ...") fba.connection.commit() - # DEBUG: print("DEBUG: Deleting instance_data for domain:", domain) - for key in instance_data: + # DEBUG: print("DEBUG: Deleting _pending for domain:", domain) + for key in _pending: try: # DEBUG: print("DEBUG: Deleting key:", key) - del instance_data[key][domain] + del _pending[key][domain] except: pass -- 2.39.5