(Aug-26-2024, 09:44 PM)deanhystad Wrote: Quote:is the format you append to the dictionary name before .key to specify a nested dictionary.
Not quite right.
new_test_dict is a dictionary.
new_test_dict['PCFData'] returns the value associated with the key 'PCFData' in new_test_dict. The value must be a dictionary because you use "Q_STATISTICS_DATA" to index the value.
You are not "appending" a "format" to the dictionary name. You are "indexing" a dictionary using a key. Maybe it is more clear when written this way.
temp = new_test_dict['PCFData']
temp = temp['Q_STATISTICS_DATA']
temp = temp['SYSTEM.ADMIN.COMMAND.QUEUE']
print(temp.keys())
I looked at the python-benedict package mention by bowlofred. I don't think that would work because you have "." in some of your dictionary keys (i.e. 'SYSTEM.ADMIN.COMMAND.QUEUE). That was an unfortunate choice.
This is an unfriendly format. Difficult to parse.
Quote:['PCFData']['Q_STATISTICS_DATA']['SYSTEM.ADMIN.COMMAND.QUEUE']
I would pick something like this.
Quote:'PCFData|Q_STATISTICS_DATA|SYSTEM.ADMIN.COMMAND.QUEUE'
Then you could use something like I posted in your other thread.
https://python-forum.io/thread-42716-pos...#pid180738
class PathDictionary:
def __init__(self, dictionary):
self.dictionary = dictionary
def __getitem__(self, path):
value = self.dictionary
for part in path.split("|"): # Choose a separator that is not found in any of the keys.
value = value[part]
return value
To use:
cmdq = 'PCFData|Q_STATISTICS_DATA|SYSTEM.ADMIN.COMMAND.QUEUE'
pathdict = PathDictionary(new_data_dict)
print(pathdict[cmdq].keys())
This is the full data stream so you get what I am parsing. I have it working. just nee to concatenate
new_test_dict with a string variable containing
['PCFData']['Q_STATISTICS_DATA']['SYSTEM.ADMIN.COMMAND.QUEUE']
and '.keys'
It works when it is hard-coded. But I have a LIST of those paths and want to iterate through the list sub'ing the path values in the list into the .keys and .value extraction.
{'reason': 'NONE', 'MQMD': {'StrucId': 'MD', 'Version': 1, 'Report': 'NONE', 'MsgType': 'DATAGRAM', 'Expiry': -1, 'Feedback': 'NONE', 'Encoding': 546, 'CodedCharSetId': 1208, 'Format': 'MQADMIN', 'Priority': 0, 'Persistence': 'NOT_PERSISTENT', 'MsgId': '0x414d5120424f424245453220202020200e57ba66feed0840', 'CorrelId': '0x000000000000000000000000000000000000000000000000', 'BackoutCount': 0, 'ReplyToQ': '', 'ReplyToQMgr': 'BOBBEE2', 'UserIdentifier': '', 'AccountingToken': '0x0000000000000000000000000000000000000000000000000000000000000000', 'ApplIdentityData': '', 'PutApplType': 'QMGR', 'PutApplName': 'BOBBEE2', 'PutDate': '20240826', 'PutTime': '16115225', 'ApplOriginData': '', 'GroupId': '0x000000000000000000000000000000000000000000000000', 'MsgSeqNumber': 1, 'Offset': 0, 'MsgFlags': 0, 'OriginalLength': -1}, 'PCFheader': {'Type': 'STATISTICS', 'StrucLength': 36, 'Version': 3, 'Command': 'STATISTICS_Q', 'MsgSeqNumber': 1, 'Control': 'LAST', 'CompCode': 0, 'Reason': 0, 'ParameterCount': 8, 'sReason': 'NONE'}, 'PCFData': {'Q_MGR_NAME': 'BOBBEE2', 'START_DATE': '2024-08-26', 'START_TIME': '09.10.52', 'END_DATE': '2024-08-26', 'END_TIME': '09.11.52', 'COMMAND_LEVEL': 940, 'OBJECT_COUNT': 1, 'Q_STATISTICS_DATA': {'SYSTEM.ADMIN.COMMAND.QUEUE': {'Q_NAME': 'SYSTEM.ADMIN.COMMAND.QUEUE', 'CREATION_DATE': '2023-04-18', 'CREATION_TIME': '12.32.19', 'Q_TYPE': 'LOCAL', 'DEFINITION_TYPE': 'PREDEFINED', 'Q_MIN_DEPTH': 0, 'Q_MAX_DEPTH': 0, 'AVG_Q_TIME': [0, 0], 'PUTS': [9, 0], 'PUTS_FAILED': 0, 'PUT1S': [0, 0], 'PUT1S_FAILED': 0, 'PUT_BYTES': [728, 0], 'GETS': [9, 0], 'GET_BYTES': [728, 0], 'GETS_FAILED': 0, 'BROWSES': [0, 0], 'BROWSE_BYTES': [0, 0], 'BROWSES_FAILED': 0, 'MSGS_NOT_QUEUED': 9, 'MSGS_EXPIRED': 0, 'MSGS_PURGED': 0}}}, 'time': '2024-08-26 16:24:11.910772', 'delta': '0:00:00'}
Method 1 - Using keys() method:
['reason', 'MQMD', 'PCFheader', 'PCFData', 'time', 'delta']