Skip to content
This repository was archived by the owner on Feb 14, 2020. It is now read-only.

Commit f741ec4

Browse files
committed
Merge pull request #3 from rhyolight/add-caching
Add caching
2 parents 063256b + 2f764a8 commit f741ec4

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
cache
12
build
23
dist
34
pycept.egg-info

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ This is a *very* minimal HTTP client library for the [CEPT API](https://cept.3sc
2727
catBitmap['positions']
2828
)
2929

30+
### Caching
31+
32+
To prevent duplicate requests to the CEPT API, pycept will cache SDR responses by default to `/tmp/pycept`. You can provide your own cache directory location by specifying a `cache_dir` value to the constructor:
33+
34+
pycept.Cept("your_app_id", "your_app_key", cache_dir="./my-cache")
35+
3036
### Run Tests
3137

3238
nosetests

pycept/cept.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,56 @@
2020
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121
# THE SOFTWARE.
2222

23-
import requests
23+
import os
2424
import json
25+
import requests
2526

2627
DEFAULT_BASE_URL = "http://api.cept.at"
2728
DEFAULT_VERSION = "v1"
29+
DEFAULT_CACHE_DIR = "/tmp/pycept"
2830

2931
class Cept(object):
3032
"""
3133
Main class for the Cept API.
3234
"""
3335

34-
def __init__(self, app_id, app_key, base_url=DEFAULT_BASE_URL, version=DEFAULT_VERSION):
36+
def __init__(self, app_id, app_key, base_url=DEFAULT_BASE_URL,
37+
version=DEFAULT_VERSION, cache_dir=DEFAULT_CACHE_DIR):
3538
self.app_id = app_id
3639
self.app_key = app_key
3740
self.api_url = "%s/%s" % (base_url, version)
41+
# Create the cache directory if necessary.
42+
if not os.path.exists(cache_dir):
43+
os.mkdir(cache_dir)
44+
self.cache_dir = cache_dir
45+
3846

3947

4048
def getBitmap(self, term):
4149
urlParams = self._buildUrlParams()
4250
urlParams['term'] = term
4351
url = "%s/term2bitmap" % (self.api_url,)
44-
response = requests.get(url, params=urlParams)
45-
return response.json['bitmap']
52+
# Create a cache location for each term, where it will either be read in from
53+
# or cached within if we have to go to the CEPT API to get the SDR.
54+
cache_file = os.path.join(self.cache_dir, term + '.json')
55+
# Get it from the cache if it's there.
56+
if os.path.exists(cache_file):
57+
cached_sdr = json.loads(open(cache_file).read())
58+
# Get it from CEPT API if it's not cached.
59+
else:
60+
print '\tfetching %s from CEPT API' % term
61+
response = requests.get(url, params=urlParams)
62+
cached_sdr = response.json['bitmap']
63+
# attach the sparcity for reference
64+
total = float(cached_sdr['width']) * float(cached_sdr['height'])
65+
on = len(cached_sdr['positions'])
66+
sparcity = round((on / total) * 100)
67+
cached_sdr['sparcity'] = sparcity
68+
# write to cache
69+
with open(cache_file, 'w') as f:
70+
f.write(json.dumps(cached_sdr))
71+
72+
return cached_sdr
4673

4774

4875
def getSdr(self, term):

0 commit comments

Comments
 (0)