Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some feature #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions pickledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import atexit
import sys
import os
import signal
Expand All @@ -56,6 +55,8 @@ def __init__(self, location, auto_dump, sig):
if sig:
self.set_sigterm_handler()

atexit.register(self._autodumpdb)

def __getitem__(self, item):
'''Syntax sugar for get()'''
return self.get(item)
Expand All @@ -68,6 +69,10 @@ def __delitem__(self, key):
'''Sytax sugar for rem()'''
return self.rem(key)

def __contains__(self, key):
'''Sytax sugar for exists()'''
return self.exists(key)

def set_sigterm_handler(self):
'''Assigns sigterm_handler for graceful shutdown during dump()'''
def sigterm_handler():
Expand All @@ -89,10 +94,12 @@ def load(self, location, auto_dump):

def dump(self):
'''Force dump memory db to file'''
json.dump(self.db, open(self.loco, 'wt'))
json.dump(self.db, open(self.loco, 'wt'), ensure_ascii=False, indent=4)
self.dthread = Thread(
target=json.dump,
args=(self.db, open(self.loco, 'wt')))
args=(self.db, open(self.loco, 'wt')),
kwargs=dict(ensure_ascii=False, indent=4)
)
self.dthread.start()
self.dthread.join()
return True
Expand Down
4 changes: 4 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ def test_sugar_rem(self):
del self.db["foo"]
assert "foo" not in self.db.db

def test_sugar_exists(self):
self.db["foo"] = "bar"
assert "foo" in self.db

def test_set(self):
self.db.set('key', 'value')
x = self.db.get('key')
Expand Down