Skip to content

Commit

Permalink
Merge pull request #4 from RamanjaneyuluIdavalapati/master
Browse files Browse the repository at this point in the history
plyvel based diskdict
  • Loading branch information
Ram Idavalapati authored May 12, 2018
2 parents 59c46dc + 0e8e81c commit 84fcb39
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 32 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
language: python
python:
- '2.7'
- '3.5'
before_install:
- export DISKDICT_DATAFILE_PATH='/tmp/disk.dict'
- export DATA_FILE='/tmp/diskdict'
install:
- pip install .
script:
Expand All @@ -11,8 +11,8 @@ deploy:
- provider: releases
api_key:
secure: DrwGaBEFBMft6Cgd2k7koWQxVx28T9RpilvobItKgKBy4w/A7GDK5BO3ft0KmISaPcPghkx6wEUt+VynSYW5qTao7H/wCZ3jn5xN/5DRpT/NJt+jzXSXa1WlsBhs7zEYcXxI69ZCxRQ+Y5k0zgCooEyit9rUKtXoj6MakCy5o+rZcYZqEayazuTsRM8SZzqkk0A/DTHt0QadFsjxmiczcPxefYx4yNlsKbhfNFINjzpWrJg5P8KydO8gpUT03oRY899UYZ7sduKeTHx+6yUcwjoCW2h7P5PqAK6Gb11NU0nKg19AMBXhjbjWIMsOpsiieQavkMSVJ4ZyfMOuGc8i8oCb/0uiSmJbMG9pDVMXrIc07cCgx1RvGXUJyUEKsLaRBvWLZ64zWu4ZLOfaj/CM7s3ZTslnNojJQtbRrqhstYUnikQnOU7tI6Yl5ig2KV+u1TZWT4MLfrAo1ErxAffVEwIkm1aL3HFNW0+rAv2eUwF5dPaJGI38fg5S4IWe8vjYNWW6kLfcP5z+vgk7lTdt7jS645h826MZ9LQascBfGBNMqX6j73LeOaGXFIE+DmhPUqDI+9yEKtmYgBGxMHSRDGcR2ZTr1GRGIASTboQEvzLeiZSc/2xKHOeJnXnZz/U2SnbT0TpyIPPJ+4CgcloaMwqPjBi8E4iSCivm5PWw9Do=
name: diskdict-0.2
tag_name: 0.2
name: diskdict-0.2.1
tag_name: 0.2.1
on:
repo: deep-compute/diskdict
- provider: pypi
Expand Down
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ $ sudo pip install diskdict
```python
>>> from diskdict import DiskDict

# Creating the diskdict file
>>> dd = DiskDict('/tmp/disk.dict')
# Initiate the diskdict with the path
>>> dd = DiskDict('/tmp/diskdict')

# Storing the data into diskdict file
# Storing the data into diskdict
>>> dd['deepcompute'] = 1
>>> dd['deeporg'] = 2
>>> dd[1] = 5

# Get the values regarding keys from diskdict file
# Get the values regarding keys from diskdict
>>> print(dd.get('deepcompute'))
1

>>> print(dd['deepcompute'])
1

# Get key, value pairs in tuple format from diskdict file
# Get key, value pairs in tuple format from diskdict
>>> next(dd.items())
('deeporg', 2)

Expand All @@ -42,7 +42,7 @@ $ sudo pip install diskdict
('deepcompute', 1)
(1, 5)

# Get the keys from diskdict file
# Get the keys from diskdict
>>> next(dd.keys())
deeporg

Expand All @@ -53,7 +53,7 @@ deeporg
deepcompute
1

# Get the values from diskdict file
# Get the values from diskdict
>>> next(dd.values())
2

Expand Down Expand Up @@ -86,4 +86,3 @@ DiskDict Console
```
$ python setup.py test
```

49 changes: 31 additions & 18 deletions diskdict/diskdict.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import os
import pickle

from deeputil import Dummy
from sqlitedict import SqliteDict
import plyvel

DUMMY_LOG = Dummy()

# export DISKDICT_DATAFILE_PATH=='/home/usr/diskdict_file_path'
DATA_FILE = os.environ.get('DISKDICT_DATAFILE_PATH', '')
# export DISKDICT_DATADIR_PATH=='/home/usr/diskdict_dir_path'
DATA_FILE = os.environ.get('DISKDICT_DATADIR_PATH', '/tmp')


class DiskDict(object):
# FIXME: using eval - dangerous

def __init__(self, fpath, autocommit=True, log=DUMMY_LOG):
def __init__(self, path, log=DUMMY_LOG):
'''
>>> dd = DiskDict(DATA_FILE)
>>> dd['deepcompute'] = 1
'''

self._fpath = fpath
self._path = path
self.log = log
self._f = SqliteDict(fpath, autocommit=autocommit)
self._f = plyvel.DB(path, create_if_missing=True)

def _enckey(self, k):
return repr(k).encode('utf8')

def _deckey(self, k):
return eval(k.decode('utf8'))

def get(self, k, default=None):
'''
Expand All @@ -29,16 +36,22 @@ def get(self, k, default=None):
1
'''

k = repr(k)
return self._f.get(k)
k = self._enckey(k)
v = self._f.get(k, None)
if v is None: return default
return pickle.loads(v)

def __getitem__(self, k):
k = repr(k)
return self._f[k]
return self.get(k)

def __setitem__(self, k, v):
k = repr(k)
self._f[k] = v
k = self._enckey(k)
v = pickle.dumps(v)
self._f.put(k, v)

def __delitem__(self, k):
k = self._enckey(k)
self._f.delete(k)

def items(self):
'''
Expand All @@ -47,8 +60,8 @@ def items(self):
('deepcompute', 1)
'''

for k, v in self._f.iteritems():
yield eval(k), v
for k, v in self._f:
yield self._deckey(k), pickle.loads(v)

def values(self):
'''
Expand All @@ -57,7 +70,7 @@ def values(self):
1
'''

for v in self._f.itervalues():
for _, v in self.items():
yield v

def keys(self):
Expand All @@ -67,16 +80,16 @@ def keys(self):
deepcompute
'''

for k in self._f.iterkeys():
yield eval(k)
for k, _ in self.items():
yield k

def flush(self):
'''
>>> dd = DiskDict(DATA_FILE)
>>> dd.flush()
'''

self._f.commit()
pass

def close(self):
'''
Expand Down
11 changes: 9 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '0.2'
version = '0.2.1'
setup(
name="diskdict",
version=version,
Expand All @@ -14,11 +14,18 @@
install_requires=[
'deeputil==0.2.5',
'basescript==0.2.5',
'sqlitedict==1.5.0',
'plyvel==1.0.4',
],
package_dir={'diskdict': 'diskdict'},
packages=find_packages('.'),
include_package_data=True,
classifiers=[
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 2.7",
"Environment :: Console",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
],
test_suite='test.suitefn',
entry_points={
"console_scripts": [
Expand Down

0 comments on commit 84fcb39

Please sign in to comment.