-
Notifications
You must be signed in to change notification settings - Fork 18
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
Showing
16 changed files
with
171 additions
and
0 deletions.
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,15 @@ | ||
from functools import wraps | ||
|
||
|
||
def cached(fn): | ||
cache = {} # args -> value | ||
|
||
@wraps(fn) | ||
def wrapper(*args, **kw): | ||
if args in cache: | ||
return cache[args] | ||
|
||
value = cache[args] = fn(*args) | ||
return value | ||
|
||
return wrapper |
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,7 @@ | ||
# Caching | ||
|
||
Save a computation result and using it later. AKA memoization. | ||
|
||
You trade memory consumption for performance. | ||
|
||
Works on "pure" functions that don't have side effects. |
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 @@ | ||
13 |
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,6 @@ | ||
import os | ||
|
||
!kitty +icat book-covers.png | ||
width = os.get_terminal_size().columns | ||
url = 'https://pragprog.com/search/?q=miki+tebeka' | ||
print(f'{url:^{width}}') |
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,3 @@ | ||
There's two hard problems in computer science: We only have one joke and it's not funny. | ||
|
||
- Phillip Scott Bowden |
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,3 @@ | ||
# Caching in Python | ||
[Miki Tebeka](mailto:[email protected]) | ||
|
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,14 @@ | ||
# Use Libraries | ||
|
||
|
||
The "Not Invented Here" (NIH) Syndrome is the tendency to reject ideas that come from "outside". | ||
And unfortunately, "outside" can still apply to solutions that come from other teams within the organization. | ||
|
||
- functools.lru_cache | ||
- https://docs.python.org/3/library/functools.html#functools.lru_cache | ||
- joblib.Memory | ||
- https://joblib.readthedocs.io/en/latest/memory.html#memory | ||
- Redis/Memcached | ||
- https://redis.io/ | ||
- https://memcached.org/ | ||
|
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,39 @@ | ||
current = 'current.txt' | ||
|
||
try: | ||
with open(current) as fp: | ||
i = int(fp.read()) | ||
except OSError: | ||
i = 0 | ||
|
||
!clear | ||
if i == 0: | ||
!glow caching.md | ||
elif i == 1: | ||
!source-highlight -n -f esc -s python -i user.py | ||
%run user.py | ||
elif i == 2: | ||
!source-highlight -n -f esc -s python -i cache.py | ||
elif i == 3: | ||
!source-highlight -n -f esc -s python -i user_cached.py | ||
%run user_cached.py | ||
elif i == 4: | ||
!glow two.md | ||
elif i == 5: | ||
!glow three.md | ||
elif i == 6: | ||
!glow four.md | ||
elif i == 7: | ||
!glow libs.md | ||
elif i == 8: | ||
!source-highlight -n -f esc -s python -i user_lru.py | ||
%run user_lru.py | ||
elif i == 9: | ||
!source-highlight -n -f esc -s python -i user_mem.py | ||
%run user_mem.py | ||
else: | ||
echo 'Thank You!' | cowsay | lolcat -a -d 3 | ||
|
||
|
||
with open(current, 'w') as out: | ||
print(i+1, file=out, end='') |
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,5 @@ | ||
%run setup.py | ||
|
||
%timeit get_user('bruce') | ||
|
||
In cache show get_user(['bruce']) |
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,5 @@ | ||
%alias_magic -p next.ipy next %run | ||
%alias_magic -p end.ipy end %run | ||
!rm -f current.txt | ||
!clear | ||
!glow header.md |
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,3 @@ | ||
There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors. | ||
|
||
- Leon Bambrick |
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,3 @@ | ||
There are only two hard things in Computer Science: cache invalidation and naming things. | ||
|
||
- Phil Karlton |
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,14 @@ | ||
from dataclasses import dataclass | ||
from time import sleep | ||
|
||
@dataclass | ||
class User: | ||
login: str | ||
name: str | ||
roles: list[str] | ||
|
||
|
||
def get_user(login): | ||
sleep(0.1) # simulate DB access | ||
return User(login, 'Bruce', ['ADMIN']) | ||
|
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,17 @@ | ||
from dataclasses import dataclass | ||
from time import sleep | ||
|
||
from cache import cached | ||
|
||
@dataclass | ||
class User: | ||
login: str | ||
name: str | ||
roles: list[str] | ||
|
||
|
||
@cached | ||
def get_user(login): | ||
sleep(0.1) # simulate DB access | ||
return User(login, 'Bruce', ['ADMIN']) | ||
|
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,17 @@ | ||
from dataclasses import dataclass | ||
from time import sleep | ||
|
||
from functools import lru_cache | ||
|
||
@dataclass | ||
class User: | ||
login: str | ||
name: str | ||
roles: list[str] | ||
|
||
|
||
@lru_cache(1024) | ||
def get_user(login): | ||
sleep(0.1) # simulate DB access | ||
return User(login, 'Bruce', ['ADMIN']) | ||
|
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,19 @@ | ||
from dataclasses import dataclass | ||
from time import sleep | ||
|
||
from joblib import Memory | ||
|
||
memory = Memory('/tmp/users', verbose=False) | ||
|
||
@dataclass | ||
class User: | ||
login: str | ||
name: str | ||
roles: list[str] | ||
|
||
|
||
@memory.cache | ||
def get_user(login): | ||
sleep(0.1) # simulate DB access | ||
return User(login, 'Bruce', ['ADMIN']) | ||
|