Skip to content

Commit

Permalink
[refactor] Improve performance of flatten dict function (#2896)
Browse files Browse the repository at this point in the history
[refactor] Replace redundant `MutableMapping` type into `dict`
  • Loading branch information
KaroMourad authored Jul 10, 2023
1 parent e28741c commit 2d7d994
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 6 deletions.
3 changes: 1 addition & 2 deletions pkgs/aimstack/asp/boards/audios.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections.abc import MutableMapping
from asp import AudioSequence

audios = AudioSequence.filter()
Expand All @@ -9,7 +8,7 @@ def flatten(dictionary, parent_key='', separator='.'):
items = []
for key, value in dictionary.items():
new_key = parent_key + separator + key if parent_key else key
if isinstance(value, MutableMapping):
if isinstance(value, dict):
items.extend(flatten(value, new_key, separator=separator).items())
else:
items.append((new_key, value))
Expand Down
3 changes: 1 addition & 2 deletions pkgs/aimstack/asp/boards/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from asp import Run, Metric
from itertools import groupby
from collections.abc import MutableMapping
import math

if 'hash' in session_state:
Expand Down Expand Up @@ -30,7 +29,7 @@ def flatten(dictionary, parent_key='', separator='.'):
flattened = {}
for key, value in dictionary.items():
new_key = f"{parent_key}{separator}{key}" if parent_key else key
if isinstance(value, MutableMapping):
if isinstance(value, dict):
flattened.update(flatten(value, new_key, separator=separator))
else:
flattened[new_key] = value
Expand Down
3 changes: 1 addition & 2 deletions pkgs/aimstack/asp/boards/runs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from asp import Run
from collections.abc import MutableMapping

form = ui.form('Search')

Expand All @@ -12,7 +11,7 @@ def flatten(dictionary, parent_key='', separator='.'):
flattened = {}
for key, value in dictionary.items():
new_key = f"{parent_key}{separator}{key}" if parent_key else key
if isinstance(value, MutableMapping):
if isinstance(value, dict):
flattened.update(flatten(value, new_key, separator=separator))
else:
flattened[new_key] = value
Expand Down

0 comments on commit 2d7d994

Please sign in to comment.