From fd2feeb2b3ecf7a6fabeb6d9effa3982e7082be9 Mon Sep 17 00:00:00 2001 From: Avi <25426909+ivavi9@users.noreply.github.com> Date: Sun, 28 May 2023 00:31:36 +0530 Subject: [PATCH] modified the asdict functionality to optionally recursively give suds object (#92) --- suds/sudsobject.py | 41 +++++++++++++++++++++++++- tests/test_utils.py | 72 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 tests/test_utils.py diff --git a/suds/sudsobject.py b/suds/sudsobject.py index 6493842c..dcee400c 100644 --- a/suds/sudsobject.py +++ b/suds/sudsobject.py @@ -41,7 +41,7 @@ def items(sobject): yield item -def asdict(sobject): +def asdict(sobject, recursive=False): """ Convert a sudsobject into a dictionary. @@ -51,8 +51,47 @@ def asdict(sobject): @rtype: dict """ + if recursive: + return recursive_asdict(sobject) return dict(items(sobject)) +def recursive_asdict(suds_object): + """Convert a suds object to a dictionary. + + Args: + suds_object: A suds object + + Returns: + A python dictionary containing the items contained in the suds object. + """ + + # Create an empty dictionary. + output_dict = {} + + # Iterate over the items in the suds object. + for key, value in asdict(suds_object).items(): + + # If the value is a suds object, recursively convert it to a dictionary. + if hasattr(value, "__keylist__"): + output_dict[key] = recursive_asdict(value) + + # If the value is a list, recursively convert each item in the list to a dictionary. + elif isinstance(value, list): + output_dict[key] = [] + for item in value: + if hasattr(item, "__keylist__"): + output_dict[key].append(recursive_asdict(item)) + else: + output_dict[key].append(item) + + # Otherwise, just add the value to the dictionary. + else: + output_dict[key] = value + + # Return the dictionary. + return output_dict + + def merge(a, b): """ Merge all attributes and metadata from I{a} to I{b}. diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..d09fcdf7 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,72 @@ +import pytest +if __name__ == "__main__": + import testutils + testutils.run_using_pytest(globals()) + +from suds.sudsobject import Object +from suds.sudsobject import asdict, recursive_asdict + + +# test_suds_functions.py + +def test_asdict(): + # Create a suds object + sobject = Object() + sobject.name = "John Doe" + sobject.age = 30 + sobject.children = [] + + # Call the asdict function + result = asdict(sobject) + + # Assert the expected result + expected = { + 'name': 'John Doe', + 'age': 30, + 'children': [] + } + assert result == expected + + +def test_recursive_asdict(): + # Create a suds object with nested objects + outer_object = Object() + outer_object.name = "John Doe" + outer_object.age = 30 + outer_object.children = [] + + inner_object = Object() + inner_object.name = "Jane Doe" + inner_object.age = 25 + inner_object.children = [] + + outer_object.children.append(inner_object) + + grandchild_object = Object() + grandchild_object.name = "Baby Doe" + grandchild_object.age = 1 + + inner_object.children.append(grandchild_object) + + # Call the recursive_asdict function + result = asdict(outer_object,True) + + # Assert the expected result + expected = { + 'name': 'John Doe', + 'age': 30, + 'children': [ + { + 'name': 'Jane Doe', + 'age': 25, + 'children': [ + { + 'name': 'Baby Doe', + 'age': 1 + } + ] + } + ] + } + assert result == expected +