Skip to content

Instantly share code, notes, and snippets.

View DougAF's full-sized avatar
💭
Always Learning!

Douglas Franklin DougAF

💭
Always Learning!
View GitHub Profile
@DougAF
DougAF / example.py
Created July 6, 2021 22:30
poke article exa
pikachu_cry = make_pokemon("Pika")
pikachu_cry()
@DougAF
DougAF / example.py
Created July 6, 2021 22:29
poke article
pikachu = Pokemon("Pika")
pikachu.cry()
def citrus_press_factory(press_type):
"""Creates a citrus press."""
press_types = {"lime", "lemon", "orange", "grapefruit"}
if not press_type in press_types:
raise ValueError(f"press_type must be one of {press_types}")
compatible_fruit_types_lookup = {
"lime": {"lime", "lemon"},
"lemon": {"lime", "lemon"},
@DougAF
DougAF / poke_class.py
Created June 26, 2021 15:40
Pokemon class for article
"""You can use currying to replicate the behavior of some classes."""
class Pokemon:
def __init__(self, sound):
self.sound = sound
def cry(self):
return self.sound
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from joblib import Parallel, delayed
def multiprocess(inputs):
return Parallel(n_jobs=2)(delayed(f)(x) for x in inputs)
@DougAF
DougAF / compare_calls.py
Created May 14, 2021 17:16
Calling demo inputs pattern
demo_inputs = [randint(1, 100) for _ in range(10_000)]
single_thread(demo_inputs)
multiprocess(demo_inputs)
batch_multiprocess(demo_inputs)
@DougAF
DougAF / batch_multiprocess_cached.py
Created May 10, 2021 21:04
batch_multiprocess_cached.py
def batch_multiprocess_caches(inputs):
"""
Compute multiprocess with one batch and one cache per process.
"""
len_inputs = len(inputs)
n_batches = multiprocessing.cpu_count()
batch_size = len_inputs // n_batches
batches = []
for i in range(n_batches):
@DougAF
DougAF / batch_multiprocess.py
Created May 10, 2021 21:03
batch_multiprocess.py
def batch_multiprocess(inputs):
"""
Compute multiprocess with one batch per process.
"""
len_inputs = len(inputs)
n_batches = multiprocessing.cpu_count()
batch_size = len_inputs // n_batches
batches = []
for i in range(n_batches):
@DougAF
DougAF / multiprocess_cached.py
Created May 10, 2021 21:03
multiprocess_cached.py
def multiprocess_with_cache(inputs):
"""
Compute multiprocess with one memory mapped cache.
"""
location = "./cachedir"
memory = Memory(location, verbose=0)
f_memoized = memory.cache(f)
return Parallel(n_jobs=-1)(delayed(f_memoized)(x) for x in inputs)