adafruit / adafruit/Adafruit_CircuitPython_JSON_Stream

Odd (literally) issue with nested dictionaries from weather.gov API

オープン
#5 コメント 4 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
Python
スター
2
フォーク
7
PR マージ指標
30日以内にマージされた PR はありません

説明

I'm trying to fetch and parse https://api.weather.gov/gridpoints/BOX/71,90/forecast/hourly with a MatrixPortal, which does not have a lot of spare RAM. Things are basically working, but when I tried to add probability of precipitation to the data I'm fetching, I got a surprise — it's skipping every other list item.

The json in question is a dictionary with the data I want under the key `properties`. That key's value is another dictionary, which contains the key `periods`, which is a _list_ of more dictionaries. Parsing all this works fine as long as I'm simply reading key/value pairs in the right order:

```python
hourly_file = io.open("hourly.json",'rb')
json_data = adafruit_json_stream.load(hourly_file)
periods = json_data['properties']['periods']

for period in periods:
print(f"Number: {period['number']:03}")
print(f"Start: {period['startTime']}")
print(f"End: {period['endTime']}")
print(f"Temp: {period['temperature']} {period['temperatureUnit']}")
print(f"Forecast: {period['shortForecast']}")
```

For testing, I'm using the system python on Fedora Linux, with [hourly.json](https://github.com/adafruit/Adafruit_CircuitPython_JSON_Stream/files/15503026/hourly.json) pre-downloaded. But this is exactly the same problem I'm seeing on the MatrixPortal with CircuitPython 9.1. What problem? Well, each period looks something like this:

```json
{
"number": 1,
"name": "",
"startTime": "2024-05-30T12:00:00-04:00",
"endTime": "2024-05-30T13:00:00-04:00",
"isDaytime": true,
"temperature": 57,
"temperatureUnit": "F",
"temperatureTrend": null,
"probabilityOfPrecipitation": {
"unitCode": "wmoUnit:percent",
"value": 80
},
"dewpoint": {
"unitCode": "wmoUnit:degC",
"value": 10
},
"relativeHumidity": {
"unitCode": "wmoUnit:percent",
"value": 77
},
"windSpeed": "9 mph",
"windDirection": "N",
"icon": "https://api.weather.gov/icons/land/day/rain_showers,80?size=small",
"shortForecast": "Rain Showers",
"detailedForecast": ""
}
```
and if try to get at one of the further-nested values, that's when stuff gets weird. For example:

```python
for period in periods:
print(f"Number: {period['number']:03}")
print(f"Start: {period['startTime']}")
print(f"End: {period['endTime']}")
print(f"Temp: {period['temperature']} {period['temperatureUnit']}")
print(f"Rain%: {period['probabilityOfPrecipitation']['value']}")
print(f"Forecast: {period['shortForecast']}")
```

... _skips every other period_, printing (in this example) just the odd-numbered ones.

Or, if I remove any lookups for keys _after_ the nested item, like:

```python
for period in periods:
print(f"Number: {period['number']:03}")
print(f"Start: {period['startTime']}")
print(f"End: {period['endTime']}")
print(f"Temp: {period['temperature']} {period['temperatureUnit']}")
print(f"Rain%: {period['probabilityOfPrecipitation']['value']}")
#print(f"Forecast: {period['shortForecast']}")
```

... it stops after the _first_ period.

Is there a better way to do this?

Is there a way to turn the `Transient` "period" into a real dictionary on each step of the loop? That'll use more memory, but only temporarily. Or, for that matter, just `period['probabilityOfPrecipitation']`?

Or should I be doing something else altogether?

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

まず、リンクされた hourly.json ファイル、システム Python、adafruit_json_stream.load を使ってネストされたアクセスの動作を再現し、その後、期間とネストされた辞書がどのように走査されるかを調べます。probabilityOfPrecipitation.value にアクセスしても期間がスキップされたり停止したりせず、低メモリのストリーミング動作が維持されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
embedded-iot
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。