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 / multiprocess.py
Created May 10, 2021 21:02
multi process
def multiprocess(inputs):
"""
Compute multiprocess.
"""
return Parallel(n_jobs=-1)(delayed(f)(x) for x in inputs)
@DougAF
DougAF / singlethread_cached.py
Created May 10, 2021 21:01
single thread w/cache
def single_thread_with_cache(inputs):
"""
Compute single threaded with cache.
"""
f_memoized = cache(f)
return [f_memoized(x) for x in inputs]
@DougAF
DougAF / singlethread.py
Created May 10, 2021 21:01
singlethread
def single_thread(inputs):
"""
Compute single threaded.
"""
return [f(x) for x in inputs]
@DougAF
DougAF / imports.py
Created May 10, 2021 20:35
imports for functions article
import multiprocessing
from functools import cache
from random import randint
from joblib import Memory, Parallel, delayed
from resc.functions import factors
from resc.timer import timefunc
def memoize(func):
"""Decorates a function to implement a memo.
A simpler, less optimized version of functools.cache."""
memo = {}
@functools.wraps(func)
def memorize_closure(*args, **kwargs):
key = _make_key(args, kwargs)
def _make_key(args, kwargs):
"""Creates a hashable key. A simplified version of functools._make_key."""
# create a key for the memo from args and kwargs
key = args
if kwargs:
# marks the start of the keyword arguments in key
key += (object(),)
for item in kwargs.items():
@DougAF
DougAF / memo_class_constructor.py
Created May 3, 2021 19:11
Class constructor for medium article
class MemoizedFunction:
"""Takes a function and returns a callable that is a memoized version of that function."""
def __init__(self, func):
self.func = func
self.cache = dict()
self.cache_hits = 0
self.n_calls = 0
def __call__(self, *args, **kwargs):
@DougAF
DougAF / memo_ex.py
Created April 30, 2021 16:20
Memoized example for medium
import time
memo_cache = {}
def slow_func(num):
if num in memo_cache:
return memo_cache[num]
print(f"Calculating {num} squared")
@DougAF
DougAF / non_memo_slow_calc.py
Last active April 30, 2021 16:19
Example to introduce memorization for medium
import time
def slow_func(num):
print(f"Calculating {num} squared")
time.sleep(1)
result = num * num
return result
@DougAF
DougAF / fibbo_recu.py
Created April 26, 2021 19:43
recursive fibbo calc
def fibo_rec(num):
"""Recursive Fibo."""
return num if num <= 1 else fibo_rec(num - 1) + fibo_rec(num - 2)