Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dol/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,7 +1971,7 @@ def cache_vals(store=None, *, cache=dict):
), f"store should be a type, was a {type(store)}: {store}"

class CachedStore(store):
@wraps(store)
@wraps(store.__init__)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._cache = _mk_cache_instance(
Expand Down
46 changes: 23 additions & 23 deletions dol/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,8 +776,8 @@ def path_set(
>>> from collections import OrderedDict
>>> input_dict = {}
>>> path_set(input_dict, 'new.key', 42, new_mapping=OrderedDict)
>>> input_dict # doctest: +NORMALIZE_WHITESPACE
{'new': OrderedDict([('key', 42)])}
>>> input_dict # doctest: +ELLIPSIS
{'new': OrderedDict(...'key'...: 42...)}

"""
if isinstance(key_path, str) and sep is not None:
Expand Down Expand Up @@ -1660,7 +1660,7 @@ def identity(x):
# and have classmethods specialized for short-hand versions that use `name:regex` or
# `name:format`, ...
class KeyTemplate:
"""A class for parsing and generating keys based on a template.
r"""A class for parsing and generating keys based on a template.

Args:
template: A template string with fields to be extracted or filled in.
Expand Down Expand Up @@ -1900,7 +1900,7 @@ def str_to_dict(self, s: str) -> dict:
r"""Parses the input string and returns a dictionary of extracted values.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json',
... r'root/{}/v_{ver:03.0f:\d+}.json',
... from_str_funcs={'ver': int},
... )
>>> st.str_to_dict('root/life/v_30.json')
Expand All @@ -1920,7 +1920,7 @@ def dict_to_str(self, params: dict) -> str:
r"""Generates a string from the dictionary values based on the template.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.dict_to_str({'i01_': 'life', 'ver': 42})
'root/life/v_042.json'
Expand All @@ -1936,7 +1936,7 @@ def dict_to_tuple(self, params: dict) -> tuple:
r"""Generates a tuple from the dictionary values based on the template.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.str_to_tuple('root/life/v_42.json')
('life', 42)
Expand All @@ -1951,7 +1951,7 @@ def tuple_to_dict(self, param_vals: tuple) -> dict:
r"""Generates a dictionary from the tuple values based on the template.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.tuple_to_dict(('life', 42))
{'i01_': 'life', 'ver': 42}
Expand All @@ -1967,7 +1967,7 @@ def str_to_tuple(self, s: str) -> tuple:
r"""Parses the input string and returns a tuple of extracted values.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.str_to_tuple('root/life/v_42.json')
('life', 42)
Expand All @@ -1981,7 +1981,7 @@ def tuple_to_str(self, param_vals: tuple) -> str:
r"""Generates a string from the tuple values based on the template.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.tuple_to_str(('life', 42))
'root/life/v_042.json'
Expand All @@ -1995,7 +1995,7 @@ def str_to_single(self, s: str) -> Any:
r"""Parses the input string and returns a single value.

>>> st = KeyTemplate(
... 'root/life/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/life/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.str_to_single('root/life/v_42.json')
42
Expand All @@ -2009,7 +2009,7 @@ def single_to_str(self, k: Any) -> str:
r"""Generates a string from the single value based on the template.

>>> st = KeyTemplate(
... 'root/life/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/life/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.single_to_str(42)
'root/life/v_042.json'
Expand All @@ -2026,7 +2026,7 @@ def dict_to_namedtuple(
r"""Generates a namedtuple from the dictionary values based on the template.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> App = st.dict_to_namedtuple({'i01_': 'life', 'ver': 42})
>>> App
Expand All @@ -2041,7 +2041,7 @@ def namedtuple_to_dict(self, nt):
r"""Converts a namedtuple to a dictionary.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> App = st.dict_to_namedtuple({'i01_': 'life', 'ver': 42})
>>> st.namedtuple_to_dict(App)
Expand All @@ -2055,7 +2055,7 @@ def str_to_namedtuple(self, s: str):
r"""Converts a string to a namedtuple.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> App = st.str_to_namedtuple('root/life/v_042.json')
>>> App
Expand All @@ -2070,7 +2070,7 @@ def str_to_simple_str(self, s: str):
r"""Converts a string to a simple string (i.e. a simple character-delimited string).

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.str_to_simple_str('root/life/v_042.json')
'life,042'
Expand All @@ -2089,7 +2089,7 @@ def simple_str_to_tuple(self, ss: str):
r"""Converts a simple character-delimited string to a dict.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... simple_str_sep='-',
... )
>>> st.simple_str_to_tuple('life-042')
Expand All @@ -2114,7 +2114,7 @@ def simple_str_to_str(self, ss: str):
r"""Converts a simple character-delimited string to a string.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... simple_str_sep='-',
... )
>>> st.simple_str_to_str('life-042')
Expand All @@ -2129,7 +2129,7 @@ def match_str(self, s: str) -> bool:
Returns True iff the string matches the template.

>>> st = KeyTemplate(
... 'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... r'root/{}/v_{ver:03.0f:\d+}.json', from_str_funcs={'ver': int},
... )
>>> st.match_str('root/life/v_042.json')
True
Expand Down Expand Up @@ -2172,7 +2172,7 @@ def _extract_template_info(self, template):
These four values are used in the init to compute the parameters of the
instance.

>>> st = KeyTemplate('{:03.0f}/{name::\w+}')
>>> st = KeyTemplate(r'{:03.0f}/{name::\w+}')
>>> st.template
'{i01_}/{name}'
>>> st._fields
Expand Down Expand Up @@ -2238,13 +2238,13 @@ def _compile_regex(self, template, normalize_path=False):
'(?P<i01_>.*)\\.ext'
>>> KeyTemplate('{name}.ext')._regex.pattern
'(?P<name>.*)\\.ext'
>>> KeyTemplate('{::\w+}.ext')._regex.pattern
>>> KeyTemplate(r'{::\w+}.ext')._regex.pattern
'(?P<i01_>\\w+)\\.ext'
>>> KeyTemplate('{name::\w+}.ext')._regex.pattern
>>> KeyTemplate(r'{name::\w+}.ext')._regex.pattern
'(?P<name>\\w+)\\.ext'
>>> KeyTemplate('{:0.02f:\w+}.ext')._regex.pattern
>>> KeyTemplate(r'{:0.02f:\w+}.ext')._regex.pattern
'(?P<i01_>\\w+)\\.ext'
>>> KeyTemplate('{name:0.02f:\w+}.ext')._regex.pattern
>>> KeyTemplate(r'{name:0.02f:\w+}.ext')._regex.pattern
'(?P<name>\\w+)\\.ext'
"""

Expand Down
14 changes: 7 additions & 7 deletions dol/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,15 +1353,15 @@ def sig_or_none(cls, obj):
... ) # another easy one: This time, a type/class (which is callable, yes)
True

