From 32d18c880b94a1e01b15d78d1db7ddfb3d2e28b8 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Wed, 17 Jul 2024 22:30:12 +0100 Subject: [PATCH] initial commit --- .gitignore | 2 ++ examples.py | 34 ++++++++++++++++++++++++ pseudodb/__init__.py | 0 pseudodb/default/__init__.py | 0 pseudodb/default/data.py | 16 +++++++++++ pseudodb/pseudodb.py | 51 ++++++++++++++++++++++++++++++++++++ utils/__init__.py | 0 utils/file_utils.py | 21 +++++++++++++++ utils/misc_utils.py | 16 +++++++++++ utils/str_utils.py | 36 +++++++++++++++++++++++++ 10 files changed, 176 insertions(+) create mode 100644 .gitignore create mode 100644 examples.py create mode 100644 pseudodb/__init__.py create mode 100644 pseudodb/default/__init__.py create mode 100644 pseudodb/default/data.py create mode 100644 pseudodb/pseudodb.py create mode 100644 utils/__init__.py create mode 100644 utils/file_utils.py create mode 100644 utils/misc_utils.py create mode 100644 utils/str_utils.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..acb6bcd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +**/__pycache__/ diff --git a/examples.py b/examples.py new file mode 100644 index 0000000..4398aa8 --- /dev/null +++ b/examples.py @@ -0,0 +1,34 @@ +from pseudodb import pseudodb +from utils.file_utils import search_path +from utils.str_utils import obscure, unobscure, get_random_string, get_dash_suffix +from utils.misc_utils import immutable_to_list + + +if __name__ == '__main__': + db = pseudodb.PseudoDB('pseudodb.default.data') + print("Collections in Pseudodb: "+str(db.data_list())) + for collection in db.data_list(): + print("Collection: "+collection) + for key in db.get_keys(collection): + print(key+": "+str(db.get_values(collection, key))) + + immutable_list = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine') + print("Immutable: "+str(immutable_list)) + + shuffled_list = immutable_to_list(immutable_list, scramble=True) + print("Shuffled Mutable: "+str(shuffled_list)) + + files = search_path("./utils", "misc") + print("Search Results: "+str(files)) + + rando_string = get_random_string(30, caps=True, numbers=True, specials=True) + print("Random: "+rando_string) + + obscured = obscure(rando_string) + print("Obscured: "+str(obscured)) + + unobscured = unobscure(obscured) + print("Unobscured: "+unobscured) + + dash_suffix = get_dash_suffix("example-silly-string-with-dashes-"+get_random_string(10)) + print("Suffix found: "+dash_suffix) diff --git a/pseudodb/__init__.py b/pseudodb/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pseudodb/default/__init__.py b/pseudodb/default/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pseudodb/default/data.py b/pseudodb/default/data.py new file mode 100644 index 0000000..e77cbca --- /dev/null +++ b/pseudodb/default/data.py @@ -0,0 +1,16 @@ +names = { + "male": ["Greg", "John", "Edward", "Francis", "Charles", "Luke", "Thomas"], + "female": ["Doreen", "Janice", "Sue", "Mary", "Lisa", "Janine", "Doris"] +} + +cars = { + "chevy": ["Blazer", "Tahoe", "Caprice", "Suburban", "Silverado"], + "ford": ["Mustang", "Bronco", "Focus", "Taurus", "Escape"], + "chrysler": ["Sebring", "Crossfire", "Pacifica", "Voyager"] +} + +languages = { + "compiled": ["C++", "C#", "C", "Assembler", "Rust", "Go"], + "interpreted": ["Ruby", "Rexx", "Perl"], + "bytecode": ["Java", "Python", "VisualBasic"] +} \ No newline at end of file diff --git a/pseudodb/pseudodb.py b/pseudodb/pseudodb.py new file mode 100644 index 0000000..fad60f8 --- /dev/null +++ b/pseudodb/pseudodb.py @@ -0,0 +1,51 @@ +import importlib + + +class PseudoDB: + def __init__(self, datasource="default.data"): + self.datasource = datasource + self.data = self.load_data() + + def load_data(self): + module_name = self.datasource.replace('.py', '') + try: + module = importlib.import_module(module_name) + return module + except ImportError: + print(f"Data module '{module_name}' not found.") + return None + + def data_list(self): + # Check if default has been successfully loaded + if self.data is not None: + attr_names = [] + for attr_name in dir(self.data): + if not attr_name.startswith('__'): + attr_names.append(attr_name) + return attr_names + else: + return [] + + def collections(self, attr_name): + if self.data is not None: + return getattr(self.data, attr_name) + else: + return None + + def get_keys(self, attr_name): + if self.data is not None: + collection = getattr(self.data, attr_name) + if isinstance(collection, dict): + return list(collection.keys()) + else: + return {} + + def get_values(self, attr_name, key): + if self.data is not None: + collection = getattr(self.data, attr_name) + if isinstance(collection, dict): + return list(collection[key]) + else: + return collection + else: + return None diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/file_utils.py b/utils/file_utils.py new file mode 100644 index 0000000..5375c5c --- /dev/null +++ b/utils/file_utils.py @@ -0,0 +1,21 @@ +import os + + +def search_path(path, substring): + """ + given a search substring, retrieve all the files in the Store + that have that substring in them. + + Args: + path (string): path to search + substring (string): the string to be matched against + + Returns: + list: a pythonized list of all the files in which the search + substring is contained. + """ + matched_files = [matched_file + for matched_file in os.listdir(path) + if substring in matched_file] + + return matched_files diff --git a/utils/misc_utils.py b/utils/misc_utils.py new file mode 100644 index 0000000..c1e1164 --- /dev/null +++ b/utils/misc_utils.py @@ -0,0 +1,16 @@ +from random import shuffle + + +def immutable_to_list(immutable, scramble=False, prune=0): + indices = list(range(len(immutable))) + if scramble: + shuffle(indices) + + python_list = [immutable[i] for i in indices] + + if prune > 0: + del python_list[prune:] + + return python_list + + diff --git a/utils/str_utils.py b/utils/str_utils.py new file mode 100644 index 0000000..c8956cb --- /dev/null +++ b/utils/str_utils.py @@ -0,0 +1,36 @@ +import random +import string +import base64 + + +def obscure(content): + data = content.encode('utf-8') + return base64.b64encode(data) + + +def unobscure(content): + data = content.decode('utf-8') + return str(base64.b64decode(data)) + + +def get_random_string(length, caps=False, numbers=False, specials=False): + letters = string.ascii_lowercase + if caps: + letters += string.ascii_uppercase + if numbers: + letters += string.digits + if specials: + letters += string.punctuation + + return ''.join(random.choice(letters) for _ in range(length)) + + +def get_colon_prefix(content): + split_string = content.split(":", 1) + return split_string[0].strip() + + +def get_dash_suffix(content): + last_dash = content.rfind("-") + rando = content[last_dash + 1:] + return rando