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))
评论列表
文章目录