Skip to content

Commit 2ecaed9

Browse files
committed
Move memoized to utils
1 parent 067759e commit 2ecaed9

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

lib/markdown2.py

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,15 @@
112112
from collections import defaultdict
113113

114114
from lib.errors import MarkdownError
115-
from lib.utils import slugify, calculate_toc_html, curry, regex_from_encoded_pattern, dedentlines, dedent
115+
from lib.utils import (
116+
slugify,
117+
calculate_toc_html,
118+
curry,
119+
regex_from_encoded_pattern,
120+
dedentlines,
121+
dedent,
122+
memoized,
123+
)
116124

117125
# ---- globals
118126

@@ -2471,33 +2479,6 @@ class UnicodeWithAttrs(str):
24712479
toc_html = None
24722480

24732481

2474-
class _memoized(object):
2475-
"""Decorator that caches a function's return value each time it is called.
2476-
If called later with the same arguments, the cached value is returned, and
2477-
not re-evaluated.
2478-
2479-
http://wiki.python.org/moin/PythonDecoratorLibrary
2480-
"""
2481-
def __init__(self, func):
2482-
self.func = func
2483-
self.cache = {}
2484-
2485-
def __call__(self, *args):
2486-
try:
2487-
return self.cache[args]
2488-
except KeyError:
2489-
self.cache[args] = value = self.func(*args)
2490-
return value
2491-
except TypeError:
2492-
# uncachable -- for instance, passing a list as an argument.
2493-
# Better to not cache than to blow up entirely.
2494-
return self.func(*args)
2495-
2496-
def __repr__(self):
2497-
"""Return the function's docstring."""
2498-
return self.func.__doc__
2499-
2500-
25012482
def _xml_oneliner_re_from_tab_width(tab_width):
25022483
"""Standalone XML processing instruction regex."""
25032484
return re.compile(r"""
@@ -2517,7 +2498,7 @@ def _xml_oneliner_re_from_tab_width(tab_width):
25172498
(?=\n{2,}|\Z) # followed by a blank line or end of document
25182499
)
25192500
""" % (tab_width - 1), re.X)
2520-
_xml_oneliner_re_from_tab_width = _memoized(_xml_oneliner_re_from_tab_width)
2501+
_xml_oneliner_re_from_tab_width = memoized(_xml_oneliner_re_from_tab_width)
25212502

25222503

25232504
def _hr_tag_re_from_tab_width(tab_width):
@@ -2537,7 +2518,7 @@ def _hr_tag_re_from_tab_width(tab_width):
25372518
(?=\n{2,}|\Z) # followed by a blank line or end of document
25382519
)
25392520
""" % (tab_width - 1), re.X)
2540-
_hr_tag_re_from_tab_width = _memoized(_hr_tag_re_from_tab_width)
2521+
_hr_tag_re_from_tab_width = memoized(_hr_tag_re_from_tab_width)
25412522

25422523

25432524
def _xml_escape_attr(attr, skip_single_quote=True):

lib/utils.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,31 @@ def dedent(text, tabsize=8, skip_first_line=False):
185185
"""
186186
lines = text.splitlines(1)
187187
dedentlines(lines, tabsize=tabsize, skip_first_line=skip_first_line)
188-
return ''.join(lines)
188+
return ''.join(lines)
189+
190+
191+
class memoized(object):
192+
"""Decorator that caches a function's return value each time it is called.
193+
If called later with the same arguments, the cached value is returned, and
194+
not re-evaluated.
195+
196+
http://wiki.python.org/moin/PythonDecoratorLibrary
197+
"""
198+
def __init__(self, func):
199+
self.func = func
200+
self.cache = {}
201+
202+
def __call__(self, *args):
203+
try:
204+
return self.cache[args]
205+
except KeyError:
206+
self.cache[args] = value = self.func(*args)
207+
return value
208+
except TypeError:
209+
# uncachable -- for instance, passing a list as an argument.
210+
# Better to not cache than to blow up entirely.
211+
return self.func(*args)
212+
213+
def __repr__(self):
214+
"""Return the function's docstring."""
215+
return self.func.__doc__

0 commit comments

Comments
 (0)