-1

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.

  • From the documentation, it looks like `self.store.find` expects one or more keyword arguments, probably something like `for value in self.store.find(delta='somevalue')`. This is also what the error means: the function didn't expect a positional argument at all. – inclement Apr 22 '19 at 16:44
  • Create a new question does not depend on the answers you have received, if it is the same question is a duplicate, and create new publications with the same question is considered noise here. – eyllanesc Apr 22 '19 at 17:39

1 Answers1

0

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'.

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Thank you! This along with the already answered question helped. Sorry for creating a duplicate question, I just didn't understand the generator code for a while. – EastCoastYam Apr 22 '19 at 18:03