I have created a method to return the maximum lengths of the keys and values returned by calling vars(object). I can see that it iterates properly but returns the values in effect prior to recursion.
def get_max_lengths(an_object, key_length, value_length):
a_dict = vars(an_object)
max_key_length = key_length
max_value_length = value_length
for key, value in a_dict.items():
if str(value).find("__main__.") > 0 and str(value).find(" object at ") > 0:
#-> Recurse this object to measure its variables
get_max_lengths(value, max_key_length, max_value_length)
else:
if len(str(key)) > max_key_length:
max_key_length = len(key)
print((max_key_length, max_value_length))
if len(str(value)) > max_value_length:
max_value_length = len(str(value))
print((max_key_length, max_value_length))
return (max_key_length, max_value_length)
As I iterate the routine, it does accumulate the correct values but doesn’t return them
Tims's Car before creating oil_specs:
: Variables :
: vin : Unknown :
: make : Hundai :
: model : Elantra :
: owner : Tim :
: year : 2017 :
: color : blue :
Tims's Car after calling set_oil_capacity volume and units:
: Variables :
: vin : Unknown :
: make : Hundai :
: model : Elantra :
: owner : Tim :
: year : 2017 :
: color : blue :
: oil_specs : <main.Oil_Specs object at 0x000002036C4D5C90> :
Tims's Car's Oil Spec:
: Variables :
: oil_capacity_volume : 5 :
: oil_capacity_units : quarts :
: oil_preferred_type : Synthetic :
(3, 0)
(3, 7)
(4, 7)
(5, 7)
(19, 7)
(19, 9) # It should return this tupple.
The field lengths returned from get_max_lengths are (5, 7)