def __init__(self, location_name=None, location_xyz=None, forecast_link='forecast', language_name='en'): self.language_name = language_name self.forecast_link = forecast_link self.language = Language(self.language_name) if location_name: self.location_name = location_name self.location_xyz = None self.location = Location(self.location_name, self.forecast_link, self.language) elif location_xyz: self.location_name = None self.location_xyz = location_xyz self.location = LocationXYZ(location_xyz[0], location_xyz[1], location_xyz[2]) else: raise YrException( "location_name or location_xyz parameter must be set") self.connect = Connect(self.location) self.xml_source = self.connect.read() self.dictionary = self.xml2dict(self.xml_source) self.credit = { 'text': self.language.dictionary['credit'], 'url': 'http://www.yr.no/' }
def __init__( self, location_name=None, coordinates=None, location_xyz=None, forecast_link=default_forecast_link, language_name=default_language_name, ): self.forecast_link = forecast_link self.language_name = language_name self.language = Language(language_name=self.language_name) if location_xyz: if len(location_xyz) > 2: coordinates = (location_xyz[1], location_xyz[0], location_xyz[2]) else: coordinates = (location_xyz[1], location_xyz[0]) self.location_xyz = location_xyz if location_name: self.location_name = location_name self.coordinates = None self.location = Location( location_name=self.location_name, forecast_link=self.forecast_link, language=self.language, ) elif coordinates: self.location_name = None self.coordinates = coordinates if len(self.coordinates) > 2: self.location = API_Locationforecast( lat=self.coordinates[0], lon=self.coordinates[1], msl=self.coordinates[2], language=self.language, ) else: self.location = API_Locationforecast( lat=self.coordinates[0], lon=self.coordinates[1], language=self.language, ) else: raise YrException( 'location_name or location_xyz parameter must be set') self.connect = Connect(location=self.location) self.xml_source = self.connect.read() self.dictionary = self.xml2dict(self.xml_source) self.credit = self.language.dictionary['credit']
def wind_speed(self): ''' Get wind speed from yr. ''' self.location = Location(self.location_name, self.language) getlocation = Location(self.location_name, self.language) data = Connect(getlocation).read() data = et.fromstring(data) out = {'data': []} for parent in data.find('forecast').find('tabular').findall('time'): if parent.find('windSpeed') is not None: out['data'].append({'from': parent.attrib['from'], 'to': parent.attrib['to'], 'speed': float(parent.find('windSpeed').attrib['mps'])}) out['credit'] = self.yr_credit return out
class Yr: def py2json(self, python): return json.dumps(python, indent=4) def xml2dict(self, xml): return xmltodict.parse(xml) def dict2xml(self, dictionary): return xmltodict.unparse(dictionary, pretty=True) def py2result(self, python, as_json=False): # default is return result as dictionary ;) if as_json: return self.py2json(python) else: return python def forecast(self, as_json=False): if self.location_xyz: times = self.dictionary['weatherdata']['product']['time'] else: times = self.dictionary['weatherdata']['forecast']['tabular'][ 'time'] for time in times: yield self.py2result(time, as_json) def now(self, as_json=False): return next(self.forecast(as_json)) def __init__(self, location_name=None, location_xyz=None, forecast_link='forecast', language_name='en'): self.language_name = language_name self.forecast_link = forecast_link self.language = Language(self.language_name) if location_name: self.location_name = location_name self.location_xyz = None self.location = Location(self.location_name, self.forecast_link, self.language) elif location_xyz: self.location_name = None self.location_xyz = location_xyz self.location = LocationXYZ(location_xyz[0], location_xyz[1], location_xyz[2]) else: raise YrException( "location_name or location_xyz parameter must be set") self.connect = Connect(self.location) self.xml_source = self.connect.read() self.dictionary = self.xml2dict(self.xml_source) self.credit = { 'text': self.language.dictionary['credit'], 'url': 'http://www.yr.no/' }
def __init__(self, location_name=None, location_xyz=None, forecast_link='forecast', language_name='en'): self.language_name = language_name self.forecast_link = forecast_link self.language = Language(self.language_name) if location_name: self.location_name = location_name self.location_xyz = None self.location = Location(self.location_name, self.forecast_link, self.language) elif location_xyz: self.location_name = None self.location_xyz = location_xyz self.location = LocationXYZ(location_xyz[0], location_xyz[1], location_xyz[2]) else: raise YrException("location_name or location_xyz parameter must be set") self.connect = Connect(self.location) self.xml_source = self.connect.read() self.dictionary = self.xml2dict(self.xml_source) self.credit = { 'text': self.language.dictionary['credit'], 'url': 'http://www.yr.no/' }
def __init__( self, location_name=None, coordinates=None, location_xyz=None, forecast_link=default_forecast_link, language_name=default_language_name, ): self.forecast_link = forecast_link self.language_name = language_name self.language = Language(language_name=self.language_name) if location_xyz: coordinates = (location_xyz[1], location_xyz[0], location_xyz[2]) self.location_xyz = location_xyz if location_name: self.location_name = location_name self.coordinates = None self.location = Location( location_name=self.location_name, forecast_link=self.forecast_link, language=self.language, ) elif coordinates: self.location_name = None self.coordinates = coordinates self.location = API_Locationforecast( lat=self.coordinates[0], lon=self.coordinates[1], msl=self.coordinates[2], language=self.language, ) else: raise YrException('location_name or location_xyz parameter must be set') self.connect = Connect(location=self.location) self.xml_source = self.connect.read() self.dictionary = self.xml2dict(self.xml_source) self.credit = self.language.dictionary['credit']
def xmlsource(self): data = Connect(self.location).read() return data
class Yr: def py2json(self, python): return json.dumps(python, indent=4) def xml2dict(self, xml): return xmltodict.parse(xml) def dict2xml(self, dictionary): return xmltodict.unparse(dictionary, pretty=True) def py2list(self, daily=False, stats=['min', 'max', 'avg'], interval=[0, 6], parameters=['precipitation', 'windDirection', 'windSpeed', 'temperature', 'pressure']): """Return CSV file of forecast data :param str outputfile: the complete path and name to output csv file :param bool daily: by default (False) it write all forecast records, True to write one record for each day :param list stats: a list with statistical value to calculate, admited values are: 'min', 'max', 'avg'. Used only with daily=True :param list interval: list of forecast interval to use, 0 has be set everytime as first element, other options are: 3 for the three hours precipitation forecast, 6 for the six hours precipitation forecast :param list parameters: list of parameters to save in the CSV file..... """ data = self.forecast() yrcsv = YrCSV(data, parameters, stats, interval) if daily: return yrcsv.return_daily() else: return yrcsv.return_all() def py2csv(self, outputfile, daily=False, stats=['min', 'max', 'avg'], interval=[0, 6], parameters=['precipitation', 'windDirection', 'windSpeed', 'temperature', 'pressure']): """Return CSV file of forecast data :param str outputfile: the complete path and name to output csv file :param bool daily: by default (False) it write all forecast records, True to write one record for each day :param list stats: a list with statistical value to calculate, admited values are: 'min', 'max', 'avg'. Used only with daily=True :param list interval: list of forecast interval to use, 0 has be set everytime as first element, other options are: 3 for the three hours precipitation forecast, 6 for the six hours precipitation forecast :param list parameters: list of parameters to save in the CSV file..... """ data = self.forecast() yrcsv = YrCSV(data, parameters, stats, interval) yrcsv.write(outputfile, daily) def py2result(self, python, as_json=False): # default is return result as dictionary ;) if as_json: return self.py2json(python) else: return python def forecast(self, as_json=False): if self.location_xyz: times = self.dictionary['weatherdata']['product']['time'] else: times = self.dictionary['weatherdata']['forecast']['tabular']['time'] for time in times: yield self.py2result(time, as_json) def now(self, as_json=False): return next(self.forecast(as_json)) def __init__(self, location_name=None, location_xyz=None, forecast_link='forecast', language_name='en'): self.language_name = language_name self.forecast_link = forecast_link self.language = Language(self.language_name) if location_name: self.location_name = location_name self.location_xyz = None self.location = Location(self.location_name, self.forecast_link, self.language) elif location_xyz: self.location_name = None self.location_xyz = location_xyz self.location = LocationXYZ(location_xyz[0], location_xyz[1], location_xyz[2]) else: raise YrException("location_name or location_xyz parameter must be set") self.connect = Connect(self.location) self.xml_source = self.connect.read() self.dictionary = self.xml2dict(self.xml_source) self.credit = { 'text': self.language.dictionary['credit'], 'url': 'http://www.yr.no/' }
class Yr: default_forecast_link = 'forecast' default_language_name = 'en' def py2json(self, python): return json.dumps(python, indent=4) def xml2dict(self, xml): return xmltodict.parse(xml) def dict2xml(self, dictionary): return xmltodict.unparse(dictionary, pretty=True) def py2result(self, python, as_json=False): # default is return result as dictionary ;) if as_json: return self.py2json(python) else: return python def forecast(self, as_json=False): if self.coordinates: times = self.dictionary['weatherdata']['product']['time'] else: times = self.dictionary['weatherdata']['forecast']['tabular'][ 'time'] for time in times: yield self.py2result(time, as_json) def now(self, as_json=False): return next(self.forecast(as_json)) def __init__( self, location_name=None, coordinates=None, location_xyz=None, forecast_link=default_forecast_link, language_name=default_language_name, ): self.forecast_link = forecast_link self.language_name = language_name self.language = Language(language_name=self.language_name) if location_xyz: if len(location_xyz) > 2: coordinates = (location_xyz[1], location_xyz[0], location_xyz[2]) else: coordinates = (location_xyz[1], location_xyz[0]) self.location_xyz = location_xyz if location_name: self.location_name = location_name self.coordinates = None self.location = Location( location_name=self.location_name, forecast_link=self.forecast_link, language=self.language, ) elif coordinates: self.location_name = None self.coordinates = coordinates if len(self.coordinates) > 2: self.location = API_Locationforecast( lat=self.coordinates[0], lon=self.coordinates[1], msl=self.coordinates[2], language=self.language, ) else: self.location = API_Locationforecast( lat=self.coordinates[0], lon=self.coordinates[1], language=self.language, ) else: raise YrException( 'location_name or location_xyz parameter must be set') self.connect = Connect(location=self.location) self.xml_source = self.connect.read() self.dictionary = self.xml2dict(self.xml_source) self.credit = self.language.dictionary['credit']
class Yr: default_forecast_link = 'forecast' default_language_name = 'en' def py2json(self, python): return json.dumps(python, indent=4) def xml2dict(self, xml): return xmltodict.parse(xml) def dict2xml(self, dictionary): return xmltodict.unparse(dictionary, pretty=True) def py2result(self, python, as_json=False): # default is return result as dictionary ;) if as_json: return self.py2json(python) else: return python def forecast(self, as_json=False): if self.coordinates: times = self.dictionary['weatherdata']['product']['time'] else: times = self.dictionary['weatherdata']['forecast']['tabular']['time'] for time in times: yield self.py2result(time, as_json) def now(self, as_json=False): return next(self.forecast(as_json)) def __init__( self, location_name=None, coordinates=None, location_xyz=None, forecast_link=default_forecast_link, language_name=default_language_name, ): self.forecast_link = forecast_link self.language_name = language_name self.language = Language(language_name=self.language_name) if location_xyz: coordinates = (location_xyz[1], location_xyz[0], location_xyz[2]) self.location_xyz = location_xyz if location_name: self.location_name = location_name self.coordinates = None self.location = Location( location_name=self.location_name, forecast_link=self.forecast_link, language=self.language, ) elif coordinates: self.location_name = None self.coordinates = coordinates self.location = API_Locationforecast( lat=self.coordinates[0], lon=self.coordinates[1], msl=self.coordinates[2], language=self.language, ) else: raise YrException('location_name or location_xyz parameter must be set') self.connect = Connect(location=self.location) self.xml_source = self.connect.read() self.dictionary = self.xml2dict(self.xml_source) self.credit = self.language.dictionary['credit']