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 file deletion option to deldb() #89

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
2 changes: 1 addition & 1 deletion docs/commands.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ <h1>Current Commands</h1>
<p><code><span class="c2">DREM</span> <span class="c9">name</span></code> &rarr; Remove a dict and all of its pairs <small><em>(available since 0.2.2)</em></small></p>
<p><code><span class="c2">DPOP</span> <span class="c9">name</span> <span class="c9">key</span></code> &rarr; Remove one key-value in a dict <small><em>(available since 0.2.2)</em></small></p>
<p><code><span class="c2">DMERGE</span> <span class="c9">name1</span> <span class="c9">name2</span> <span class="c9">name3</span></code> &rarr; Merge <code>name1</code> and <code>name2</code> into a new dict: <code>name3</code> <small><em>(available since 0.7.3)</em></small>
<p><code><span class="c2">DELDB</span></code> &rarr; Delete everything from the database <small><em>(available since 0.2.1)</em></small></p>
<p><code><span class="c2">DELDB</span> <span class="c9">andfile</span></code> &rarr; Delete everything from the database, set <code>andfile</code> to <code>True</code> to delete the database file too.<small><em>(available since 0.2.1)</em></small></p>
<p><code><span class="c2">DUMP</span></code> &rarr; Save the database from memory to a file specified in <code>LOAD</code> <small><em>(available since 0.3)</em></small></p>
<h1>Suggestions</h1>
<p>If you would like to suggest a command, you can create an <a href="http://github.com/patx/pickledb/issues">issue on GitHub</a>.</p>
Expand Down
14 changes: 8 additions & 6 deletions pickledb.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ def load(location, auto_dump, sig=True):

class PickleDB(object):

key_string_error = TypeError('Key/name must be a string!')

key_string_error = TypeError('Key/name must be a string!')
def __init__(self, location, auto_dump, sig):
'''Creates a database object and loads the data from the location path.
If the file does not exist it will be created on the first update.
'''
self.load(location, auto_dump)
self.__location__ = location
self.dthread = None
if sig:
self.set_sigterm_handler()
Expand All @@ -63,11 +63,11 @@ def __getitem__(self, item):
return self.get(item)

def __setitem__(self, key, value):
'''Sytax sugar for set()'''
'''Syntax sugar for set()'''
return self.set(key, value)

def __delitem__(self, key):
'''Sytax sugar for rem()'''
'''Syntax sugar for rem()'''
return self.rem(key)

def set_sigterm_handler(self):
Expand Down Expand Up @@ -290,9 +290,11 @@ def dmerge(self, name1, name2):
self._autodumpdb()
return True

def deldb(self):
'''Delete everything from the database'''
def deldb(self, andfile = False):
'''Delete everything from the database, optionally delete the database file'''
self.db = {}
self._autodumpdb()
if andfile:
os.remove(self.__location__)
return True

8 changes: 8 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ def test_not_dexists(self):
assert x is False
self.db.drem('dict')

def test_location(self):
assert self.db.__location__ == 'tests.db'
self.db.dump()
self.db.deldb()
from pickledb import os
assert os.path.exists(self.db.__location__)
self.db.deldb(True)
assert not os.path.exists(self.db.__location__)

if __name__ == "__main__":
tests = TestClass()
Expand Down