Skip to content

Instantly share code, notes, and snippets.

View boechat107's full-sized avatar

Andre Boechat boechat107

View GitHub Profile
@boechat107
boechat107 / calls_to_fn_counter.py
Created October 10, 2019 14:01
Counting the number of times a function is called in a test (pytest)
import random
class MutableCounter:
"""
Used by "count_calls".
"""
n = 0
def inc(self):
self.n += 1
@boechat107
boechat107 / fp_with_types.py
Last active January 3, 2020 14:36
Functional Programming with type annotation in Python
from typing import Callable, Iterable, TypeVar, Iterator, Optional
A = TypeVar("A")
def some(pred: Callable[[A], bool], seq: Iterable[A]) -> Optional[A]:
"""
Returns the first item in "seq" that satisfies "pred".
Examples:
@boechat107
boechat107 / cache_with_schedule.py
Last active March 22, 2020 17:58
A Python decorator to cache functions and clear the cache as scheduled
from datetime import datetime as dt
from croniter import croniter
from functools import wraps
from typing import Dict, Any
def cache_with_schedule(schedule: str):
"""
Decorator to cache the results of a function "f(x)" and clear the cache as
scheduled.