Here is a dictionary I have as input
user_vars = {
"verbose": true,
"testing": false,
"login": {
"username": "theadmin",
"password": "password123"
},
"interfaces": {
"trust": {
"ip": "10.8.10.1/30"
},
"untrust": {
"ip": "10.9.10.1/30"
}
}
}
I found this article on how to create an object from a class: https://bobbyhadz.com/blog/python-convert-nested-dictionary-to-object#:~:text=To%20convert%20a%20nested%20dictionary,the%20key%20to%20the%20value.
So I did this:
class payloadGen:
def __init__(self, **kwargs):
for key, value in kwargs.items():
if isinstance(value, dict):
self.__dict__[key] = payloadGen(**value)
else:
self.__dict__[key] = value
So when I call it I am able to do the following just fine:
customer = payloadGen(**user_vars)
print(customer.interfaces.trust.ip)
print(customer.verbose)
#output
10.8.10.1/30
True
But when I try to print a parent of nested values, I get the object memory address:
print(customer.interfaces)
#output
<payloadgen.payloadGen object at 0x7fbe8cedd7f0>
This is expected. What I want to be able to do is print all child keys and values recursively:
print(customer.interfaces)
#wanted output
"trust": {"ip": "10.8.10.1/30"},"untrust": {"ip": "10.9.10.1/30"}
I realize this maybe a complicated affair. I am new to class's, constructors, inherence, and objects. So if need to do some more reading on how to accomplish this please point me in the right direction.
EDIT: Thanks to @Axe319. I needed to just adjust the class to add a representor call, and use a lambda function.
class payloadGen:
def __init__(self, **kwargs):
for key, value in kwargs.items():
if isinstance(value, dict):
self.__dict__[key] = payloadGen(**value)
else:
self.__dict__[key] = value
__repr__ = lambda self: repr(self.__dict__)
Now I can call individual parent entries and all child parent/children display
customer = payloadGen(**user_vars)
print(customer.interfaces.trust.ip)
print(customer.interfaces)
#output
10.8.10.1/30
{interfaces": {"trust": {"ip": "10.8.10.1/30"},"untrust": {"ip": "10.9.10.1/30"}}}