API

时间:2016-01-07 17:45:10

标签: python json api mongoengine

我正在开发一个小的Python脚本,以便从forecast.io获取天气数据。一旦我获得了JSON文档,我就调用一个Class来创建一个新的记录保存在数据库中。问题是,某些字段(也是我的类中的属性)并不总是在API中通知,所以我必须包含某种防御性代码,否则当找不到字段时脚本会中断。

我发现@Alex Martelli的答案非常好:Reading from Python dict if key might not be present

  

如果你想做一些与使用默认值不同的事情(比方说,   当钥匙不存在时完全跳过打印),那么你需要一个   更多结构,即:

for r in results:
    if 'key_name' in r:
        print r['key_name'] 
     

for r in results:
    try: print r['key_name']
    except KeyError: pass

但我想知道是否必须包含" if"或者"尝试"在我想保存的每个领域还是有更好的方法来做到这一点? (我想要保存27个字段和27"如果"看起来很难看)

这是我到目前为止的代码:

from datetime import datetime

import tornado.web
import tornado.httpclient
from tornado import gen

from src.db.city import list_cities
from src.db.weather import Weather

from motorengine import *


@gen.coroutine
def forecastio_api():
    http_client = tornado.httpclient.AsyncHTTPClient()
    base_url = "https://api.forecast.io/forecast/APIKEY"
    city yield list_cities()
    for city in city:
        url = base_url + "/%s,%s" %(str(city.loc[0]), str(city.loc[1]))
        response = yield http_client.fetch(url)
        json = tornado.escape.json_decode(response.body)
        for day in json['daily']['data']:
            weather = Weather(city=city,
                              time = datetime.fromtimestamp(day['time']),
                              summary = day.get('summary'),
                              icon = day.get('icon'),
                              sunrise_time = datetime.fromtimestamp(day.get('sunriseTime')),
                              sunset_time = datetime.fromtimestamp(day.get('sunsetTime')),
                              moon_phase = day.get('moonPhase'),
                              precip_intensity = day.get('precipIntensity'),
                              precip_intensity_max = day.get('precipIntensityMax'),
                              precip_intensity_max_time = datetime.fromtimestamp(day.get('precipIntensityMaxTime')),
                              precip_probability = day.get('precipProbability'),
                              precip_type = day.get('precipType'),
                              temperature_min = day.get('temperatureMin'),
                              temperature_min_time = datetime.fromtimestamp(day.get('temperatureMinTime')),
                              temperature_max = day.get('temperatureMax'),
                              temperature_max_time = datetime.fromtimestamp(day.get('temperatureMaxTime')),
                              apparent_temperature_min = day.get('apparentTemperatureMin'),
                              apparent_temperature_min_time = datetime.fromtimestamp(day.get('apparentTemperatureMinTime')),
                              apparent_temperature_max = day.get('apparentTemperatureMax'),
                              apparent_temperature_max_time = datetime.fromtimestamp(day.get('apparentTemperatureMaxTime')),
                              dew_point = day.get('dewPoint'),
                              humidity = day.get('humidity'),
                              wind_speed = day.get('windSpeed'),
                              wind_bearing = day.get('windBearing'),
                              visibility = day.get('visibility'),
                              cloud_cover = day.get('cloudCover'),
                              pressure = day.get('pressure'),
                              ozone = day.get('ozone')
            )
            weather.create()


if __name__ == '__main__':
    io_loop = tornado.ioloop.IOLoop.instance()
    connect("DATABASE", host="localhost", port=27017, io_loop=io_loop)
    forecastio_api()
    io_loop.start()

这是使用Motornegine的天气等级:

from tornado import gen
from motorengine import Document
from motorengine.fields import DateTimeField, DecimalField, ReferenceField, StringField

from src.db.city import City


class Weather(Document):
    __collection__ = 'weather'
    __lazy__ = False
    city = ReferenceField(reference_document_type=City)
    time = DateTimeField(required=True)
    summary = StringField()
    icon = StringField()
    sunrise_time = DateTimeField()
    sunset_time = DateTimeField()
    moon_phase = DecimalField(precision=2)
    precip_intensity = DecimalField(precision=4)
    precip_intensity_max = DecimalField(precision=4)
    precip_intensity_max_time = DateTimeField()
    precip_probability = DecimalField(precision=2)
    precip_type = StringField()
    temperature_min = DecimalField(precision=2)
    temperature_min_time = DateTimeField()
    temperature_max = DecimalField(precision=2)
    temperature_max_time = DateTimeField()
    apparent_temperature_min = DecimalField(precision=2)
    apparent_temperature_min_time = DateTimeField()
    apparent_temperature_max = DecimalField(precision=2)
    apparent_temperature_max_time = DateTimeField()
    dew_point = DecimalField(precision=2)
    humidity = DecimalField(precision=2)
    wind_speed = DecimalField(precision=2)
    wind_bearing = DecimalField(precision=2)
    visibility = DecimalField(precision=2)
    cloud_cover = DecimalField(precision=2)
    pressure = DecimalField(precision=2)
    ozone = DecimalField(precision=2)
    create_time = DateTimeField(auto_now_on_insert=True)

    @gen.coroutine
    def create(self):
        yield self.save()

1 个答案:

答案 0 :(得分:1)

您可以查看Schematics。这个库可以帮助您定义可以从dicts轻松填充的对象(您可以轻松地将json转换为python dict)。它允许您为每个属性定义验证规则。当某些属性丢失或格式错误时,该对象将抛出ModelValidationError错误。 Schematics允许您在定义模型时添加默认值和更多好东西。