Skip to content

Commit

Permalink
#8 Make code PEP-8 compatible. Run flake8 in travis
Browse files Browse the repository at this point in the history
  • Loading branch information
svetlin-mladenov committed May 20, 2017
1 parent 0a61d05 commit 4b3dca1
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 160
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ python:
install:
- pip install -r requirements-dev.txt
- pip install .
script: pytest
script:
- pytest
- flake8
2 changes: 2 additions & 0 deletions fcache/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from .fcache import fcache

__all__ = ["fcache"]
7 changes: 7 additions & 0 deletions fcache/fcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

GLOBAL_CACHE = FileCache('.fcache')


def get_global_cache():
global GLOBAL_CACHE
return GLOBAL_CACHE


def get_func_fullname(f, bounded_obj):
f_name = getattr(f, '__module__', '') + '.'
if bounded_obj is not None:
Expand All @@ -21,6 +23,7 @@ def get_func_fullname(f, bounded_obj):

def fcache(f):
cache = get_global_cache()

def decorated(*args, **kwargs):
if f.__closure__:
f_closure_values = tuple(map(lambda c: c.cell_contents, f.__closure__))
Expand All @@ -38,8 +41,12 @@ def decorated(*args, **kwargs):
cache[call_hash] = ret_val
return ret_val
return decorated


# XXX replace fcache by a object
fcache.clear_at_exit = False


@atexit.register
def maybe_clear_at_exit():
if fcache.clear_at_exit:
Expand Down
7 changes: 5 additions & 2 deletions fcache/file_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ def __init__(self, prev, next, key):
self.next = next
self.key = key


# TODO use slots
class LruList:
def __init__(self, next):
self.next = next


class FileCache:
def __init__(self, cache_dir, capacity=80*1024*1024*1024):
self._cache_dir = cache_dir
Expand All @@ -27,7 +29,8 @@ def __init__(self, cache_dir, capacity=80*1024*1024*1024):
disk_files = []
for cached_pickle in os.listdir(cache_dir):
key, ext = os.path.splitext(cached_pickle)
if ext != '.pkl': continue
if ext != '.pkl':
continue
key = int(key)
full_fn = os.path.join(cache_dir, cached_pickle)
disk_files.append((full_fn, os.stat(full_fn), key))
Expand Down Expand Up @@ -75,7 +78,7 @@ def __contains__(self, key):

def pop_lru(self):
lru = self._lru_items.next
#del self[lru.key]
# del self[lru.key]
del self._cached_keys[lru.key]
size = os.stat(self.cache_fn(lru.key)).st_size
os.unlink(self.cache_fn(lru.key))
Expand Down
1 change: 0 additions & 1 deletion fcache/hashing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import hashlib
import pickle
import io
from collections import OrderedDict


class StablePickler(pickle.Pickler):
Expand Down
16 changes: 14 additions & 2 deletions fcache/tests/test_caching_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

fcache.clear_at_exit = True


class Num:
def __init__(self, n):
self.n = n
Expand All @@ -16,14 +17,18 @@ def __iadd__(self, other):
self.n += other.n
return self


# Test global function

global_function_call_counter = 0


def global_function(n):
global global_function_call_counter
global_function_call_counter += 1
return n * n


def test_global_function():
cached = fcache(global_function)
exp_5 = global_function(5)
Expand Down Expand Up @@ -54,7 +59,7 @@ def loc_func(n):

# Test lambda
def test_lambda():
lamb = lambda x: x
lamb = lambda x: x # NOQA
cached = fcache(lamb)
for i in range(5):
for n in range(5):
Expand All @@ -67,6 +72,7 @@ def test_lambda():
else:
assert f_input is not cached_res


# Test redifining a function
def test_redefining_a_function():
@fcache
Expand Down Expand Up @@ -94,13 +100,16 @@ def fun(n, add=1):

# Test closure
def test_closure():

def outer():
n = Num(0)

@fcache
def mut():
nonlocal n
n += Num(1)
return n

@fcache
def get():
return n
Expand All @@ -112,7 +121,7 @@ def get():
assert mut() == Num(3)
get_res = get()
cached_res = get()
assert get_res == Num(3)
assert get_res == Num(3)
assert cached_res == get_res
assert cached_res is not get_res

Expand All @@ -121,11 +130,14 @@ def get():
class SimpleClass:
def __init__(self):
self.n = 0

def inc(self):
self.n += 1

def get(self):
return self.n


def test_bound_method():
obj = SimpleClass()
fun = fcache(obj.get)
Expand Down
3 changes: 3 additions & 0 deletions fcache/tests/test_file_cache.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from fcache.file_cache import FileCache


def test_caching_values(tmpdir):
cache = FileCache(str(tmpdir))
kvs = [(1, 1), (2, 20), (3, 300)]
Expand Down Expand Up @@ -28,6 +29,7 @@ def test_cache_clearing(tmpdir):
cache.clear()
assert len(cache) == 0


def test_cache_limit(tmpdir):
record_size = 512
cache = FileCache(str(tmpdir), capacity=int(2.5*record_size))
Expand All @@ -45,6 +47,7 @@ def test_cache_limit(tmpdir):
assert 1 in cache
assert 3 in cache


def test_cache_limit_restore_from_disk(tmpdir):
record_size = 512
cache = FileCache(str(tmpdir), capacity=int(2.5*record_size))
Expand Down
4 changes: 2 additions & 2 deletions fcache/tests/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def get_hash(repr_):
env = os.environ
cmd_line = [sys.executable, '-c', get_hash_command(repr_)]
p = subprocess.Popen(cmd_line, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
env=env)
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
env=env)
out, err = p.communicate()
return int(out.strip())

Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest==3.0.5
flake8
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@
install_requires=[
],
)

0 comments on commit 4b3dca1

Please sign in to comment.