Skip to content

Commit

Permalink
Fixing the combination of "**" and re, and adding re to readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
nixjdm committed Apr 5, 2023
1 parent 32dd1c0 commit 6bd9fea
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ KeyError: 'a'
Deep Collections supports the following matching styles:

- glob
- regex (_built in soon_)
- none (_built in soon_)
- regex
- equality
- hash
- glob+regex
- custom (_built in soon_)

This can be set with many functions by passing e.g. `match_with="regex"`.
Expand All @@ -100,6 +102,10 @@ To abandon all matching styles and traverse paths as quickly as possible, use `g

Any given path element is matched with `fnmatchcase` from [the Python stdlib](https://docs.python.org/3/library/fnmatch.html#fnmatch.fnmatchcase). This style is used in the above examples.

#### Matching Style: Regex

Any given path element is matched with `re.compile().match()` from [the Python stdlib](https://docs.python.org/3/library/re.html).

### DeepCollection object API

DeepCollections are instantiated as a normal class, optionally with a given initial collection as an arguement.
Expand Down
2 changes: 1 addition & 1 deletion deep_collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def resolve_path(obj, path, *args, match_with="glob", recursive_match_all=True,
path = _simplify_double_splats(path)

if recursive_match_all and "**" in path:
yield from _resolve_double_splat(obj, path, *args, **kwargs)
yield from _resolve_double_splat(obj, path, *args, match_with=match_with, **kwargs)
elif path:
first_step = path[0]
path_remainder = path[1:]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "deep_collections"
version = "0.4.1"
version = "0.4.2"
authors = ["Joseph Nix <nixjdm@terminallabs.com>"]
classifiers=[
'Intended Audience :: Developers',
Expand Down
28 changes: 25 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ def __hash__(self):
({fun_five: 1}, -5, {"match_with": "hash"}, 1),
({"foo!": 1}, "foo!", {"match_with": "equality"}, 1),
({1: 1}, 0, {"match_with": "equality"}, []),
({"accccb": 1}, "a[bcd]*b", {"match_with": "regex"}, 1),
({'alias': 'foo'}, '^a', {"match_with": "regex"}, "foo"),
({'alias': 'foo'}, '^a...s$', {"match_with": "regex"}, "foo"),
({"not a hit": {'alias': 'foo'}}, ["**", '^a'], {"match_with": "regex"}, "foo"),
({"xd": 1}, "xd", {"match_with": "regex"}, 1),
({"xd": 1}, "?d", {"match_with": "regex"}, re.error),
({"xd": 1}, "?d", {"match_with": "regex"}, re.error), # glob, not valid regex
({"xd": 1}, "?d", {}, 1),
({"xd": 1}, "?d", {}, 1),
({"xd": 1}, "Xd", {"case_sensitive": False}, 1),
(
{"b": {"accccb": {"xd": {"e": 0}}}},
["**", "a[bcd]*b", "?d", "e"],
["**", "^a", "?d", "e"],
{"match_with": "glob+regex"},
0,
),
Expand Down Expand Up @@ -102,6 +104,26 @@ def test_paths_to_key(obj, key, result):
else:
assert list(paths_to_key(obj, key)) == result

# Basic tests of various match styles
@pytest.mark.parametrize(
"obj, key, kwargs, result",
[
({'alias': 'foo'}, 'a*', {"match_with": "glob"}, [['alias']]),
({'alias': 'foo'}, '^a', {"match_with": "regex"}, [['alias']]),
({'alias': 'foo'}, '^a', {"match_with": "equality"}, []),
({'alias': 'foo'}, 'alias', {"match_with": "equality"}, [['alias']]),
({'alias': 'foo'}, '?lias', {"match_with": "regex"}, re.error),
({'alias': {'blias': 'foo'}}, '^a', {"match_with": "glob+regex"}, [['alias']]),
({'alias': {'blias': 'foo'}}, '*s', {"match_with": "glob+regex"}, [['alias', 'blias'], ['alias']]),
({1: 'foo'}, 1, {"match_with": "hash"}, [[1]]),
],
)
def test_paths_to_key_styles(obj, key, kwargs, result):
if inspect.isclass(result) and issubclass(result, Exception):
with pytest.raises(result):
list(paths_to_key(obj, key, **kwargs))
else:
assert list(paths_to_key(obj, key, **kwargs)) == result

@pytest.mark.parametrize(
"obj, value, result",
Expand Down

0 comments on commit 6bd9fea

Please sign in to comment.