Skip to content

Commit

Permalink
Add injection
Browse files Browse the repository at this point in the history
A new function, `injection` for dependency injection of globals (for classes and functions that weren't designed for dependency injection).
  • Loading branch information
norvig authored Mar 9, 2018
1 parent a8ccb30 commit aa6664f
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,17 @@ def vector_clip(vector, lowest, highest):
# ______________________________________________________________________________
# Misc Functions

class injection():
"""Dependency injection of temporary values for global functions/classes/etc.
E.g., `with injection(DataBase=MockDataBase): ...`"""
def __init__(self, **kwds):
self.new = kwds
def __enter__(self):
self.old = {v: globals()[v] for v in self.new}
globals().update(self.new)
def __exit__(self, type, value, traceback):
globals().update(self.old)


def memoize(fn, slot=None, maxsize=32):
"""Memoize fn: make it remember the computed value for any argument list.
Expand Down

0 comments on commit aa6664f

Please sign in to comment.