Skip to content

Commit

Permalink
Remove items called node and edges.
Browse files Browse the repository at this point in the history
  • Loading branch information
everaldorodrigo committed Oct 23, 2024
1 parent f0f4849 commit 4a9f443
Showing 1 changed file with 43 additions and 42 deletions.
85 changes: 43 additions & 42 deletions src/hub/dataload/sources/civic/civic_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,48 +23,49 @@ def merge_dicts(d1, d2):
return merged


def remove_nodes_and_edges(data):
if isinstance(data, dict):
new_data = {}
for key, value in data.items():
if key in ['node', 'nodes', 'edge', 'edges']:
# Recursively process the value for 'node', 'nodes', 'edge', 'edges'
if isinstance(value, list):
# If the value is a list, process each item and return as a list
new_data = remove_nodes_and_edges(value)
else:
# If it's not a list, it's a dictionary or other structure
new_data.update(remove_nodes_and_edges(value))
else:
# For other keys, recursively process the value
new_data[key] = remove_nodes_and_edges(value)
return new_data
elif isinstance(data, list):
# If it's a list, process each item in the list and return as a list
return [remove_nodes_and_edges(item) for item in data]
else:
# If it's neither a dict nor a list, return the value as is
return data


# def remove_nodes_and_edges(obj):
# if not obj or type(obj) in [str, bool, int, float]:
# return obj

# if isinstance(obj, list):
# return [remove_nodes_and_edges(item) for item in obj]

# if 'edges' in obj:
# return [remove_nodes_and_edges(edge['node']) for edge in obj['edges']]

# # if 'nodes' in obj:
# # return [remove_nodes_and_edges(node) for node in obj['nodes']]

# return {
# key: remove_nodes_and_edges(value)
# for key, value in obj.items()
# if key != 'node'
# }
# def remove_nodes_and_edges(data):
# if isinstance(data, dict):
# new_data = {}
# for key, value in data.items():
# if key in ['node', 'nodes', 'edge', 'edges']:
# # Recursively process the value for 'node', 'nodes', 'edge', 'edges'
# if isinstance(value, list):
# # If the value is a list, process each item and return as a list
# new_data = remove_nodes_and_edges(value)
# else:
# # If it's not a list, it's a dictionary or other structure
# new_data.update(remove_nodes_and_edges(value))
# else:
# # For other keys, recursively process the value
# new_data[key] = remove_nodes_and_edges(value)
# return new_data
# elif isinstance(data, list):
# # If it's a list, process each item in the list and return as a list
# return [remove_nodes_and_edges(item) for item in data]
# else:
# # If it's neither a dict nor a list, return the value as is
# return data


def remove_nodes_and_edges(obj):
if not obj or type(obj) in [str, bool, int, float]:
return obj

if isinstance(obj, list):
return [remove_nodes_and_edges(item) for item in obj]

if 'edges' in obj:
return [remove_nodes_and_edges(edge['node']) for edge in obj['edges']]

# if 'nodes' in obj:
# return [remove_nodes_and_edges(node) for node in obj['nodes']]

return {
key: remove_nodes_and_edges(value)
for key, value in obj.items()
if key != 'node'
}


def get_id(doc):
try:
Expand Down

0 comments on commit 4a9f443

Please sign in to comment.