add type awareness to the pseudodb
This commit is contained in:
parent
e3fad44b12
commit
ea4794a7bc
27
examples.py
27
examples.py
@ -1,3 +1,5 @@
|
||||
import array
|
||||
|
||||
from pseudodb import pseudodb
|
||||
from utils.file_utils import search_path
|
||||
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')
|
||||
data_collections = db.get_collections()
|
||||
print("Collections in Pseudodb: "+str(data_collections))
|
||||
for collection in data_collections:
|
||||
print("Collection: "+collection)
|
||||
for collection, coltype in data_collections:
|
||||
|
||||
if coltype is dict:
|
||||
print("Processing Dictionary: "+collection)
|
||||
for key in db.get_keys(collection):
|
||||
print("\t"+key+": "+str(db.get_values(collection, key)))
|
||||
|
||||
print()
|
||||
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')
|
||||
print("Immutable: "+str(immutable_list))
|
||||
|
||||
shuffled_list = immutable_to_list(immutable_list, scramble=True)
|
||||
print("Shuffled Mutable: "+str(shuffled_list))
|
||||
|
||||
print("\n\n")
|
||||
files = search_path("./utils", "misc")
|
||||
print("Search Results: "+str(files))
|
||||
|
||||
print("\n\n")
|
||||
rando_string = get_random_string(30, caps=True, numbers=True, specials=True)
|
||||
print("Random: "+rando_string)
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
from array import array as ar
|
||||
|
||||
names = {
|
||||
"male": ["Greg", "John", "Edward", "Francis", "Charles", "Luke", "Thomas"],
|
||||
"female": ["Doreen", "Janice", "Sue", "Mary", "Lisa", "Janine", "Doris"]
|
||||
@ -14,3 +16,13 @@ languages = {
|
||||
"interpreted": ["Ruby", "Rexx", "Perl"],
|
||||
"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!"
|
||||
|
@ -1,4 +1,5 @@
|
||||
import importlib
|
||||
from array import array
|
||||
|
||||
|
||||
class PseudoDB:
|
||||
@ -20,9 +21,12 @@ class PseudoDB:
|
||||
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
|
||||
if not attr_name.startswith('__') and not attr_name == 'ar':
|
||||
collection = getattr(self.data, attr_name)
|
||||
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:
|
||||
return []
|
||||
|
||||
@ -34,12 +38,18 @@ class PseudoDB:
|
||||
else:
|
||||
return {}
|
||||
|
||||
def get_values(self, attr_name, key):
|
||||
def get_values(self, attr_name, key=None):
|
||||
if self.data is not None:
|
||||
collection = getattr(self.data, attr_name)
|
||||
if isinstance(collection, dict):
|
||||
return list(collection[key])
|
||||
else:
|
||||
elif isinstance(collection, list):
|
||||
return collection
|
||||
elif isinstance(collection, array):
|
||||
return collection
|
||||
elif isinstance(collection, tuple):
|
||||
return collection
|
||||
else: # assume it's a string
|
||||
return str(collection)
|
||||
else:
|
||||
return None
|
||||
|
Loading…
Reference in New Issue
Block a user