Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1046dc
commit 165afd3
Showing
3 changed files
with
47 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from collections import defaultdict | ||
from datetime import datetime | ||
import pickle | ||
from random import choice | ||
|
||
CACHE = 'data' | ||
NAMES = ('bob', 'julian', 'martin', 'dante', 'snake') | ||
TIMES = range(15, 61, 15) | ||
|
||
|
||
def gen_random_entry(): | ||
while True: | ||
yield datetime.now(), choice(NAMES), choice(TIMES) | ||
|
||
|
||
class Cache: | ||
|
||
def __enter__(self): | ||
try: | ||
self.cache = pickle.load(open(CACHE, "rb")) | ||
except FileNotFoundError: | ||
self.cache = defaultdict(list) | ||
return self.cache | ||
|
||
def __exit__(self, exc_type, exc_val, exc_tb): | ||
pickle.dump(self.cache, open(CACHE, "wb")) | ||
|
||
|
||
if __name__ == '__main__': | ||
it = gen_random_entry() | ||
|
||
with Cache() as wl: | ||
for i in range(5): | ||
d, n, t = next(it) | ||
wl[n].append((d, t)) | ||
|
||
print('Cached work so far') | ||
print('(this will grow each time program is run)') | ||
print() | ||
col1, col2 = ('NAME', 'MINUTES') | ||
print('{:<10}: {}'.format(col1, col2)) | ||
with Cache() as wl: | ||
for name, work in sorted(wl.items()): | ||
total = sum(w[1] for w in work) # TODO: use namedtuple | ||
print('{:<10}: {}'.format(name, total)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters