Skip to content

Commit 474ba6d

Browse files
committed
Documentation
1 parent fb77a64 commit 474ba6d

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

dpath/segments.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from copy import deepcopy
22
from fnmatch import fnmatchcase
3-
from typing import List, Sequence, Tuple, Iterator, Any, Dict, Union
3+
from typing import List, Sequence, Tuple, Iterator, Any, Dict, Union, Optional
44

55
from dpath import options
66
from dpath.exceptions import InvalidGlob, InvalidKeyName, PathNotFound
7-
from dpath.util import PathSegment
7+
from dpath.util import PathSegment, Creator, Hints
88

99

1010
def make_walkable(node) -> Iterator[Tuple[PathSegment, Any]]:
@@ -266,9 +266,9 @@ def extend(thing: List, index: int, value=None):
266266
return thing
267267

268268

269-
def __default_creator__(
269+
def _default_creator(
270270
current: Union[Dict, List],
271-
segments: List[PathSegment],
271+
segments: Sequence[PathSegment],
272272
i: int,
273273
hints: Sequence[Tuple[PathSegment, type]] = ()
274274
):
@@ -301,7 +301,13 @@ def __default_creator__(
301301
current[segment] = {}
302302

303303

304-
def set(obj, segments, value, creator=__default_creator__, hints=()):
304+
def set(
305+
obj,
306+
segments: Sequence[PathSegment],
307+
value,
308+
creator: Optional[Creator] = _default_creator,
309+
hints: Hints = ()
310+
):
305311
"""
306312
Set the value in obj at the place indicated by segments. If creator is not
307313
None (default __default_creator__), then call the creator function to
@@ -323,7 +329,7 @@ def set(obj, segments, value, creator=__default_creator__, hints=()):
323329
current[segment]
324330
except:
325331
if creator is not None:
326-
creator(current, segments, i, hints=hints)
332+
creator(current, segments, i, hints)
327333
else:
328334
raise
329335

dpath/util.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from collections.abc import MutableMapping, MutableSequence
22
from enum import IntFlag, auto
3-
from typing import Union, List, Any, Dict, Callable, Sequence, Tuple
3+
from typing import Union, List, Any, Dict, Callable, Sequence, Tuple, Optional
44

55
from dpath import options, segments
66
from dpath.exceptions import InvalidKeyName, PathNotFound
@@ -9,19 +9,40 @@
99

1010

1111
class MergeType(IntFlag):
12-
REPLACE = auto()
1312
ADDITIVE = auto()
13+
"""List objects are combined onto one long list (NOT a set). This is the default flag."""
14+
15+
REPLACE = auto()
16+
"""Instead of combining list objects, when 2 list objects are at an equal depth of merge, replace the destination
17+
with the source."""
18+
1419
TYPESAFE = auto()
20+
"""When 2 keys at equal levels are of different types, raise a TypeError exception. By default, the source
21+
replaces the destination in this situation."""
1522

1623

17-
# Type alias for dict path segments where integers are explicitly casted
1824
PathSegment = Union[int, str]
25+
"""Type alias for dict path segments where integers are explicitly casted."""
26+
27+
Filter = Callable[[Any], bool]
28+
"""Type alias for filter functions.
29+
30+
(Any) -> bool"""
31+
32+
Hints = Sequence[Tuple[PathSegment, type]]
33+
"""Type alias for creator function hint sequences."""
1934

20-
# Type alias for filter functions
21-
Filter = Callable[[Any], bool] # (Any) -> bool
35+
Creator = Callable[[Union[Dict, List], Sequence[PathSegment], int, Optional[Hints]], None]
36+
"""Type alias for creator functions.
2237
23-
# Type alias for creator functions
24-
Creator = Callable[[Union[Dict, List], List[PathSegment], int, Sequence[Tuple[PathSegment, type]]], None]
38+
Example creator function signature:
39+
40+
def creator(
41+
current: Union[Dict, List],
42+
segments: Sequence[PathSegment],
43+
i: int,
44+
hints: Sequence[Tuple[PathSegment, type]] = ()
45+
)"""
2546

2647

2748
def _split_path(path: str, separator: str) -> Union[List[PathSegment], PathSegment]:
@@ -270,14 +291,6 @@ def merge(dst: Dict, src: Dict, separator='/', afilter: Filter = None, flags=Mer
270291
https://github.com/akesterson/dpath-python/issues/58
271292
272293
flags is an OR'ed combination of MergeType enum members.
273-
* ADDITIVE : List objects are combined onto one long
274-
list (NOT a set). This is the default flag.
275-
* REPLACE : Instead of combining list objects, when
276-
2 list objects are at an equal depth of merge, replace
277-
the destination with the source.
278-
* TYPESAFE : When 2 keys at equal levels are of different
279-
types, raise a TypeError exception. By default, the source
280-
replaces the destination in this situation.
281294
"""
282295
filtered_src = search(src, '**', afilter=afilter, separator='/')
283296

0 commit comments

Comments
 (0)