RBGKew / pykew

Python library for accessing Kew's data services
30 stars 4 forks source link

Is there a Json Structure data? #9

Open iBuitron opened 10 months ago

iBuitron commented 10 months ago

Hello!

I'm trying to automate the verification of species names for my app I'm developing.

Using the search options and with different species I get different results in the structure of the json depending on whether it is "accepted" or not "accepted" and whether it has "synonymOf" or not, and it is something that confuses me since I would like to cover all the cases according to my requirements (if elf cases).

Is there a case-by-case data structure?

I'm using this:

Use cases:

# sp_to_find = "Aira pumila"
# sp_to_find = "Solanum lycopersicum"
sp_to_find = "polylepis besseri"
import pykew.powo as powo

response = powo.search(query=sp_to_find)
result: list[dict] = [specie for specie in response if specie["name"] == sp_to_find]

There is and example, and i want to cover all case scenarios

new_result = []

for specie in result:
    if specie.get("accepted"):
        new_result.append(
            {
                "name": specie.get("name"),
                "url": specie.get("url"),
                "fqId": specie.get("fqId"),
            }
        )

    elif specie.get("synonymOf"):
        new_result.append(
            {
                "name": specie["synonymOf"].get("name"),
                "url": specie["synonymOf"].get("url"),
                "fqId": specie["synonymOf"].get("fqId"),
            }
        )

print(new_result)
iBuitron commented 10 months ago

For not making a new topic:

There is another question:

I'm looking in your code:

    def _run_query(self):
        params = self._build_params()
        response = self._api.get('search', params)
        # wait a proportion of server response time of previous call
        # before making subsequent calls
        self._wait_time = response.elapsed.total_seconds() / 2.0
        self._response = response.json()
        if 'results' in self._response:
            self._results = iter(self._response['results'])
        if 'cursor' in self._response:
            self._cursor = self._response['cursor']

It doesn't allow async request? (like using httpx?)