quality.py 文件源码

python
阅读 26 收藏 0 点赞 0 评论 0

项目:electricitymap 作者: tmrowco 项目源码 文件源码
def validate_production(obj, country_code):
    if not 'datetime' in obj:
        raise Exception('datetime was not returned for %s' % country_code)
    if not 'countryCode' in obj:
        raise Exception('countryCode was not returned for %s' % country_code)
    if not type(obj['datetime']) == datetime.datetime:
        raise Exception('datetime %s is not valid for %s' % (obj['datetime'], country_code))
    if obj.get('countryCode', None) != country_code:
        raise Exception("Country codes %s and %s don't match" % (obj.get('countryCode', None), country_code))
    if arrow.get(obj['datetime']) > arrow.now():
        raise Exception("Data from %s can't be in the future" % country_code)
    if obj.get('production', {}).get('unknown', None) is None and \
        obj.get('production', {}).get('coal', None) is None and \
        obj.get('production', {}).get('oil', None) is None and \
        country_code not in ['CH', 'NO', 'AUS-TAS']:
        raise Exception("Coal or oil or unknown production value is required for %s" % (country_code))
    for k, v in obj['production'].iteritems():
        if v is None: continue
        if v < 0: raise ValueError('%s: key %s has negative value %s' % (country_code, k, v))
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号