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
pikachu_cry = make_pokemon("Pika") | |
pikachu_cry() |
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
pikachu = Pokemon("Pika") | |
pikachu.cry() |
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
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"}, |
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
"""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.
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
from joblib import Parallel, delayed | |
def multiprocess(inputs): | |
return Parallel(n_jobs=2)(delayed(f)(x) for x in inputs) |
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
demo_inputs = [randint(1, 100) for _ in range(10_000)] | |
single_thread(demo_inputs) | |
multiprocess(demo_inputs) | |
batch_multiprocess(demo_inputs) |
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
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): |
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
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): |
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
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) |
NewerOlder