Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dictionaries
#1
Hi

I'm struggling with this variable that i think is a dictionary

This is what i have

page = [
    {'id': 1, 'name': 'initial',
     'components': [
         {'id': 1, 'type': 'button', 'name': 'bt_settings'},
         {'id': 2, 'type': 'button', 'name': 'bt_sensors'},
         {'id': 3, 'type': 'button', 'name': 'bt_start'},
     ]
     },
    {'id': 2, 'name': 'settings',
     'components': [
         {'id': 1, 'type': 'text', 'name': 'txt_filename'},
         {'id': 2, 'type': 'text', 'name': 'txt_googlecrd'},
     ]
     },
    {'id': 3, 'name': 'testType',
     'components': [
         {'id': 11, 'type': 'button', 'name': ''},
         {'id': 12, 'type': 'text', 'name': 'txt_other'},
     ]
     },
]
Now let's suppose that for the component 'txt_filename' in page 'settings' i want to know the respective id fields? In this case page id = 2 and component id = 1
I'm very confused. Should this be a class? how do i do it?
help here
Reply
#2
Use Python to inspect the object:

dataset = page = [
    {'id': 1, 'name': 'initial',
     'components': [
         {'id': 1, 'type': 'button', 'name': 'bt_settings'},
         {'id': 2, 'type': 'button', 'name': 'bt_sensors'},
         {'id': 3, 'type': 'button', 'name': 'bt_start'},
     ]
     },
    {'id': 2, 'name': 'settings',
     'components': [
         {'id': 1, 'type': 'text', 'name': 'txt_filename'},
         {'id': 2, 'type': 'text', 'name': 'txt_googlecrd'},
     ]
     },
    {'id': 3, 'name': 'testType',
     'components': [
         {'id': 11, 'type': 'button', 'name': ''},
         {'id': 12, 'type': 'text', 'name': 'txt_other'},
     ]
     },
]
type(dataset)
Output:
list
A list is a sequence of objects. Accessing the items is possible with iteration in a for-loop and/or using the index/slice access.

Let's inspect the list:
for element in dataset:
    print('Element', type(element), element)
Now we see, that the list has only elements which are dicts. The next step should be to iterate over the list and compare the vlaue of the name with the string literal 'settings'.

for element in dataset:
    if element['name'] == 'settings':
        print(element)
Output:
{'id': 2, 'name': 'settings', 'components': [{'id': 1, 'type': 'text', 'name': 'txt_filename'}, {'id': 2, 'type': 'text', 'name': 'txt_googlecrd'}]}
Ok, we got the right 'row'. You need the page_id, which is in the dict and the component_id which is in the dicts, which are in a list.
This will end in a nested loop:

for element in dataset:
    if element['name'] == 'settings':
        page_id = element['id']
        for component in element['components']:
            if component['name'] == 'txt_filename':
                component_id = component['id']
                print('page_id:', page_id)
                print('component_id:', component_id)
                break
Output:
page_id: 2 component_id: 1
The next step should be to make a function. The function should take as arguments the data and the name, which you are searching for. The function should return the both values page_id and component_id.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
thanks

and if it is inside a class

class NextionApp:
    def __init__(self):
        pages = [
            {'id': '0', 'name': 'credits',
             'components': [
                 {'id': '0', 'type': 'button', 'name': 'b0'},
             ]
             },
            {'id': '1', 'name': 'initial',
             'components': [
                 {'id': '1', 'type': 'button', 'name': 'bt_settings'},
                 {'id': '2', 'type': 'button', 'name': 'bt_sensors'},
                 {'id': '3', 'type': 'button', 'name': 'bt_start'},
             ]
             },
how that would it change the call of the search?

(Mar-07-2018, 11:07 AM)ricardons Wrote: thanks

and if it is inside a class

class NextionApp:
    def __init__(self):
        pages = [
            {'id': '1', 'name': 'initial',
             'components': [
                 {'id': '1', 'type': 'button', 'name': 'bt_settings'},
                 {'id': '2', 'type': 'button', 'name': 'bt_sensors'},
                 {'id': '3', 'type': 'button', 'name': 'bt_start'},
             ]
             },
           {'id': '2', 'name': 'settings',
             'components': [
                 {'id': '1', 'type': 'text', 'name': 'txt_filename'},
                 {'id': '2', 'type': 'text', 'name': 'txt_googlecrd'},
                 {'id': '3', 'type': 'text', 'name': 'txt_duration'},
                 {'id': '4', 'type': 'text', 'name': 'txt_interval'},
                 {'id': '5', 'type': 'text', 'name': 'txt_no_reads'},
                 {'id': '6', 'type': 'button', 'name': 'b0'},
                 {'id': '7', 'type': 'button', 'name': 'b1'},
             ]
             },
how that would it change the call of the search?

sorry about the mess in the messages. consider only the last reply
Reply
#4
I think it's the wrong way. Think about how this class should work.
You have some input, in this case this datastructure in python code.
Then you have some attributes. What should you store inside the class?

A simple function is the first step. Before you think about classes, do it as a function.
def get_page_component(dataset):
    for element in dataset:
        if element['name'] == 'settings':
            page_id = element['id']
            for component in element['components']:
                if component['name'] == 'txt_filename':
                    component_id = component['id']
                    # leaving the loop here
                    return page_id, component_id

page_id, component_id = get_page_component(dataset)
But what are you really trying? Storing this data into a class hardcoded is maybe not what you want.
This dataset is coming from somewhere or it's just an example to learn?
By the way, you assign to a local variable in the __init__ method.
Local context inside a function is destroyed when leaving the function.
You can assign pages to a class attribute.
In this case it's available and shared across all instances of the class.
If you want to store an attribute in a method call of a class, use self.pages = ....

For me it's too late. Too tired.

You should read more examples. It's normally bad to come up with a solution.
I learned a lot from mistakes I made.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Looks like JSON to me.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
look very cool

User has been warned for this post. Reason: Necro'd thread
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020