Skip to content

Instantly share code, notes, and snippets.

View VictorDarvariu's full-sized avatar

Victor-Alexandru Darvariu VictorDarvariu

View GitHub Profile
@VictorDarvariu
VictorDarvariu / decorator_with_arguments.py
Created March 6, 2020 16:44
Simple example of a custom decorator with arguments in Python.
from collections import namedtuple
User = namedtuple('User', field_names=['username', 'role'])
current_user = User(username='Jeff', role='red')
def role_required(required_role):
def has_role(current_user):
return current_user.role == required_role
def role_decorator(func):
@VictorDarvariu
VictorDarvariu / np_seed_contextmanager.py
Created February 5, 2020 10:11
Example of managing NumPy seed with contextmanager
import contextlib
import numpy as np
# For info on contextlib, see https://docs.python.org/3/library/contextlib.html
@contextlib.contextmanager
def local_seed(seed):
state = np.random.get_state()
np.random.seed(seed)
try:
yield