-
Notifications
You must be signed in to change notification settings - Fork 0
/
no_sql_db.py
84 lines (65 loc) · 2.67 KB
/
no_sql_db.py
1
2
3
4
5
6
7
8
9
10
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# This file provides a very simple "no sql database using python dictionaries"
# If you don't know SQL then you might consider something like this for this course
# We're not using a class here as we're roughly expecting this to be a singleton
# If you need to multithread this, a cheap and easy way is to stick it on its own bottle server on a different port
# Write a few dispatch methods and add routes
# A heads up, this code is for demonstration purposes; you might want to modify it for your own needs
# Currently it does basic insertions and lookups
class Table():
def __init__(self, table_name, *table_fields):
self.entries = []
self.fields = table_fields
self.name = table_name
def create_entry(self, data):
'''
Inserts an entry in the table
Doesn't do any type checking
'''
# Bare minimum, we'll check the number of fields
if len(data) != len(self.fields):
raise ValueError('Wrong number of fields for table')
self.entries.append(data)
return
def search_table(self, target_field_name, target_value):
'''
Search the table given a field name and a target value
Returns the first entry found that matches
'''
# Lazy search for matching entries
for entry in self.entries:
for field_name, value in zip(self.fields, entry):
if target_field_name == field_name and target_value == value:
return entry
# Nothing Found
return None
class DB():
'''
This is a singleton class that handles all the tables
You'll probably want to extend this with features like multiple lookups, and deletion
A method to write to and load from file might also be useful for your purposes
'''
def __init__(self):
self.tables = {}
# Setup your tables
self.add_table('users', "id", "username", "password")
return
def add_table(self, table_name, *table_fields):
'''
Adds a table to the database
'''
table = Table(table_name, *table_fields)
self.tables[table_name] = table
return
def search_table(self, table_name, target_field_name, target_value):
'''
Calls the search table method on an appropriate table
'''
return self.tables[table_name].search_table(target_field_name, target_value)
def create_table_entry(self, table_name, data):
'''
Calls the create entry method on the appropriate table
'''
return self.tables[table_name].create_entry(data)
# Our global database
# Invoke this as needed
database = DB()