You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.rst
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -408,6 +408,19 @@ setting a library-wide dpath option:
408
408
Again, by default, this behavior is OFF, and empty string keys will
409
409
result in ``dpath.exceptions.InvalidKeyName`` being thrown.
410
410
411
+
Separator got you down? Use lists as paths
412
+
==========================================
413
+
414
+
The default behavior in dpath is to assume that the path given is a string, which must be tokenized by splitting at the separator to yield a distinct set of path components against which dictionary keys can be individually glob tested. However, this presents a problem when you want to use paths that have a separator in their name; the tokenizer cannot properly understand what you mean by '/a/b/c' if it is possible for '/' to exist as a valid character in a key name.
415
+
416
+
To get around this, you can sidestep the whole "filesystem path" style, and abandon the separator entirely, by using lists as paths. All of the methods in dpath.util.* support the use of a list instead of a string as a path. So for example:
Copy file name to clipboardExpand all lines: dpath/path.py
+11-16Lines changed: 11 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -42,27 +42,22 @@ def paths_only(path):
42
42
l.append(p[0])
43
43
returnl
44
44
45
-
defvalidate(path, separator="/", regex=None):
45
+
defvalidate(path, regex=None):
46
46
"""
47
47
Validate that all the keys in the given list of path components are valid, given that they do not contain the separator, and match any optional regex given.
48
48
"""
49
49
validated= []
50
50
foreleminpath:
51
51
key=elem[0]
52
52
strkey=str(key)
53
-
if (separatorand (separatorinstrkey)):
54
-
raisedpath.exceptions.InvalidKeyName("{0} at {1} contains the separator {2}"
55
-
"".format(strkey,
56
-
separator.join(validated),
57
-
separator))
58
-
elif (regexand (notregex.findall(strkey))):
53
+
if (regexand (notregex.findall(strkey))):
59
54
raisedpath.exceptions.InvalidKeyName("{} at {} does not match the expression {}"
0 commit comments