Skip to content

Commit 008d5ce

Browse files
committed
refact(jsonp) beatify a bit set_path_value()
1 parent b0e2409 commit 008d5ce

File tree

1 file changed

+49
-59
lines changed

1 file changed

+49
-59
lines changed

graphtik/jsonpointer.py

Lines changed: 49 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from functools import partial
2222
from typing import (
2323
Any,
24-
Callable,
2524
Collection,
2625
Iterable,
2726
List,
@@ -291,75 +290,57 @@ def is_collection(item):
291290
)
292291

293292

294-
def set_or_concatenate_dataframe(doc: Doc, key, value, concat_axis) -> Optional[Doc]:
293+
def list_scouter(doc: Doc, idx, mother, overwrite) -> Tuple[Any, Optional[Doc]]:
295294
"""
296-
Set item or if both `doc` & `value` are dataframes, concat and return `doc`.
295+
Get `doc `list item by (int) `idx`, or create a new one from `mother`.
297296
298-
:return:
299-
`doc` is not None only if it has changed, due to concatanation,
300-
and needs to be replaced in its parent container.
301-
"""
302-
if (
303-
concat_axis is not None
304-
and isinstance(doc, NDFrame)
305-
and isinstance(value, NDFrame)
306-
):
307-
return pd.concat((doc, value), axis=concat_axis)
308-
else:
309-
doc[key] = value
310-
311-
312-
def list_scouter(
313-
doc: Doc, part, container_factory, overwrite
314-
) -> Tuple[Any, Optional[Doc]]:
315-
"""
316-
Get `doc `list item by (int) `part`, or create a new one from `container_factory`.
317-
318-
:param container_factory:
319-
when called may return an intermediate container or the "value"
297+
:param mother:
298+
factory producing the child containers to extend missing steps,
299+
or the "child" value (when `overwrite` is true).
320300
321301
:return:
322302
a 2-tuple (child, ``None``)
323303
324304
NOTE: must come after collection-scouter due to special ``-`` index collision.
325305
"""
326-
if part == "-": # It means the position after the last item.
327-
item = container_factory()
306+
if idx == "-": # It means the position after the last item.
307+
item = mother()
328308
doc.append(item)
329309
return item, None
330310

331-
part = int(part) # Let it bubble, to try next (as key-item).
311+
idx = int(idx) # Let it bubble, to try next (as key-item).
332312
if overwrite and log.isEnabledFor(logging.WARNING):
333313
try:
334-
child = doc[part]
314+
child = doc[idx]
335315
except LookupError:
336316
# Out of bounds, but ok for now, will break also on assignment, below.
337317
pass
338318
else:
339319
if not is_collection(child):
340-
_log_overwrite(part, doc, child)
320+
_log_overwrite(idx, doc, child)
341321
elif not overwrite:
342322
try:
343-
child = doc[part]
323+
child = doc[idx]
344324
if is_collection(child):
345325
return child, None
346326
except LookupError:
347327
# Ignore resolve errors, assignment errors below are more important.
348328
pass
349329

350-
item = container_factory()
351-
doc[part] = item
330+
item = mother()
331+
doc[idx] = item
352332
return item, None
353333

354334

