dotted-notation 0.55.3__tar.gz → 0.55.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/CHANGELOG.md +28 -0
- {dotted_notation-0.55.3/dotted_notation.egg-info → dotted_notation-0.55.4}/PKG-INFO +36 -1
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/README.md +35 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/access.py +12 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/api.py +69 -67
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/base.py +35 -10
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/grammar.py +7 -1
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/groups.py +8 -8
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/recursive.py +3 -5
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/wrappers.py +21 -2
- {dotted_notation-0.55.3 → dotted_notation-0.55.4/dotted_notation.egg-info}/PKG-INFO +36 -1
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/pyproject.toml +1 -1
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/LICENSE +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/MANIFEST.in +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/__init__.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/__main__.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/access.pxd +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/base.pxd +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/cli/__init__.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/cli/_compat.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/cli/formats.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/cli/main.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/containers.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/engine.pxd +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/engine.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/filters.pxd +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/filters.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/groups.pxd +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/matchers.pxd +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/matchers.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/native.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/predicates.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/recursive.pxd +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/results.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/transforms.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/translators/__init__.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/translators/core.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/translators/spark.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/translators/spark_pyspark.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/translators/sql.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/translators/sql_pg.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/utils.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/utypes.py +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted/wrappers.pxd +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted_notation.egg-info/SOURCES.txt +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted_notation.egg-info/dependency_links.txt +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted_notation.egg-info/entry_points.txt +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted_notation.egg-info/requires.txt +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted_notation.egg-info/top_level.txt +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/setup.cfg +0 -0
- {dotted_notation-0.55.3 → dotted_notation-0.55.4}/setup.py +0 -0
|
@@ -3,6 +3,34 @@
|
|
|
3
3
|
All notable changes to `dotted` are recorded here. Versions prior to
|
|
4
4
|
the ones listed are omitted — browse git history for earlier entries.
|
|
5
5
|
|
|
6
|
+
## [0.55.4]
|
|
7
|
+
|
|
8
|
+
### Changed
|
|
9
|
+
- A `match(..., groups=...)` capture is the path's segment as the path
|
|
10
|
+
writes it, accessor, filters, value guard and transforms included:
|
|
11
|
+
`match('*[*].*', 'lots[*&a=1].d', groups=True)` captures
|
|
12
|
+
`('lots', '[*&a=1]', 'd')`, and `[3]` captures `'[3]'`, not `3`.
|
|
13
|
+
A filtered pattern segment, `*&id=*`, captures once, the whole
|
|
14
|
+
segment, and a negation, `(!a.b)`, captures the segments it consumed
|
|
15
|
+
as one.
|
|
16
|
+
- `translate`: a `$N` that is a whole segment, `.$0` or `[$0]`, becomes
|
|
17
|
+
the captured segment, its accessor included:
|
|
18
|
+
`translate('bye[3]', {'bye[*]': 'gone.$0'})` is `'gone[3]'`.
|
|
19
|
+
|
|
20
|
+
### Fixed
|
|
21
|
+
- Under partial matching, a pattern with `**` (or a group on either
|
|
22
|
+
side) drops no part of the path: the last capture runs to its end,
|
|
23
|
+
`match('**.b', 'a.b.c', groups=True)` captures `('a', 'b.c')`.
|
|
24
|
+
- A value-guarded path segment, `a=1` or `a.b!=1`, matches an
|
|
25
|
+
unguarded pattern, as a filtered one does.
|
|
26
|
+
- A whole-list slice, `a[]` or `a[:]`, matches a slice filter,
|
|
27
|
+
`a[x=1]`: the filter only narrows the list. A partial slice, `a[1:]`,
|
|
28
|
+
still does not.
|
|
29
|
+
- `a&x=1.b` parses as `(a&x=1, b)`: a dot in a number needs a digit
|
|
30
|
+
or an exponent after it (`1.5`, `1.e5`), so the dot starts the next
|
|
31
|
+
segment instead of ending the float `1.`. `a&x=1.` and `[1.]` no
|
|
32
|
+
longer parse.
|
|
33
|
+
|
|
6
34
|
## [0.55.3]
|
|
7
35
|
|
|
8
36
|
### Added
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dotted_notation
|
|
3
|
-
Version: 0.55.
|
|
3
|
+
Version: 0.55.4
|
|
4
4
|
Summary: Dotted notation for safe nested data traversal with optional chaining, pattern matching, and transforms
|
|
5
5
|
Author-email: Frey Waid <logophage1@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -619,6 +619,19 @@ With the `groups=True` parameter, you'll see how it was matched:
|
|
|
619
619
|
In the above example, `hello` matched to `hello` and `*` matched to `there.bye` (partial
|
|
620
620
|
matching is enabled by default).
|
|
621
621
|
|
|
622
|
+
A capture is the path's segment as the path writes it: its accessor, filters, value
|
|
623
|
+
guard and transforms come with it. A filter, guard or transform on the path only
|
|
624
|
+
narrows a segment, so an undecorated pattern still matches it:
|
|
625
|
+
|
|
626
|
+
>>> dotted.match('*[*].*', 'lots[*&a=1].d', groups=True)
|
|
627
|
+
('lots[*&a=1].d', ('lots', '[*&a=1]', 'd'))
|
|
628
|
+
>>> dotted.match('*.*', 'a.b|int', groups=True)
|
|
629
|
+
('a.b|int', ('a', 'b|int'))
|
|
630
|
+
>>> dotted.match('*', 'a=1', groups=True)
|
|
631
|
+
('a=1', ('a=1',))
|
|
632
|
+
>>> dotted.match('**.b', 'a.b.c', groups=True)
|
|
633
|
+
('a.b.c', ('a', 'b.c'))
|
|
634
|
+
|
|
622
635
|
Use `groups=GroupMode.patterns` to capture only pattern segments (wildcards, regex, etc.),
|
|
623
636
|
excluding literals:
|
|
624
637
|
|
|
@@ -680,6 +693,18 @@ only a recursive pattern that subsumes it will match:
|
|
|
680
693
|
'*b'
|
|
681
694
|
>>> dotted.match('*', '**')
|
|
682
695
|
|
|
696
|
+
A slice filter reads the list narrowed to the items that pass it, so the
|
|
697
|
+
slice of the whole list covers it, as `*` covers `a&x=1`. Which items pass
|
|
698
|
+
depends on the data, so no part of the list covers it, and neither does
|
|
699
|
+
`[*]`, which steps into each item rather than reading the list:
|
|
700
|
+
|
|
701
|
+
>>> dotted.match('a[]', 'a[x=1]')
|
|
702
|
+
'a[x=1]'
|
|
703
|
+
>>> dotted.match('a[:]', 'a[x=1]', groups=True)
|
|
704
|
+
('a[x=1]', ('a', '[x=1]'))
|
|
705
|
+
>>> dotted.match('a[1:]', 'a[x=1]')
|
|
706
|
+
>>> dotted.match('a[*]', 'a[x=1]')
|
|
707
|
+
|
|
683
708
|
<a id="replace"></a>
|
|
684
709
|
### Replace
|
|
685
710
|
|
|
@@ -743,6 +768,16 @@ With multiple wildcards, `$N` indices follow wildcard order:
|
|
|
743
768
|
>>> translate('a.X.b.Y.c', {'a.*.b.*.c': '$1.$0'})
|
|
744
769
|
'Y.X'
|
|
745
770
|
|
|
771
|
+
A `$N` that is a whole segment, `.$0` or `[$0]`, becomes the captured segment as the
|
|
772
|
+
path writes it, its accessor included:
|
|
773
|
+
|
|
774
|
+
>>> translate('bye[3]', {'bye[*]': 'gone.$0'})
|
|
775
|
+
'gone[3]'
|
|
776
|
+
>>> translate('bye.3', {'bye[*]': 'gone.$0'})
|
|
777
|
+
'gone.3'
|
|
778
|
+
>>> translate('bye[*&a=1]', {'bye[*]': 'gone.$0'})
|
|
779
|
+
'gone[*&a=1]'
|
|
780
|
+
|
|
746
781
|
The pattern map can be a dict or an iterable of `(pattern, template)` tuples.
|
|
747
782
|
Returns `None` if no pattern matches:
|
|
748
783
|
|
|
@@ -569,6 +569,19 @@ With the `groups=True` parameter, you'll see how it was matched:
|
|
|
569
569
|
In the above example, `hello` matched to `hello` and `*` matched to `there.bye` (partial
|
|
570
570
|
matching is enabled by default).
|
|
571
571
|
|
|
572
|
+
A capture is the path's segment as the path writes it: its accessor, filters, value
|
|
573
|
+
guard and transforms come with it. A filter, guard or transform on the path only
|
|
574
|
+
narrows a segment, so an undecorated pattern still matches it:
|
|
575
|
+
|
|
576
|
+
>>> dotted.match('*[*].*', 'lots[*&a=1].d', groups=True)
|
|
577
|
+
('lots[*&a=1].d', ('lots', '[*&a=1]', 'd'))
|
|
578
|
+
>>> dotted.match('*.*', 'a.b|int', groups=True)
|
|
579
|
+
('a.b|int', ('a', 'b|int'))
|
|
580
|
+
>>> dotted.match('*', 'a=1', groups=True)
|
|
581
|
+
('a=1', ('a=1',))
|
|
582
|
+
>>> dotted.match('**.b', 'a.b.c', groups=True)
|
|
583
|
+
('a.b.c', ('a', 'b.c'))
|
|
584
|
+
|
|
572
585
|
Use `groups=GroupMode.patterns` to capture only pattern segments (wildcards, regex, etc.),
|
|
573
586
|
excluding literals:
|
|
574
587
|
|
|
@@ -630,6 +643,18 @@ only a recursive pattern that subsumes it will match:
|
|
|
630
643
|
'*b'
|
|
631
644
|
>>> dotted.match('*', '**')
|
|
632
645
|
|
|
646
|
+
A slice filter reads the list narrowed to the items that pass it, so the
|
|
647
|
+
slice of the whole list covers it, as `*` covers `a&x=1`. Which items pass
|
|
648
|
+
depends on the data, so no part of the list covers it, and neither does
|
|
649
|
+
`[*]`, which steps into each item rather than reading the list:
|
|
650
|
+
|
|
651
|
+
>>> dotted.match('a[]', 'a[x=1]')
|
|
652
|
+
'a[x=1]'
|
|
653
|
+
>>> dotted.match('a[:]', 'a[x=1]', groups=True)
|
|
654
|
+
('a[x=1]', ('a', '[x=1]'))
|
|
655
|
+
>>> dotted.match('a[1:]', 'a[x=1]')
|
|
656
|
+
>>> dotted.match('a[*]', 'a[x=1]')
|
|
657
|
+
|
|
633
658
|
<a id="replace"></a>
|
|
634
659
|
### Replace
|
|
635
660
|
|
|
@@ -693,6 +718,16 @@ With multiple wildcards, `$N` indices follow wildcard order:
|
|
|
693
718
|
>>> translate('a.X.b.Y.c', {'a.*.b.*.c': '$1.$0'})
|
|
694
719
|
'Y.X'
|
|
695
720
|
|
|
721
|
+
A `$N` that is a whole segment, `.$0` or `[$0]`, becomes the captured segment as the
|
|
722
|
+
path writes it, its accessor included:
|
|
723
|
+
|
|
724
|
+
>>> translate('bye[3]', {'bye[*]': 'gone.$0'})
|
|
725
|
+
'gone[3]'
|
|
726
|
+
>>> translate('bye.3', {'bye[*]': 'gone.$0'})
|
|
727
|
+
'gone.3'
|
|
728
|
+
>>> translate('bye[*&a=1]', {'bye[*]': 'gone.$0'})
|
|
729
|
+
'gone[*&a=1]'
|
|
730
|
+
|
|
696
731
|
The pattern map can be a dict or an iterable of `(pattern, template)` tuples.
|
|
697
732
|
Returns `None` if no pattern matches:
|
|
698
733
|
|
|
@@ -1103,7 +1103,19 @@ class Slice(SimpleOp):
|
|
|
1103
1103
|
def matchable(self, op):
|
|
1104
1104
|
return isinstance(op, Slice)
|
|
1105
1105
|
|
|
1106
|
+
def is_whole(self):
|
|
1107
|
+
"""
|
|
1108
|
+
True if this slice reads the whole list: [], [:], [0:], [::1].
|
|
1109
|
+
"""
|
|
1110
|
+
start, stop, step = self.args
|
|
1111
|
+
return start in (None, 0) and stop is None and step in (None, 1)
|
|
1112
|
+
|
|
1106
1113
|
def match(self, op, specials=False):
|
|
1114
|
+
# a slice filter reads the list narrowed to the items that pass
|
|
1115
|
+
# it, which ones depending on the data: only the whole list
|
|
1116
|
+
# covers it
|
|
1117
|
+
if isinstance(op, SliceFilter):
|
|
1118
|
+
return base.MatchResult(slice(None)) if self.is_whole() else None
|
|
1107
1119
|
if not isinstance(op, Slice):
|
|
1108
1120
|
return None
|
|
1109
1121
|
if self.cardinality() < op.cardinality():
|
|
@@ -943,77 +943,48 @@ def match(pattern, path, groups=False, partial=True, strict=False):
|
|
|
943
943
|
def _match_parsed(pats, path_ops, path, groups, partial):
|
|
944
944
|
"""
|
|
945
945
|
match() on parsed arguments. `path` is what a successful match returns.
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
946
|
+
Each capture is the path segments one pattern segment matched, as the
|
|
947
|
+
path writes them.
|
|
948
|
+
"""
|
|
949
|
+
caps = _captures(pats, path_ops, partial, bool(groups))
|
|
950
|
+
if not groups:
|
|
951
|
+
return None if caps is None else path
|
|
952
|
+
if caps is None:
|
|
953
|
+
return (None, ())
|
|
950
954
|
# groups can be a bool, a GroupMode member, or its string value.
|
|
951
|
-
|
|
952
|
-
|
|
955
|
+
if groups == GroupMode.patterns or groups == GroupMode.patterns.value:
|
|
956
|
+
caps = [c for c in caps if c[1]]
|
|
957
|
+
return (path, tuple(results.assemble(ops) for ops, _ in caps))
|
|
953
958
|
|
|
954
|
-
def returns(r, matches, is_pat=None):
|
|
955
|
-
if not groups:
|
|
956
|
-
return r
|
|
957
|
-
if _patterns_only and is_pat is not None:
|
|
958
|
-
matches = [m for m, ip in zip(matches, is_pat) if ip]
|
|
959
|
-
return (r, tuple(matches))
|
|
960
959
|
|
|
960
|
+
def _captures(pats, path_ops, partial, collect=True):
|
|
961
|
+
"""
|
|
962
|
+
The captures of the path that pats match, as base.match_ops gives
|
|
963
|
+
them: (ops, is_pattern) pairs, each ops the path ops one pattern
|
|
964
|
+
segment matched. None when pats don't match. Without collect, a
|
|
965
|
+
match may give no captures.
|
|
966
|
+
"""
|
|
961
967
|
# Variadic ops (recursive, groups) consume variable-length path
|
|
962
968
|
# segments — use the recursive matcher. On the path side they make
|
|
963
969
|
# the path denote multiple expansions, which likewise needs it.
|
|
964
970
|
if pats.variadic or path_ops.variadic:
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
for
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
return returns(None, [], [])
|
|
983
|
-
if not collect:
|
|
984
|
-
# captures are only returned with groups
|
|
985
|
-
continue
|
|
986
|
-
is_pat = pop.is_pattern()
|
|
987
|
-
if isinstance(m, (tuple, list)):
|
|
988
|
-
for _m in m:
|
|
989
|
-
_matches.append(_m.val)
|
|
990
|
-
_is_pat.append(is_pat)
|
|
991
|
-
else:
|
|
992
|
-
_matches.append(m.val)
|
|
993
|
-
_is_pat.append(is_pat)
|
|
994
|
-
|
|
995
|
-
# we've completed matching but the last item in match groups is treated 'greedily'
|
|
996
|
-
assert kop is not None # sanity
|
|
997
|
-
|
|
998
|
-
# exact match
|
|
999
|
-
if len(pats) == len(path_ops):
|
|
1000
|
-
return returns(path, _matches, _is_pat)
|
|
1001
|
-
|
|
1002
|
-
# if we're not doing partial matches or we haven't consumed all pats, fail
|
|
1003
|
-
if not partial or idx < len(pats) - 1:
|
|
1004
|
-
return returns(None, [], [])
|
|
1005
|
-
|
|
1006
|
-
# otherwise inexact (partial) match
|
|
1007
|
-
if not collect:
|
|
1008
|
-
return path
|
|
1009
|
-
# assemble remaining segments
|
|
1010
|
-
rpath = path_ops.assemble(start=idx)
|
|
1011
|
-
if pop is None:
|
|
1012
|
-
_matches.append(rpath)
|
|
1013
|
-
_is_pat.append(False)
|
|
1014
|
-
else:
|
|
1015
|
-
_matches[-1] = rpath
|
|
1016
|
-
return returns(path, _matches, _is_pat)
|
|
971
|
+
return base.match_ops(list(pats), list(path_ops), partial)
|
|
972
|
+
|
|
973
|
+
# one path segment per pattern segment; a partial match's last
|
|
974
|
+
# capture runs to the end of the path
|
|
975
|
+
if len(pats) > len(path_ops):
|
|
976
|
+
return None
|
|
977
|
+
if len(pats) < len(path_ops) and not partial:
|
|
978
|
+
return None
|
|
979
|
+
caps = []
|
|
980
|
+
for pop, kop in zip(pats, path_ops):
|
|
981
|
+
if not pop.match(kop.match_target(), specials=True):
|
|
982
|
+
return None
|
|
983
|
+
if collect:
|
|
984
|
+
caps.append(((kop,), pop.is_pattern()))
|
|
985
|
+
if collect and len(pats) < len(path_ops):
|
|
986
|
+
caps[-1] = (tuple(path_ops.ops[len(pats) - 1:]), caps[-1][1])
|
|
987
|
+
return caps
|
|
1017
988
|
|
|
1018
989
|
|
|
1019
990
|
def match_multi(pattern, iterable, groups=False, partial=True, strict=False):
|
|
@@ -1068,17 +1039,48 @@ def translate(path, pattern_map):
|
|
|
1068
1039
|
pats = parse(pattern)
|
|
1069
1040
|
if path_ops is None:
|
|
1070
1041
|
path_ops = parse(path)
|
|
1071
|
-
|
|
1072
|
-
if
|
|
1042
|
+
caps = _captures(pats, path_ops, False)
|
|
1043
|
+
if caps is None:
|
|
1073
1044
|
continue
|
|
1074
1045
|
try:
|
|
1075
|
-
return
|
|
1046
|
+
return _fill(template, [ops for ops, is_pat in caps if is_pat])
|
|
1076
1047
|
except IndexError:
|
|
1077
1048
|
continue
|
|
1078
1049
|
|
|
1079
1050
|
return None
|
|
1080
1051
|
|
|
1081
1052
|
|
|
1053
|
+
def _fill(template, captures):
|
|
1054
|
+
"""
|
|
1055
|
+
`template` with each $N filled from captures[N], the path ops of one
|
|
1056
|
+
capture. A $N that is a whole segment, gone.$0 or gone[$0], becomes
|
|
1057
|
+
the captured segment as the path writes it, its accessor included:
|
|
1058
|
+
gone.$0 on a capture of [3] is gone[3]. A $N anywhere else takes the
|
|
1059
|
+
capture's text, as replace() does.
|
|
1060
|
+
"""
|
|
1061
|
+
texts = [results.assemble(ops) for ops in captures]
|
|
1062
|
+
ops = []
|
|
1063
|
+
for op in parse(template):
|
|
1064
|
+
n = _segment_subst(op)
|
|
1065
|
+
if n is None:
|
|
1066
|
+
ops.append(op.resolve(texts, False))
|
|
1067
|
+
continue
|
|
1068
|
+
ops.extend(captures[n])
|
|
1069
|
+
return results.assemble(ops)
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
def _segment_subst(op):
|
|
1073
|
+
"""
|
|
1074
|
+
N when template op `op` is a whole $N segment, .$0 or [$0], else None.
|
|
1075
|
+
"""
|
|
1076
|
+
if type(op) is not access.Key and type(op) is not access.Slot:
|
|
1077
|
+
return None
|
|
1078
|
+
sub = op.op
|
|
1079
|
+
if type(sub) is not matchers.Subst or sub.transforms or not isinstance(sub.value, int):
|
|
1080
|
+
return None
|
|
1081
|
+
return sub.value
|
|
1082
|
+
|
|
1083
|
+
|
|
1082
1084
|
def translate_multi(paths, pattern_map):
|
|
1083
1085
|
"""
|
|
1084
1086
|
Translate multiple paths via pattern_map.
|
|
@@ -45,9 +45,10 @@ def has_any(gen):
|
|
|
45
45
|
def match_ops(pats, path_ops, partial):
|
|
46
46
|
"""
|
|
47
47
|
Match a list of pattern ops against a list of path ops.
|
|
48
|
-
Returns a list of (
|
|
49
|
-
on failure
|
|
50
|
-
|
|
48
|
+
Returns a list of (ops, is_pattern) capture pairs on success, None
|
|
49
|
+
on failure: ops are the path ops one pattern segment matched, as the
|
|
50
|
+
path writes them. Dispatch is per-op on both sides: a variadic op at
|
|
51
|
+
the head of the path (group, recursive) decides how the pattern must
|
|
51
52
|
cover it via do_match_path; otherwise the head pattern op decides
|
|
52
53
|
how many path segments it consumes via do_match.
|
|
53
54
|
"""
|
|
@@ -58,10 +59,31 @@ def match_ops(pats, path_ops, partial):
|
|
|
58
59
|
if not path_ops:
|
|
59
60
|
return []
|
|
60
61
|
if partial:
|
|
61
|
-
return
|
|
62
|
+
return leftover(path_ops)
|
|
62
63
|
return None
|
|
63
64
|
|
|
64
65
|
|
|
66
|
+
def leftover(path_ops):
|
|
67
|
+
"""
|
|
68
|
+
The captures of what a partial match leaves of the path: one capture
|
|
69
|
+
with no pattern segment of its own, which joins the capture before it
|
|
70
|
+
(see capture).
|
|
71
|
+
"""
|
|
72
|
+
return [(tuple(path_ops), None)]
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def capture(path_ops, is_pat, rest):
|
|
76
|
+
"""
|
|
77
|
+
The captures `rest` with a capture of `path_ops` first: the path ops
|
|
78
|
+
one pattern segment matched. A partial match's leftover at the head
|
|
79
|
+
of `rest` joins it, so the last capture runs to the end of the path.
|
|
80
|
+
"""
|
|
81
|
+
ops = tuple(path_ops)
|
|
82
|
+
if rest and rest[0][1] is None:
|
|
83
|
+
return [(ops + rest[0][0], is_pat)] + rest[1:]
|
|
84
|
+
return [(ops, is_pat)] + rest
|
|
85
|
+
|
|
86
|
+
|
|
65
87
|
class MatchResult:
|
|
66
88
|
def __init__(self, val):
|
|
67
89
|
self.val = val
|
|
@@ -110,6 +132,13 @@ class Op:
|
|
|
110
132
|
return self
|
|
111
133
|
def scrub(self, node):
|
|
112
134
|
return node
|
|
135
|
+
def match_target(self):
|
|
136
|
+
"""
|
|
137
|
+
What a pattern matches this path op as: the op itself. A
|
|
138
|
+
decoration that narrows only the values a segment reaches, a
|
|
139
|
+
value guard or a transform, plays no part (see wrappers).
|
|
140
|
+
"""
|
|
141
|
+
return self
|
|
113
142
|
def match_keys(self, node):
|
|
114
143
|
"""
|
|
115
144
|
Keys of dict-like `node` that this op matches.
|
|
@@ -512,16 +541,12 @@ class TraversalOp(Op):
|
|
|
512
541
|
"""
|
|
513
542
|
if not path_ops:
|
|
514
543
|
return None
|
|
515
|
-
|
|
516
|
-
if not m:
|
|
544
|
+
if not self.match(path_ops[0].match_target(), specials=True):
|
|
517
545
|
return None
|
|
518
546
|
rest = match_ops(rest_pats, path_ops[1:], partial)
|
|
519
547
|
if rest is None:
|
|
520
548
|
return None
|
|
521
|
-
|
|
522
|
-
if isinstance(m, (tuple, list)):
|
|
523
|
-
return [(_m.val, is_pat) for _m in m] + rest
|
|
524
|
-
return [(m.val, is_pat)] + rest
|
|
549
|
+
return capture(path_ops[:1], self.is_pattern(), rest)
|
|
525
550
|
|
|
526
551
|
|
|
527
552
|
class MatchOp(Op):
|
|
@@ -242,7 +242,13 @@ _numeric_quoted = S('#') + ((S("'") + ppc.number + S("'")) | (S('"') + ppc.numbe
|
|
|
242
242
|
numeric_quoted = _numeric_quoted.set_parse_action(matchers.NumericQuoted)
|
|
243
243
|
|
|
244
244
|
numeric_key = integer.copy().set_parse_action(matchers.Numeric)
|
|
245
|
-
|
|
245
|
+
|
|
246
|
+
# A number as pyparsing reads one, but a dot in it needs a digit or an
|
|
247
|
+
# exponent after it: in a&x=1.b the dot starts the next segment, it does
|
|
248
|
+
# not end 1. 1.e5 is still the float 100000.0.
|
|
249
|
+
_number = pp.Regex(r'[+-]?(?:[0-9]+(?:\.[0-9]+|\.(?=[eE][+-]?[0-9]))?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?')
|
|
250
|
+
_number.set_parse_action(lambda t: int(t[0]) if t[0].lstrip('+-').isdigit() else float(t[0]))
|
|
251
|
+
numeric_slot = _number.copy().add_parse_action(matchers.Numeric)
|
|
246
252
|
|
|
247
253
|
# Extended numeric literals: scientific (1e10), underscore (1_000), hex (0x1F), octal (0o17), binary (0b1010)
|
|
248
254
|
_numeric_extended_re = pp.Regex(
|
|
@@ -4,7 +4,6 @@ from . import base
|
|
|
4
4
|
from . import matchers
|
|
5
5
|
from . import wrappers
|
|
6
6
|
from . import engine
|
|
7
|
-
from . import results
|
|
8
7
|
from .access import AccessOp, Key, Attr, Slot, SlotSpecial
|
|
9
8
|
|
|
10
9
|
|
|
@@ -212,8 +211,7 @@ class OpGroup(base.TraversalOp):
|
|
|
212
211
|
rest = base.match_ops(list(rest_pats), path_ops[n:], partial)
|
|
213
212
|
if rest is None:
|
|
214
213
|
continue
|
|
215
|
-
|
|
216
|
-
return [(combined, True)] + rest
|
|
214
|
+
return base.capture(path_ops[:n], True, rest)
|
|
217
215
|
return None
|
|
218
216
|
|
|
219
217
|
def do_match_path(self, pats, rest_path, partial):
|
|
@@ -231,6 +229,8 @@ class OpGroup(base.TraversalOp):
|
|
|
231
229
|
decomposition exists do captures fall back to the first
|
|
232
230
|
branch's expansion.
|
|
233
231
|
"""
|
|
232
|
+
if not pats and partial:
|
|
233
|
+
return base.leftover([self] + list(rest_path))
|
|
234
234
|
fallback = self._verify_covered(pats, rest_path, partial)
|
|
235
235
|
if fallback is None:
|
|
236
236
|
return None
|
|
@@ -276,7 +276,7 @@ class OpGroup(base.TraversalOp):
|
|
|
276
276
|
if rest is None:
|
|
277
277
|
continue
|
|
278
278
|
is_pat = any(p.is_pattern() for p in head)
|
|
279
|
-
return
|
|
279
|
+
return base.capture([self], is_pat, rest)
|
|
280
280
|
return None
|
|
281
281
|
|
|
282
282
|
def _covers_branches(self, head_pats, tail_partial=False):
|
|
@@ -904,7 +904,7 @@ class OpGroupNot(OpGroup):
|
|
|
904
904
|
rest = base.match_ops(rest_pats, path_ops[i + 1:], partial)
|
|
905
905
|
if rest is None:
|
|
906
906
|
return None
|
|
907
|
-
return
|
|
907
|
+
return base.capture(path_ops[:i + 1], True, rest)
|
|
908
908
|
return None
|
|
909
909
|
|
|
910
910
|
def do_match_path(self, pats, rest_path, partial):
|
|
@@ -915,14 +915,14 @@ class OpGroupNot(OpGroup):
|
|
|
915
915
|
segment.
|
|
916
916
|
"""
|
|
917
917
|
if not pats:
|
|
918
|
-
return [] if partial else None
|
|
918
|
+
return base.leftover([self] + list(rest_path)) if partial else None
|
|
919
919
|
head = pats[0]
|
|
920
920
|
if head != self and not self._wildcard_covers(head):
|
|
921
921
|
return None
|
|
922
922
|
rest = base.match_ops(pats[1:], rest_path, partial)
|
|
923
923
|
if rest is None:
|
|
924
924
|
return None
|
|
925
|
-
return
|
|
925
|
+
return base.capture([self], True, rest)
|
|
926
926
|
|
|
927
927
|
def _wildcard_covers(self, head):
|
|
928
928
|
"""
|
|
@@ -1027,7 +1027,7 @@ class Pack(base.TraversalOp):
|
|
|
1027
1027
|
rest = base.match_ops(rest_pats, path_ops[1:], partial)
|
|
1028
1028
|
if rest is None:
|
|
1029
1029
|
return None
|
|
1030
|
-
return [
|
|
1030
|
+
return base.capture(path_ops[:1], False, rest)
|
|
1031
1031
|
|
|
1032
1032
|
def operator(self, top=False):
|
|
1033
1033
|
"""
|
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
from . import base
|
|
4
4
|
from . import engine
|
|
5
5
|
from . import matchers
|
|
6
|
-
from . import results
|
|
7
6
|
from . import utils
|
|
8
7
|
from .access import BaseOp, Key, Attr
|
|
9
8
|
|
|
@@ -161,8 +160,7 @@ class Recursive(BaseOp):
|
|
|
161
160
|
rest = base.match_ops(rest_pats, path_ops[n:], partial)
|
|
162
161
|
if rest is None:
|
|
163
162
|
continue
|
|
164
|
-
|
|
165
|
-
return [(combined, True)] + rest
|
|
163
|
+
return base.capture(path_ops[:n], True, rest)
|
|
166
164
|
return None
|
|
167
165
|
|
|
168
166
|
def do_match_path(self, pats, rest_path, partial):
|
|
@@ -175,7 +173,7 @@ class Recursive(BaseOp):
|
|
|
175
173
|
a recursive pattern may keep covering segments after the group.
|
|
176
174
|
"""
|
|
177
175
|
if not pats:
|
|
178
|
-
return [] if partial else None
|
|
176
|
+
return base.leftover([self] + list(rest_path)) if partial else None
|
|
179
177
|
head = pats[0]
|
|
180
178
|
if not isinstance(head, Recursive):
|
|
181
179
|
return None
|
|
@@ -186,7 +184,7 @@ class Recursive(BaseOp):
|
|
|
186
184
|
rest = base.match_ops(pats, rest_path, partial)
|
|
187
185
|
if rest is None:
|
|
188
186
|
return None
|
|
189
|
-
return
|
|
187
|
+
return base.capture([self], True, rest)
|
|
190
188
|
|
|
191
189
|
def _subsumed_by(self, pat):
|
|
192
190
|
"""
|
|
@@ -97,9 +97,14 @@ class Wrap(base.TraversalOp):
|
|
|
97
97
|
def do_match_path(self, pats, rest_path, partial):
|
|
98
98
|
"""
|
|
99
99
|
Delegate path-side matching to the wrapped op (only reached for
|
|
100
|
-
variadic inners, via is_variadic).
|
|
100
|
+
variadic inners, via is_variadic). A capture of the wrapped op
|
|
101
|
+
is a capture of this one: the segment as the path writes it.
|
|
101
102
|
"""
|
|
102
|
-
|
|
103
|
+
r = self.inner.do_match_path(pats, rest_path, partial)
|
|
104
|
+
if r is None:
|
|
105
|
+
return None
|
|
106
|
+
inner = self.inner
|
|
107
|
+
return [(tuple(self if op is inner else op for op in ops), is_pat) for ops, is_pat in r]
|
|
103
108
|
|
|
104
109
|
def covered_by(self, matcher):
|
|
105
110
|
"""
|
|
@@ -213,6 +218,13 @@ class TransformWrap(Wrap):
|
|
|
213
218
|
"""
|
|
214
219
|
return TransformWrap(inner, self.transforms)
|
|
215
220
|
|
|
221
|
+
def match_target(self):
|
|
222
|
+
"""
|
|
223
|
+
A transform changes what the segment yields, not which segment it
|
|
224
|
+
is: a pattern matches the wrapped op.
|
|
225
|
+
"""
|
|
226
|
+
return self.inner.match_target()
|
|
227
|
+
|
|
216
228
|
def __repr__(self):
|
|
217
229
|
return self.operator(top=True)
|
|
218
230
|
|
|
@@ -398,6 +410,13 @@ class ValueGuard(Wrap):
|
|
|
398
410
|
"""
|
|
399
411
|
return self.pred_op is predicates.NE
|
|
400
412
|
|
|
413
|
+
def match_target(self):
|
|
414
|
+
"""
|
|
415
|
+
A guard narrows the values a segment reaches, as a filter does:
|
|
416
|
+
a pattern matches the wrapped op.
|
|
417
|
+
"""
|
|
418
|
+
return self.inner.match_target()
|
|
419
|
+
|
|
401
420
|
def __repr__(self):
|
|
402
421
|
return self.operator(top=True)
|
|
403
422
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: dotted_notation
|
|
3
|
-
Version: 0.55.
|
|
3
|
+
Version: 0.55.4
|
|
4
4
|
Summary: Dotted notation for safe nested data traversal with optional chaining, pattern matching, and transforms
|
|
5
5
|
Author-email: Frey Waid <logophage1@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -619,6 +619,19 @@ With the `groups=True` parameter, you'll see how it was matched:
|
|
|
619
619
|
In the above example, `hello` matched to `hello` and `*` matched to `there.bye` (partial
|
|
620
620
|
matching is enabled by default).
|
|
621
621
|
|
|
622
|
+
A capture is the path's segment as the path writes it: its accessor, filters, value
|
|
623
|
+
guard and transforms come with it. A filter, guard or transform on the path only
|
|
624
|
+
narrows a segment, so an undecorated pattern still matches it:
|
|
625
|
+
|
|
626
|
+
>>> dotted.match('*[*].*', 'lots[*&a=1].d', groups=True)
|
|
627
|
+
('lots[*&a=1].d', ('lots', '[*&a=1]', 'd'))
|
|
628
|
+
>>> dotted.match('*.*', 'a.b|int', groups=True)
|
|
629
|
+
('a.b|int', ('a', 'b|int'))
|
|
630
|
+
>>> dotted.match('*', 'a=1', groups=True)
|
|
631
|
+
('a=1', ('a=1',))
|
|
632
|
+
>>> dotted.match('**.b', 'a.b.c', groups=True)
|
|
633
|
+
('a.b.c', ('a', 'b.c'))
|
|
634
|
+
|
|
622
635
|
Use `groups=GroupMode.patterns` to capture only pattern segments (wildcards, regex, etc.),
|
|
623
636
|
excluding literals:
|
|
624
637
|
|
|
@@ -680,6 +693,18 @@ only a recursive pattern that subsumes it will match:
|
|
|
680
693
|
'*b'
|
|
681
694
|
>>> dotted.match('*', '**')
|
|
682
695
|
|
|
696
|
+
A slice filter reads the list narrowed to the items that pass it, so the
|
|
697
|
+
slice of the whole list covers it, as `*` covers `a&x=1`. Which items pass
|
|
698
|
+
depends on the data, so no part of the list covers it, and neither does
|
|
699
|
+
`[*]`, which steps into each item rather than reading the list:
|
|
700
|
+
|
|
701
|
+
>>> dotted.match('a[]', 'a[x=1]')
|
|
702
|
+
'a[x=1]'
|
|
703
|
+
>>> dotted.match('a[:]', 'a[x=1]', groups=True)
|
|
704
|
+
('a[x=1]', ('a', '[x=1]'))
|
|
705
|
+
>>> dotted.match('a[1:]', 'a[x=1]')
|
|
706
|
+
>>> dotted.match('a[*]', 'a[x=1]')
|
|
707
|
+
|
|
683
708
|
<a id="replace"></a>
|
|
684
709
|
### Replace
|
|
685
710
|
|
|
@@ -743,6 +768,16 @@ With multiple wildcards, `$N` indices follow wildcard order:
|
|
|
743
768
|
>>> translate('a.X.b.Y.c', {'a.*.b.*.c': '$1.$0'})
|
|
744
769
|
'Y.X'
|
|
745
770
|
|
|
771
|
+
A `$N` that is a whole segment, `.$0` or `[$0]`, becomes the captured segment as the
|
|
772
|
+
path writes it, its accessor included:
|
|
773
|
+
|
|
774
|
+
>>> translate('bye[3]', {'bye[*]': 'gone.$0'})
|
|
775
|
+
'gone[3]'
|
|
776
|
+
>>> translate('bye.3', {'bye[*]': 'gone.$0'})
|
|
777
|
+
'gone.3'
|
|
778
|
+
>>> translate('bye[*&a=1]', {'bye[*]': 'gone.$0'})
|
|
779
|
+
'gone[*&a=1]'
|
|
780
|
+
|
|
746
781
|
The pattern map can be a dict or an iterable of `(pattern, template)` tuples.
|
|
747
782
|
Returns `None` if no pattern matches:
|
|
748
783
|
|
|
@@ -6,7 +6,7 @@ build-backend = "setuptools.build_meta"
|
|
|
6
6
|
|
|
7
7
|
[project]
|
|
8
8
|
name = "dotted_notation"
|
|
9
|
-
version = "0.55.
|
|
9
|
+
version = "0.55.4"
|
|
10
10
|
description = "Dotted notation for safe nested data traversal with optional chaining, pattern matching, and transforms"
|
|
11
11
|
readme = "README.md"
|
|
12
12
|
requires-python = ">=3.6"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{dotted_notation-0.55.3 → dotted_notation-0.55.4}/dotted_notation.egg-info/dependency_links.txt
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|