add type awareness to the pseudodb

This commit is contained in:
Greg Gauthier 2024-07-19 00:24:32 +01:00
parent e3fad44b12
commit ea4794a7bc
3 changed files with 53 additions and 12 deletions

View File

@ -1,3 +1,5 @@
import array
from pseudodb import pseudodb from pseudodb import pseudodb
from utils.file_utils import search_path from utils.file_utils import search_path
from utils.str_utils import obscure, unobscure, get_random_string, get_dash_suffix from utils.str_utils import obscure, unobscure, get_random_string, get_dash_suffix
@ -8,22 +10,39 @@ if __name__ == '__main__':
db = pseudodb.PseudoDB('pseudodb.default.data') db = pseudodb.PseudoDB('pseudodb.default.data')
data_collections = db.get_collections() data_collections = db.get_collections()
print("Collections in Pseudodb: "+str(data_collections)) print("Collections in Pseudodb: "+str(data_collections))
for collection in data_collections: for collection, coltype in data_collections:
print("Collection: "+collection)
for key in db.get_keys(collection):
print("\t"+key+": "+str(db.get_values(collection, key)))
print() if coltype is dict:
print("Processing Dictionary: "+collection)
for key in db.get_keys(collection):
print("\t"+key+": "+str(db.get_values(collection, key)))
elif coltype is list:
print("Processing List: "+collection)
print([str(item) for item in db.get_values(collection)])
elif coltype is array.array:
print("Processing Array: "+collection)
print([str(item) for item in db.get_values(collection)])
elif coltype is tuple:
print("Processing Tuple: "+collection)
print([str(item) for item in db.get_values(collection)])
else: # just assume it's a string
print(str(db.get_values(collection)))
print("\n\n")
immutable_list = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine') immutable_list = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine')
print("Immutable: "+str(immutable_list)) print("Immutable: "+str(immutable_list))
shuffled_list = immutable_to_list(immutable_list, scramble=True) shuffled_list = immutable_to_list(immutable_list, scramble=True)
print("Shuffled Mutable: "+str(shuffled_list)) print("Shuffled Mutable: "+str(shuffled_list))
print("\n\n")
files = search_path("./utils", "misc") files = search_path("./utils", "misc")
print("Search Results: "+str(files)) print("Search Results: "+str(files))
print("\n\n")
rando_string = get_random_string(30, caps=True, numbers=True, specials=True) rando_string = get_random_string(30, caps=True, numbers=True, specials=True)
print("Random: "+rando_string) print("Random: "+rando_string)

View File

@ -1,3 +1,5 @@
from array import array as ar
names = { names = {
"male": ["Greg", "John", "Edward", "Francis", "Charles", "Luke", "Thomas"], "male": ["Greg", "John", "Edward", "Francis", "Charles", "Luke", "Thomas"],
"female": ["Doreen", "Janice", "Sue", "Mary", "Lisa", "Janine", "Doris"] "female": ["Doreen", "Janice", "Sue", "Mary", "Lisa", "Janine", "Doris"]
@ -14,3 +16,13 @@ languages = {
"interpreted": ["Ruby", "Rexx", "Perl"], "interpreted": ["Ruby", "Rexx", "Perl"],
"bytecode": ["Java", "Python", "VisualBasic"] "bytecode": ["Java", "Python", "VisualBasic"]
} }
fruits = ["apple", "banana", "cherry", "orange", "strawberry", "pear"]
disparates = ["string!", 2020, 3.14, {"thing": "and such"}]
years = (2010, 2011, 2012, 2013, 2014, 2015)
floats = ar('f', [3.14, 0.5, 9.99, 1.5])
message = "This is just a plain old string!"

View File

@ -1,4 +1,5 @@
import importlib import importlib
from array import array
class PseudoDB: class PseudoDB:
@ -20,9 +21,12 @@ class PseudoDB:
if self.data is not None: if self.data is not None:
attr_names = [] attr_names = []
for attr_name in dir(self.data): for attr_name in dir(self.data):
if not attr_name.startswith('__'): if not attr_name.startswith('__') and not attr_name == 'ar':
attr_names.append(attr_name) collection = getattr(self.data, attr_name)
return attr_names attr_names.append((attr_name, type(collection)))
# Sort the collections by their name before returning
return sorted(attr_names, key=lambda x: x[0])
else: else:
return [] return []
@ -34,12 +38,18 @@ class PseudoDB:
else: else:
return {} return {}
def get_values(self, attr_name, key): def get_values(self, attr_name, key=None):
if self.data is not None: if self.data is not None:
collection = getattr(self.data, attr_name) collection = getattr(self.data, attr_name)
if isinstance(collection, dict): if isinstance(collection, dict):
return list(collection[key]) return list(collection[key])
else: elif isinstance(collection, list):
return collection return collection
elif isinstance(collection, array):
return collection
elif isinstance(collection, tuple):
return collection
else: # assume it's a string
return str(collection)
else: else:
return None return None