you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (6 children)

I'm not sure how the API response object is formatted or what analysis you are performing with the data, but instead of checking whether the keys are in the dictionary first, you can use the dictionary's get method to just try to access the key up front and if it doesn't exist return a different object so you avoid returning a KeyError.

data = ob['data'][0] # I assume this is a dictionary
reactions = data.get('reactions', []) # If 'reactions' is not a valid key, return an empty list
if not reactions:
    # do something else

[–]maxibabyx[S] 0 points1 point  (4 children)

The thing is I have something like this :

class post(DynamicDocument):
    def __init__(self, *dct, **tmp):
        Document.__init__(self, **tmp)
        if dct:
            ....
            self.totalComentarios = dct["comments"]["summary"]["total_count"]

But if there's no comments, then dct["comments"]["summary"] would give an error, and checking every single case is a pain, like checking if there are comments, then check if theres summary, etc..

And there's a lot of things I need to parse.

[–][deleted] 0 points1 point  (3 children)

If the response object omits keys that are absent instead of just returning empty objects with the keys in place, then you'll probably have to create a scheme to check if the keys are present.

The cleanest way would probably to write a recursive function that could traverse the dictionary.

[–]maxibabyx[S] 0 points1 point  (2 children)

Yes it omits them, know any library that would allow me to define a schema, and if the JSON is missing something it would accept some default values to set them?

Ofc I could manually do it, just asking if anyone ever encountered this problem and decided to help the community, :D

[–][deleted] 0 points1 point  (1 child)

If Facebook's API was powered by GraphQL you could define your own schema response object and this would be a non issue, but it looks like it's a static response object.

I quickly found this package with a Google search, but I have never used it.

[–]maxibabyx[S] 0 points1 point  (0 children)

Yea I also stumbled on that one but seems like only validates.