Skip to content

Commit 43554c2

Browse files
committed
Use fstrings
1 parent 474ba6d commit 43554c2

2 files changed

Lines changed: 16 additions & 19 deletions

File tree

dpath/segments.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def walk(obj, location=()):
7070
if length is not None and length == 0 and not options.ALLOW_EMPTY_STRING_KEYS:
7171
raise InvalidKeyName("Empty string keys not allowed without "
7272
"dpath.options.ALLOW_EMPTY_STRING_KEYS=True: "
73-
"{}".format(location + (k,)))
73+
f"{location + (k,)}")
7474
yield (location + (k,)), v
7575

7676
for k, v in make_walkable(obj):
@@ -87,7 +87,7 @@ def get(obj, segments):
8787
current = obj
8888
for (i, segment) in enumerate(segments):
8989
if leaf(current):
90-
raise PathNotFound('Path: {}[{}]'.format(segments, i))
90+
raise PathNotFound(f"Path: {segments}[{i}]")
9191

9292
current = current[segment]
9393
return current
@@ -196,8 +196,7 @@ def match(segments, glob):
196196
ss = glob.index('**')
197197

198198
if '**' in glob[ss + 1:]:
199-
raise InvalidGlob("Invalid glob. Only one '**' is permitted per glob: {}"
200-
"".format(glob))
199+
raise InvalidGlob(f"Invalid glob. Only one '**' is permitted per glob: {glob}")
201200

202201
# Convert '**' segment into multiple '*' segments such that the
203202
# lengths of the path and glob match. '**' also can collapse and
@@ -249,7 +248,7 @@ def extend(thing: List, index: int, value=None):
249248
extend(thing, int) -> [thing..., None, ...]
250249
"""
251250
try:
252-
expansion = (type(thing)())
251+
expansion = type(thing)()
253252

254253
# Using this rather than the multiply notation in order to support a
255254
# wider variety of sequence like things.
@@ -335,7 +334,7 @@ def set(
335334

336335
current = current[segment]
337336
if i != length - 1 and leaf(current):
338-
raise PathNotFound('Path: {}[{}]'.format(segments, i))
337+
raise PathNotFound(f"Path: {segments}[{i}]")
339338

340339
if isinstance(segments[-1], int):
341340
extend(current, segments[-1])

dpath/util.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ def new(obj: Dict, path: str, value, separator="/", creator: Creator = None) ->
9292
return segments.set(obj, split_segments, value)
9393

9494

95-
def delete(obj: Dict, glob: str, separator='/', afilter: Filter = None) -> int:
95+
def delete(obj: Dict, glob: str, separator="/", afilter: Filter = None) -> int:
9696
"""
9797
Given a obj, delete all elements that match the glob.
9898
@@ -144,12 +144,12 @@ def f(obj, pair, counter):
144144

145145
[deleted] = segments.foldm(obj, f, [0])
146146
if not deleted:
147-
raise PathNotFound("Could not find {0} to delete it".format(glob))
147+
raise PathNotFound(f"Could not find {glob} to delete it")
148148

149149
return deleted
150150

151151

152-
def set(obj: Dict, glob: str, value, separator='/', afilter: Filter = None) -> int:
152+
def set(obj: Dict, glob: str, value, separator="/", afilter: Filter = None) -> int:
153153
"""
154154
Given a path glob, set all existing elements in the document
155155
to the given value. Returns the number of elements changed.
@@ -205,12 +205,12 @@ def f(_, pair, results):
205205

206206
raise KeyError(glob)
207207
elif len(results) > 1:
208-
raise ValueError("dpath.util.get() globs must match only one leaf : %s" % glob)
208+
raise ValueError(f"dpath.util.get() globs must match only one leaf: {glob}")
209209

210210
return results[0]
211211

212212

213-
def values(obj: Dict, glob: str, separator='/', afilter: Filter = None, dirs=True):
213+
def values(obj: Dict, glob: str, separator="/", afilter: Filter = None, dirs=True):
214214
"""
215215
Given an object and a path glob, return an array of all values which match
216216
the glob. The arguments to this function are identical to those of search().
@@ -220,7 +220,7 @@ def values(obj: Dict, glob: str, separator='/', afilter: Filter = None, dirs=Tru
220220
return [v for p, v in search(obj, glob, yielded, separator, afilter, dirs)]
221221

222222

223-
def search(obj: Dict, glob: str, yielded=False, separator='/', afilter: Filter = None, dirs=True):
223+
def search(obj: Dict, glob: str, yielded=False, separator="/", afilter: Filter = None, dirs=True):
224224
"""
225225
Given a path glob, return a dictionary containing all keys
226226
that matched the given glob.
@@ -249,7 +249,7 @@ def keeper(path, found):
249249
def yielder():
250250
for path, found in segments.walk(obj):
251251
if keeper(path, found):
252-
yield (separator.join(map(segments.int_str, path)), found)
252+
yield separator.join(map(segments.int_str, path)), found
253253
return yielder()
254254
else:
255255
def f(obj, pair, result):
@@ -261,7 +261,7 @@ def f(obj, pair, result):
261261
return segments.fold(obj, f, {})
262262

263263

264-
def merge(dst: Dict, src: Dict, separator='/', afilter: Filter = None, flags=MergeType.ADDITIVE):
264+
def merge(dst: Dict, src: Dict, separator="/", afilter: Filter = None, flags=MergeType.ADDITIVE):
265265
"""
266266
Merge source into destination. Like dict.update() but performs deep
267267
merging.
@@ -311,7 +311,7 @@ def merger(dst, src, _segments=()):
311311
if len(key) == 0 and not options.ALLOW_EMPTY_STRING_KEYS:
312312
raise InvalidKeyName("Empty string keys not allowed without "
313313
"dpath.options.ALLOW_EMPTY_STRING_KEYS=True: "
314-
"{}".format(current_path))
314+
f"{current_path}")
315315

316316
# Validate src and dst types match.
317317
if flags & MergeType.TYPESAFE:
@@ -321,9 +321,7 @@ def merger(dst, src, _segments=()):
321321
ft = type(found)
322322
if tt != ft:
323323
path = separator.join(current_path)
324-
raise TypeError("Cannot merge objects of type"
325-
"{0} and {1} at {2}"
326-
"".format(tt, ft, path))
324+
raise TypeError(f"Cannot merge objects of type {tt} and {ft} at {path}")
327325

328326
# Path not present in destination, create it.
329327
if not segments.has(dst, current_path):
@@ -357,7 +355,7 @@ def merger(dst, src, _segments=()):
357355

358356
if flags & MergeType.REPLACE:
359357
try:
360-
target['']
358+
target[""]
361359
except TypeError:
362360
segments.set(dst, current_path, found)
363361
continue

0 commit comments

Comments
 (0)