Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. import uuid
  2. import json
  3. import os.path
  4.  
  5. class Collection:
  6.     def __init__(self, name, db_name):
  7.         self.name = name
  8.         self.db_name = db_name
  9.         self.data = {}
  10.         filename = db_name + '_' + name
  11.         if (os.path.isfile(filename)):
  12.             datafile = open(filename, "r")
  13.             loaded = json.loads(datafile.read())
  14.             self.data = loaded
  15.  
  16.     def __getitem__(self, key):
  17.         return self.data[key]
  18.  
  19.     def __delitem__(self, key):
  20.         del self.data[key]
  21.  
  22.     def insert(self, dict):
  23.         uid = str(uuid.uuid4())
  24.         self.data[uid] = dict
  25.         self.save()
  26.         return uid
  27.  
  28.     def find(self, dict):
  29.         results = []
  30.         for key in self.data:
  31.             if(all(item in self.data[key].items() for item in dict.items())):
  32.                 results.append(self.data[key])
  33.         return results
  34.  
  35.     def save(self):
  36.         text = json.dumps(self.data)
  37.         text_file = open(self.db_name + '_' + self.name, "w")
  38.         text_file.write(text)
  39.         text_file.close()
  40.  
  41. class DB:
  42.     def __init__(self, name):
  43.         self.name = name
  44.         self.data = {}
  45.  
  46.     def __getattr__(self, attr):
  47.         if (not(attr in self.__dict__)):
  48.             self.data[attr] = Collection(attr, self.name)
  49.         return self.data[attr]
  50.  
  51.     def __enter__(self):
  52.         return self
  53.  
  54.     def __exit__(self, type, value, traceback):
  55.         pass
  56.  
  57. # Test
  58.  
  59. with DB('db name') as db:
  60.  collection = db.my_product_collection # collection is an instance of Collection
  61.  # 'my_product_collection' can be any string identifying this specific collection
  62.  # One DB has multiple Collections. One Collection has multiple documents
  63.  uid = collection.insert({'my': 'val'}) # insert always get a dict as its sole argument
  64.  print(collection[uid])
  65.  del collection[uid]
  66.  collection.insert({'name': 'Bob', 'group': 'A', 'age': 22})
  67.  collection.insert({'name': 'Alice', 'group': 'A', 'age': 22})
  68.  collection.insert({'name': 'John', 'group': 'B'})
  69.  collection.insert({'name': 'Jack', 'email': 'jack@domain.tld'})
  70.  for uid, it in collection.find({'group': 'A', 'age': 22}):
  71.  # Equivalent to 'SELECT * FROM my_product_collection WHERE group == "A" AND age == 22'
  72.  # Find always receives a dict, the query, and should return all the documents
  73.  # that have all the keys in the query with the right value
  74.  # Here: Bob and Alice
  75.     print('found', uid, it)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement