pythonkivyjsonstore

How to loop for values in JsonStore kivy application?


I've been struggling to find a way to iterate through a certain set of values in multiple objects using the JsonStore module. My code:

class MainApp(App):

    def build(self): # build() returns an instance
        self.store = JsonStore("streak.json") # file that stores the streaks:


        return presentation

    def check_streak(self, instance):



        for value in self.store.find('delta'):

            if value > time.time():
                print("early")

            if value == time.time():
                print("on time")

            if value < time.time():
                print("late")

This function is connected to different buttons that are displayed on the page:

def display_btn(self):
        # display the names of the streaks in a list on PageTwo
        for key in self.store:
            streak_button = Button(text=key, on_press=self.check_streak)
            self.root.screen_two.ids.streak_zone.add_widget(streak_button)

When I use the check_streak I get TypeError: find() takes 1 positional argument but 2 were given

What's inside the json file:

{"first": {"action": "first", "action_num": "1", "seconds": 60, "score": 0, "delta": 1555714261.0438898}, "second": {"action": "second", "action_num": "2", "seconds": 120, "score": 0, "delta": 1555879741.894656}}

Notice that every object starts with its name, in this case, "first" and "second". I want to be able to iterate through every objects "delta" key and get its value. Once I get the value of that objects 'delta' I will then compare it to the current time.

I have been referred to a question that involves generating ids but I don't see how that is related to my problem. Although I think a generator is good for creating random numbers, The data I'm working with isn't random. If using a generator is the only way to do what I'm trying to do could someone please explain to me how I could use it in my code?

The answers I have previously received don't account for the fact that I want the "delta" values to still be attached to the object rather than just listing them.


Solution

  • How to use recursive iteration through nested json for specific key in python

    The following example does not use JsonStore. It is using json.load to load JSON objects.

    Snippets

    import json
    ...
        def check_streak(self, *args):
            with open("streak.json", "r") as read_file:
                data = json.load(read_file)
    
                for honey in item_generator(data, 'delta'):
                    print(f"honey={honey}")
                    print(f"type(honey)={type(honey)}")
    
                    if honey > time.time():
                        print("early")  # test
    
                    if honey == time.time():
                        print("on time")
    
                    if honey < time.time():
                        print("late")
    

    Note

    The store.find(key='value') function cannot be used because delta is not fixed or constant. It is not like name='kivy'.