mirror of
https://github.com/romanz/amodem.git
synced 2026-04-19 12:46:00 +08:00
util: add simple memoization decorator
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import io
|
import io
|
||||||
|
|
||||||
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from .. import util
|
from .. import util
|
||||||
@@ -101,3 +102,16 @@ def test_reader():
|
|||||||
|
|
||||||
def test_setup_logging():
|
def test_setup_logging():
|
||||||
util.setup_logging(verbosity=10)
|
util.setup_logging(verbosity=10)
|
||||||
|
|
||||||
|
|
||||||
|
def test_memoize():
|
||||||
|
f = mock.Mock(side_effect=lambda x: x)
|
||||||
|
|
||||||
|
def func(x):
|
||||||
|
# mock.Mock doesn't work with functools.wraps()
|
||||||
|
return f(x)
|
||||||
|
|
||||||
|
g = util.memoize(func)
|
||||||
|
assert g(1) == g(1)
|
||||||
|
assert g(1) != g(2)
|
||||||
|
assert f.mock_calls == [mock.call(1), mock.call(2)]
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
"""Various I/O and serialization utilities."""
|
"""Various I/O and serialization utilities."""
|
||||||
import binascii
|
import binascii
|
||||||
import contextlib
|
import contextlib
|
||||||
|
import functools
|
||||||
import io
|
import io
|
||||||
import logging
|
import logging
|
||||||
import struct
|
import struct
|
||||||
@@ -185,3 +186,21 @@ def setup_logging(verbosity, **kwargs):
|
|||||||
levels = [logging.WARNING, logging.INFO, logging.DEBUG]
|
levels = [logging.WARNING, logging.INFO, logging.DEBUG]
|
||||||
level = levels[min(verbosity, len(levels) - 1)]
|
level = levels[min(verbosity, len(levels) - 1)]
|
||||||
logging.basicConfig(format=fmt, level=level, **kwargs)
|
logging.basicConfig(format=fmt, level=level, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def memoize(func):
|
||||||
|
"""Simple caching decorator."""
|
||||||
|
cache = {}
|
||||||
|
|
||||||
|
@functools.wraps(func)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
"""Caching wrapper."""
|
||||||
|
key = (args, tuple(sorted(kwargs.items())))
|
||||||
|
if key in cache:
|
||||||
|
return cache[key]
|
||||||
|
else:
|
||||||
|
result = func(*args, **kwargs)
|
||||||
|
cache[key] = result
|
||||||
|
return result
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|||||||
Reference in New Issue
Block a user