Skip to content
Advertisement

How to look for specific values in a dictionary and return them properly

So I have a list dictionary of dictionaries (lst) that I’m trying to iterate through, compare values, and return the appropriate values.

I have the following code to retrieve 2 arguments given from the command line, compare them through the dictionaries entries, and return the appropriate value:

def get_attribute_number(cmd1, cmd2, lst):

    app_name = cmd1
    attribute = cmd2
    for name in lst: #lst is a dictionary
        if name["description"] == app_name:
            print(int(name[attribute]))

get_attribute_number(cmd1=sys.argv[1], cmd2=sys.argv[2], lst=data_list)

However, my program is not returning anything. It is supposed to for example if I provide the cmd line arguments python3 program.py [NAME] [ATTRIBUTE], is supposed to query the above data until it finds a match for the name, and return its requested attribute as an integer. Can anyone offer suggestions on why I’m not able to print(int(name[attribute])) for the corresponding napp_name?

Advertisement

Answer

i changes your code a little to be more friendly to user:

import sys

data_list = [{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos']", '{#PASSED}': '316', '{#FAILED}': '1', '{#WARNING}': '204', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/python-openflow']", '{#PASSED}': '400', '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': '40', '{#XFAILED}': '1'},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_core']", '{#PASSED}': '49', '{#FAILED}': 0, '{#WARNING}': '39', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos-utils']", '{#PASSED}': '102', '{#FAILED}': '2', '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/mef_eline']", '{#PASSED}': '102', '{#FAILED}': 0, '{#WARNING}': '88', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/flow_manager']", '{#PASSED}': '36', '{#FAILED}': 0, '{#WARNING}': '22', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/topology']", '{#PASSED}': '58', '{#FAILED}': 0, '{#WARNING}': '56', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/kronos']", '{#PASSED}': '61', '{#FAILED}': 0, '{#WARNING}': '21', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/pathfinder']", '{#PASSED}': 0, '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/status']", '{#PASSED}': '81', '{#FAILED}': 0, '{#WARNING}': '1', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_l2ls']", '{#PASSED}': '7', '{#FAILED}': 0, '{#WARNING}': '7', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/of_lldp']", '{#PASSED}': '17', '{#FAILED}': 0, '{#WARNING}': '17', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['success']", '{#DESCRIPTION}': "['kytos/maintenance']", '{#PASSED}': '28', '{#FAILED}': 0, '{#WARNING}': '28', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/storehouse']", '{#PASSED}': '63', '{#FAILED}': '1', '{#WARNING}': '32', '{#SKIPPED}': 0, '{#XFAILED}': 0},
{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos-end-to-end-tester']", '{#PASSED}': 0, '{#FAILED}': 0, '{#WARNING}': 0, '{#SKIPPED}': 0, '{#XFAILED}': 0}]

def get_attribute_number(cmd1, cmd2, lst):
    """For each attribute under a napp, retrieve that attribute as a NUMBER"""
    napp_name = cmd1
    attribute_name = cmd2
    for napp in lst: #lst is a dictionary
        if napp_name in napp["{#DESCRIPTION}"]: # change happened here
            print(napp)
            print(int(napp[attribute_name]))

get_attribute_number(sys.argv[1], sys.argv[2], data_list)

basicly thay need to enter just substring to get some results, so this:

python tt.py 'kytos/kytos' {#SKIPPED} 

outputs only desired data:

{'{#Status}': "['failed']", '{#DESCRIPTION}': "['kytos/kytos']", '{#PASSED}': '316', '{#FAILED}': '1', '{#WARNING}': '204', '{#SKIPPED}': 0, '{#XFAILED}': 0}
0
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement