I'm trying to parse a string that has been formatted using inspect.formatargvalues
and I want to be able to extract the argument values and save them in a dictionary.
Here are some examples of the text:
CALL function.a(timeout_secs=10)
CALL function.b(x=something good (happy days, ABC 123), y_z=None, check_this=True, check_this_2=None, extra_info=None, press=False, timeout_secs=30)
CALL function.c(key='A_B')
CALL function.d(target=<Tab.Chrome: 'tab-chrome'>)
CALL function.e(music_type=<MusicType.ALBUM: 1>, term=None, genre=None, results=True)
CALL function.f(button='END', delay=None)
At the moment I'm extracting the parameters using regex to get:
timeout_secs=10
x=something good (happy days, ABC 123), y_z=None, check_this=True, check_this_2=None, extra_info=None, press=False, timeout_secs=30
key='A_B'
target=<Tab.Chrome: 'tab-chrome'>
music_type=<MusicType.ALBUM: 1>, term=None, genre=None, results=True
button='END', delay=None
Then I'm splitting on ',' and then converting the array into a dictionary.
dict(parameter.split("=") for parameter in array_of_parameters)
This works for the most part but obviously breaks when parsing:
x=something good (happy days, ABC 123), y_z=None, check_this=True, check_this_2=None, extra_info=None, press=False, timeout_secs=30
Is there a way to convert the arguments to a dictionary where it covers all these scenarios?