Package: pirateweatherlib
pirateweatherlibThis library for thePirate Weather
APIprovides access to
detailed weather information from around the globe.It has been adapted fromdarkskylib.Quick startBefore you start using this library, you need to get your API keyhere.API CallsFunctionforecasthandles all request parameters and returns aForecastobject.>>>frompirateweatherimportforecast>>>boston=forecast(key,42.3601,-71.0589)>>>The first 3 positional arguments are identical to the 3 required
parameters for API call. The optional query parameters need to be
provided as keyword arguments.Usingtimeargument will get you atime machine call. Usingtimeoutargument will set defaultrequest
timeout.>>>BOSTON=key,42.3601,-71.0589>>>fromdatetimeimportdatetimeasdt>>>t=dt(2013,5,6,12).isoformat()>>>boston=forecast(*BOSTON,time=t)>>>boston.time1367866800Data Points and Data BlocksThe values as well asDataPointandDataBlockobjects are accessed
using instance attributes or dictionary keys. You can access current
values directly, without going throughcurrentlydata point.>>>boston['currently']['temperature']60.72>>>boston.temperature60.72Data blocksare indexable and iterable by theirdatavalues.>>>len(boston.hourly)24>>>>>>boston.hourly[1].temperature59.49>>>>>># list temperatures for next 10 hours...[hour.temperatureforhourinboston.hourly[:10]][60.83,59.49,58.93,57.95,56.01,53.95,51.21,49.21,47.95,46.31]Nonexistent attributes will raiseAttributeErrorand dictionary keysKeyErrorthe way you'd expect.Raw dataTo get the raw data dictionary, you can either access it through
instance attributes or navigate to it through dictionary keys, the same
way you would navigate the actual dictionary.>>>boston.hourly[2]{'ozone':290.06,'temperature':58.93,'pressure':1017.8,'windBearing':274,'dewPoint':52.58,'cloudCover':0.29,'apparentTemperature':58.93,'windSpeed':7.96,'summary':'Partly Cloudy','icon':'partly-cloudy-night','humidity':0.79,'precipProbability':0,'precipIntensity':0,'visibility':8.67,'time':1476410400}>>>>>>boston['hourly']['data'][2]{'ozone':290.06,'temperature':58.93,'pressure':1017.8,'windBearing':274,'dewPoint':52.58,'cloudCover':0.29,'apparentTemperature':58.93,'windSpeed':7.96,'summary':'Partly Cloudy','icon':'partly-cloudy-night','humidity':0.79,'precipProbability':0,'precipIntensity':0,'visibility':8.67,'time':1476410400}Flags and AlertsAll dashes-in attribute names ofFlagsobjects are replaced by
underscores_. This doesn't affect the dictionary keys.>>># instead of 'boston.flags.isd-stations'...boston.flags.isd_stations['383340-99999','383390-99999','383410-99999','384620-99999','384710-99999']>>>>>>boston.flags['isd-stations']['383340-99999','383390-99999','383410-99999','384620-99999','384710-99999']Even thoughAlertsare represented by a list, the data accessibility
through instance attributes is preserved for alerts in the list.>>>boston.alerts[0].title'Freeze Watch for Norfolk, MA'Updating dataUserefresh()method to update data of aForecastobject. Therefresh()method takes optional queries (includingtime, making it aTime machineobject) as keyword arguments. Callingrefresh()without any arguments will set all queries to default values. Usetimeoutargument to set the request timeout.>>>boston.refresh()>>>(boston.time,boston.temperature,len(boston.hourly))(1476403500,60.72,49)>>>>>>boston.refresh(units='si',extend='hourly')>>>(boston.time,boston.temperature,len(boston.hourly))(1476404205,15.81,169)>>>>>>boston.refresh(units='us')>>>(boston.time,boston.temperature,len(boston.hourly))(1476404489,60.57,49)For DevelopersResponse headers are stored in a dictionary underresponse_headersattribute.>>>boston.response_headers['X-response-Time']'146.035ms'Example scriptfrompirateweatherimportforecastfromdatetimeimportdate,timedeltaBOSTON=42.3601,71.0589weekday=date.today()withforecast('API_KEY',*BOSTON)asboston:print(boston.daily.summary,end='\n---\n')fordayinboston.daily:day=dict(day=date.strftime(weekday,'%a'),sum=day.summary,tempMin=day.temperatureMin,tempMax=day.temperatureMax)print('{day}:{sum}Temp range:{tempMin}-{tempMax}'.format(**day))weekday+=timedelta(days=1)Output:Light rain on Friday and Saturday, with temperatures bottoming out at 48°F on Tuesday.
---
Sun: Partly cloudy in the morning. Temp range: 44.86 - 57.26°F
Mon: Mostly cloudy in the morning. Temp range: 44.26 - 55.28°F
Tue: Clear throughout the day. Temp range: 36.85 - 47.9°F
Wed: Partly cloudy starting in the afternoon, continuing until evening. Temp range: 33.23 - 47.93°F
Thu: Light rain overnight. Temp range: 35.75 - 49.71°F
Fri: Light rain in the morning and afternoon. Temp range: 45.47 - 57.11°F
Sat: Drizzle in the morning. Temp range: 43.3 - 62.08°F
Sun: Clear throughout the day. Temp range: 39.81 - 60.84°FLicenseThe code is available under terms of [MIT License]
(https://raw.githubusercontent.com/noblecloud/pirateweatherlib/master/LICENSE)