Skip to content

Commit cb54f88

Browse files
committed
Reorder dict cleaning for performance
1 parent 80be8bd commit cb54f88

File tree

1 file changed

+13
-6
lines changed
  • packages/python/plotly/plotly/io

1 file changed

+13
-6
lines changed

Diff for: packages/python/plotly/plotly/io/_json.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ def clean_to_json_compatible(obj, **kwargs):
429429
if isinstance(obj, (int, float, string_types)):
430430
return obj
431431

432+
if isinstance(obj, dict):
433+
return {k: clean_to_json_compatible(v, **kwargs) for k, v in obj.items()}
434+
elif isinstance(obj, (list, tuple)):
435+
if obj:
436+
# Must process list recursively even though it may be slow
437+
return [clean_to_json_compatible(v, **kwargs) for v in obj]
438+
432439
# unpack kwargs
433440
numpy_allowed = kwargs.get("numpy_allowed", False)
434441
datetime_allowed = kwargs.get("datetime_allowed", False)
@@ -439,12 +446,6 @@ def clean_to_json_compatible(obj, **kwargs):
439446
pd = modules["pd"]
440447
image = modules["image"]
441448

442-
# Plotly
443-
try:
444-
obj = obj.to_plotly_json()
445-
except AttributeError:
446-
pass
447-
448449
# Sage
449450
if sage_all is not None:
450451
if obj in sage_all.RR:
@@ -522,6 +523,12 @@ def clean_to_json_compatible(obj, **kwargs):
522523
if image is not None and isinstance(obj, image.Image):
523524
return ImageUriValidator.pil_image_to_uri(obj)
524525

526+
# Plotly
527+
try:
528+
obj = obj.to_plotly_json()
529+
except AttributeError:
530+
pass
531+
525532
# Recurse into lists and dictionaries
526533
if isinstance(obj, dict):
527534
return {k: clean_to_json_compatible(v, **kwargs) for k, v in obj.items()}

0 commit comments

Comments
 (0)