0

I wanted to write a function that compresses a bunch of files in my firebase realtime database to one file. Therefore, I wrote a request to get the oldest x files from my database and iterate through them, to sum each parameter up (and later divide by there count to get the average). Along to this man page, I should be able to iterate through my created snapshot. But if I try to implement it myself, I get warnings that I can't iterate through an object type (although my debugger says its an OrderedDictionary).

Thats my function:

def freeSpace(size):
    file_count = math.ceil(size/1024)
    snapshot = db.reference('/Arduinos/data/').order_by_key().limit_to_first(file_count + 1).get()
    json_list = [json.loads(json.dumps(item[1])) for item in snapshot]

    sum_json = {}
    time = snapshot[0]

    for json_str in json_list:                  # iterate over jsons in list
        json_dict = json.loads(json_str)        # convert each json Python-dictionary

        for key, value in json_dict.items():    # iterate over each pair of key and value
            if key in sum_json:                 # if key (parameter) already in sum
                sum_json[key] += value          # add to current value of sum
            else:                               # if key not already in sum
                sum_json[key] = value           # add new key to sum

    print(sum_json)
    
    # part where I write the new json into the db is future work

    return True

And here is my structure of the database.

My snapshot looks like this:

OrderedDict([
  ('2023-06-09 12:37:10', {
    'arduino_id': 1,
    'data': {
      'light': {
        'ir': 20,
        'uv': 0.01,
        'visible': 10
      },
      'ph': 7.5,
      'tds': 300,
      'timestamp': 122257
    },
    'status': {
      'light': True,
      'ph': True,
      'pump': True,
      'tds': True
    }
  }),
  ('2023-06-09 12:37:15', {
    'arduino_id': 1,
    'data': {
      'light': {
        'ir': 20,
        'uv': 0.01,
        'visible': 10
      },
      'ph': 7.5,
      'tds': 300,
      'timestamp': 122257
    },
    'status': {
      'light': True,
      'ph': True,
      'pump': True,
      'tds': True
    }
  })
])

I guess my nested structure could be a reason for that. So my question is, can I still somehow iterate through this snapshot? (and if so, how?) If not, do I need to flatten the files to a "classic" json format without any sub parameters? I would like to keep this structure since I work in a bigger project and changes like that would affect a bunch of other peoples work.

I already tried casting the snapshot manually to a dictionary and calling .values() or .items() after doing some research here. But none of that would satisfy my IDE and led to old errors while running. I also tried to let ChatGPT light me up for fun, which ended in a circle of just switching from .values() to .items() and back. Also not very helpful.

*Ignore mistakes in the further part of my function. If I get the iterating done I will be able to debug those myself (hope so).

Baltimur
  • 1
  • 2
  • https://stackoverflow.com/questions/46147443/how-to-loop-through-each-firebase-database-child-with-python/46156152#46156152? – Frank van Puffelen Jun 09 '23 at 13:24
  • If you pointed out the solution with .each() - the warning in my case stays the same. Since python thinks the returned snapshot is an object, I cant use any of those functions. – Baltimur Jun 09 '23 at 16:12
  • The `each` there loops over the child nodes in a Firebase data snapshot, so you call it **before** extracting the value from the snapshot. – Frank van Puffelen Jun 09 '23 at 18:06
  • I see what you mean, but my problem is, that I cannot iterate through my snapshot. So I already get a problem when calling .each() on my reference, like the solution you shared. To be clear, my problem occurs in the 4th line when creating json_list. So maybe I'm dumb, but I dont get how this solution would fix that. – Baltimur Jun 09 '23 at 22:29
  • You're not using `each()` on that line. From the link I provided, it seems you need to iterate over `snapshot.each()`. – Frank van Puffelen Jun 09 '23 at 23:14
  • And I did try that after reading your link. But the result was the same as with .items() and .values() which I tried before. So I guess I have to cast my snapshot to some Iterable, but I currently dont know how. To be exact, I get an "Unresolved attribute reference ... for class object" warning. – Baltimur Jun 10 '23 at 10:32

0 Answers0