But here's where it get's interesting. `print`, a builtin, doesn't have a
But here's where it get's interesting. `map`, a builtin, doesn't have a
signature through inspect.signature.

>>> has_signature(print)
>>> has_signature(map)
False

But we do get one with robust_has_signature

>>> robust_has_signature(print)
>>> robust_has_signature(map)
True

"""
Expand Down Expand Up @@ -2963,7 +2963,7 @@ def extract_args_and_kwargs(
>>> Sig(foo).extract_args_and_kwargs(x=3, y=2)
Traceback (most recent call last):
...
TypeError: missing a required argument: 'w'
TypeError: missing a required keyword-only argument: 'w'

But if you specify `_allow_partial=True`...

Expand Down Expand Up @@ -3040,7 +3040,7 @@ def source_arguments(
>>> Sig(foo).source_arguments(x=3, y=2, extra="keywords", are="ignored")
Traceback (most recent call last):
...
TypeError: missing a required argument: 'w'
TypeError: missing a required keyword-only argument: 'w'

But if you specify `_allow_partial=True`...

Expand Down Expand Up @@ -3129,7 +3129,7 @@ def source_args_and_kwargs(
>>> Sig(foo).source_args_and_kwargs(x=3, y=2, extra="keywords", are="ignored")
Traceback (most recent call last):
...
TypeError: missing a required argument: 'w'
TypeError: missing a required keyword-only argument: 'w'

But if you specify `_allow_partial=True`...

Expand Down Expand Up @@ -3633,7 +3633,7 @@ def has_signature(obj, robust=False):
... )
... )
... )
2
3

If robust is set to True, `has_signature` will use `Sig` to get the signature,
so will return True in most cases.
Expand Down
5 changes: 1 addition & 4 deletions dol/sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,12 +504,9 @@ class CascadedStores(FanoutPersister):
>>> disk['g'] = 43

Now if you ask for `g`, it won't find it in cache, but will find it in `disk`
and return it. The reason you see the "Getting g from cache" message is because
the `stores` object first tries to get it in `cache`, and only if it doesn't find
it there, it tries to get it from `disk`.
and return it.

>>> stores['g']
Getting g from cache
Getting g from disk
43

Expand Down
24 changes: 12 additions & 12 deletions dol/tests/test_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,29 @@ def test_path_get():
def test_string_template_template_construction():
assert KeyTemplate("{}.ext").template == "{i01_}.ext"
assert KeyTemplate("{name}.ext").template == "{name}.ext"
assert KeyTemplate("{::\w+}.ext").template == "{i01_}.ext"
assert KeyTemplate("{name::\w+}.ext").template == "{name}.ext"
assert KeyTemplate("{name::\w+}.ext").template == "{name}.ext"
assert KeyTemplate(r"{::\w+}.ext").template == "{i01_}.ext"
assert KeyTemplate(r"{name::\w+}.ext").template == "{name}.ext"
assert KeyTemplate(r"{name::\w+}.ext").template == "{name}.ext"
assert KeyTemplate("{name:0.02f}.ext").template == "{name}.ext"
assert KeyTemplate("{name:0.02f:\w+}.ext").template == "{name}.ext"
assert KeyTemplate("{:0.02f:\w+}.ext").template == "{i01_}.ext"
assert KeyTemplate(r"{name:0.02f:\w+}.ext").template == "{name}.ext"
assert KeyTemplate(r"{:0.02f:\w+}.ext").template == "{i01_}.ext"


def test_string_template_regex():
assert KeyTemplate("{}.ext")._regex.pattern == "(?P<i01_>.*)\\.ext"
assert KeyTemplate("{name}.ext")._regex.pattern == "(?P<name>.*)\\.ext"
assert KeyTemplate("{::\w+}.ext")._regex.pattern == "(?P<i01_>\\w+)\\.ext"
assert KeyTemplate("{name::\w+}.ext")._regex.pattern == "(?P<name>\\w+)\\.ext"
assert KeyTemplate("{:0.02f:\w+}.ext")._regex.pattern == "(?P<i01_>\\w+)\\.ext"
assert KeyTemplate("{name:0.02f:\w+}.ext")._regex.pattern == "(?P<name>\\w+)\\.ext"
assert KeyTemplate("{}.ext")._regex.pattern == r"(?P<i01_>.*)\.ext"
assert KeyTemplate("{name}.ext")._regex.pattern == r"(?P<name>.*)\.ext"
assert KeyTemplate(r"{::\w+}.ext")._regex.pattern == r"(?P<i01_>\w+)\.ext"
assert KeyTemplate(r"{name::\w+}.ext")._regex.pattern == r"(?P<name>\w+)\.ext"
assert KeyTemplate(r"{:0.02f:\w+}.ext")._regex.pattern == r"(?P<i01_>\w+)\.ext"
assert KeyTemplate(r"{name:0.02f:\w+}.ext")._regex.pattern == r"(?P<name>\w+)\.ext"


def test_string_template_simple():
from dol.paths import KeyTemplate
from collections import namedtuple

st = KeyTemplate(
"root/{}/v_{version:03.0f:\d+}.json",
r"root/{}/v_{version:03.0f:\d+}.json",
from_str_funcs={"version": int},
)

Expand Down
2 changes: 1 addition & 1 deletion dol/trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,7 @@ def __delitem__(self, k):


def filter_regex(regex, *, return_search_func=False):
"""Make a filter that returns True if a string matches the given regex
r"""Make a filter that returns True if a string matches the given regex

>>> is_txt = filter_regex(r'.*\.txt')
>>> is_txt("test.txt")
Expand Down