33By Science Spot from Decimal Developement
44
55Simple wrapper for PyMongo written in python!
6- v0.0.6
6+ v0.0.7
77"""
88
9- # v0.0.6
10- __version__ = '0.0.6'
9+ # v0.0.7
10+ __version__ = '0.0.7'
11+
12+ # Import Time
13+ from time import time
1114
1215# Import mongo client from pymongo
1316from pymongo import MongoClient
1417
15- # Util Class
16-
17- class Util ():
18-
19- # Startswith filter
20- def startswith (self , data , query : str ):
21- result = []
22-
23- for doc in data :
24- if (doc ['key' ].startswith (query )):
25- result .append (doc )
26-
27- return result
28-
29-
30- # Base Class with basic things: Set, Get, Delete, All...
31- class Base ():
32-
33- # Constructor Class
34- def __init__ (self , client , options : dict = {}):
35- self .client = client
36- self .db = self .client [options ['db_name' ]]
37- self .options = options
38- self .collection = self .db [self .options ['collection_name' ]]
39-
40- def set (self , key , value ):
41- self .collection .delete_many ({
42- 'key' : key
43- })
44-
45- self .collection .insert_one ({
46- 'key' : key ,
47- 'value' : value
48- })
49- return
50-
51- def get (self , key ):
52- try :
53- try :
54- return self .collection .find ({ 'key' : key })[0 ]['value' ]
55- except :
56- return self .collection .find ({ 'key' : key })[0 ]
57- except :
58- return None
59-
60- def all (self ):
61- data = self .collection .find ({})
62- res = []
63-
64- for doc in list (data ):
65- try :
66- res .append ({
67- 'key' : doc ['key' ],
68- 'value' : doc ['value' ]
69- })
70- except :
71- return list (data )
72-
73- return res
74-
75- def drop (self ):
76- self .collection .drop ()
77-
78- def delete (self , key : str ):
79- self .collection .delete_many ({
80- 'key' : key
81- })
82- return
18+ # Import files
19+ from .Util import Util , AttrDict
20+ from .Base import Base
8321
8422# Database Class which is the main class
8523class Database ():
8624
87- def __init__ (self , mongoURL : str , options : dict = {}):
25+ def __init__ (self , mongoURL : str , options : dict = {}, events : dict = {}):
26+ startedAt = time ()
8827 try :
8928 self .client = MongoClient (mongoURL )
9029 except :
@@ -106,6 +45,23 @@ def __init__(self, mongoURL: str, options: dict = {}):
10645 self .db = self .base .db
10746 self .collection = self .base .collection
10847
48+ for callback in events .values ():
49+ if not callable (callback ):
50+ raise TypeError ('event parameter dict values must contain a function' )
51+
52+ if 'ready' in events .keys ():
53+ readyCallback = events ['ready' ]
54+ param = AttrDict ({
55+ 'started_at' : startedAt ,
56+ 'connected_at' : time (),
57+ 'time_took_to_connect' : time () - startedAt
58+ })
59+
60+ try :
61+ readyCallback (param )
62+ except TypeError :
63+ raise TypeError ('ready callback function must have 1 parameter' )
64+
10965 def all_database_names (self ):
11066 return self .base .client .list_database_names ()
11167
0 commit comments