355335
def collection_scouter(
356-
doc: Doc, part, container_factory: Callable[[], Any], overwrite, concat_axis
336+
doc: Doc, key, mother, overwrite, concat_axis
357337
) -> Tuple[Any, Optional[Doc]]:
358338
"""
359-
Get item `part` from `doc` collection, or create a new ome from `container_factory`.
339+
Get item `key` from `doc` collection, or create a new ome from `mother`.
360340
361-
:param container_factory:
362-
when called may return an intermediate container or the "value"
341+
:param mother:
342+
factory producing the child containers to extend missing steps,
343+
or the "child" value (when `overwrite` is true).
363344
364345
:return:
365346
a 2-tuple (child, doc) where `doc` is not None if it needs to be replaced
@@ -368,49 +349,58 @@ def collection_scouter(
368349
if (
369350
overwrite
370351
and log.isEnabledFor(logging.WARNING)
371-
and part in doc
372-
and not is_collection(doc[part])
352+
and key in doc
353+
and not is_collection(doc[key])
373354
):
374-
_log_overwrite(part, doc, doc[part])
355+
_log_overwrite(key, doc, doc[key])
375356
elif not overwrite:
376357
try:
377-
child = doc[part]
358+
child = doc[key]
378359
if is_collection(child):
379360
return child, None
380361
except LookupError:
381362
# Ignore resolve errors, assignment errors below are more important.
382363
pass
383364

384-
item = container_factory()
385-
doc = set_or_concatenate_dataframe(doc, part, item, concat_axis)
386-
return item, doc
365+
new_doc = None
366+
value = mother()
387367

368+
if (
369+
concat_axis is not None
370+
and isinstance(doc, NDFrame)
371+
and isinstance(value, NDFrame)
372+
):
373+
new_doc = pd.concat((doc, value), axis=concat_axis)
374+
else:
375+
doc[key] = value
388376

389-
def object_scouter(
390-
doc: Doc, part, container_factory, overwrite
391-
) -> Tuple[Any, Optional[Doc]]:
377+
return value, new_doc
378+
379+
380+
def object_scouter(doc: Doc, attr, mother, overwrite) -> Tuple[Any, Optional[Doc]]:
392381
"""
393-
Get attribute `part` in `doc` object, or create a new one from `container_factory`.
382+
Get attribute `attr` in `doc` object, or create a new one from `mother`.
394383
395-
:param container_factory:
396-
when called may return an intermediate container or the "value"
384+
:param mother:
385+
factory producing the child containers to extend missing steps,
386+
or the "child" value (when `overwrite` is true).
397387
398388
:return:
399389
a 2-tuple (child, ``None``)
400390
"""
401-
if overwrite and log.isEnabledFor(logging.WARNING) and hasattr(doc, part):
402-
_log_overwrite(part, doc, getattr(doc, part))
391+
if overwrite and log.isEnabledFor(logging.WARNING) and hasattr(doc, attr):
392+
_log_overwrite(attr, doc, getattr(doc, attr))
403393
elif not overwrite:
404394
try:
405-
child = getattr(doc, part)
395+
child = getattr(doc, attr)
406396
if is_collection(child):
407397
return child, None
408398
except AttributeError:
409399
# Ignore resolve errors, assignment errors below are more important.
410400
pass
411401

412-
item = container_factory()
413-
setattr(doc, part, item)
402+
item = mother()
403+
setattr(doc, attr, item)
414404
return item, None
415405

416406

@@ -427,7 +417,7 @@ def set_path_value(
427417
Set `value` into a :term:`jsonp` `path` within the referenced `doc`.
428418
429419
Special treatment (i.e. concat) if must insert a DataFrame into a DataFrame
430-
with steps ``.``(vertical) and ``-``(horizontal) denoting concatanation axis.
420+
with steps ``.``(vertical) and ``-``(horizontal) denoting concatenation axis.
431421
432422
:param doc:
433423
the document to extend & insert `value`
@@ -444,8 +434,8 @@ def set_path_value(
444434
https://tools.ietf.org/id/draft-handrews-relative-json-pointer-00.html
445435
446436
:param container_factory:
447-
a factory producing the collection instances to extend missing steps,
448-
(usually a mapping or a sequence)
437+
a factory producing the container to extend missing steps
438+
(usually a mapping or a sequence).
449439
:param root:
450440
From where to start resolving absolute paths, double-slashes(``//``) or
451441
final slashes.
@@ -455,7 +445,7 @@ def set_path_value(
455445
If true, a last ditch effort is made for each part, whether it matches
456446
the name of an attribute of the parent item.
457447
:param concat_axis:
458-
if 0 or 1, applies :term:'pandas concatanation` vertically or horizontally,
448+
if 0 or 1, applies :term:'pandas concatenation` vertically or horizontally,
459449
by clipping last step when traversing it and doc & value are both Pandas objects.
460450
461451
:raises ValueError:

0 commit comments

Comments
 (0)