Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy id and api key #170

Merged
merged 7 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
+ Remove deprecated `new_old` and `future_old` functions.
+ Add `__str__` functions to `Timestep`, `Element`, `Day`, `Site`
+ Add element to `Forecast` to track if forecast is daily or 3 hourly
+ Change `id` variable in `Forecast`, `Observation`, `Site` to `location_id`.
+ Change `id` variable in `Element` to `field_code`.

## [0.9.8] - 2020-07-03

+ Remove f-string in test

## [0.9.7] - 2020-07-03

+ Bugfix for `get_observation_sites`
Expand Down Expand Up @@ -141,4 +143,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [0.1] - 2014-07-16

+ Initial commit and license.
+ Initial commit and license.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ conn = datapoint.connection(api_key="aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee")
site = conn.get_nearest_forecast_site(51.500728, -0.124626)

# Get a forecast for my nearest site with 3 hourly timesteps
forecast = conn.get_forecast_for_site(site.id, "3hourly")
forecast = conn.get_forecast_for_site(site.location_id, "3hourly")

# Get the current timestep from the forecast
current_timestep = forecast.now()
Expand Down
4 changes: 1 addition & 3 deletions datapoint/Day.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class Day():
def __init__(self, api_key=""):
self.api_key = api_key

def __init__(self):
self.date = None
self.timesteps = []

Expand Down
4 changes: 2 additions & 2 deletions datapoint/Element.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Element():
def __init__(self, id=None, value=None, units=None):
def __init__(self, field_code=None, value=None, units=None):

self.id = id
self.field_code = field_code
self.value = value
self.units = units

Expand Down
6 changes: 2 additions & 4 deletions datapoint/Forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@


class Forecast():
def __init__(self, api_key="", frequency=""):
self.api_key = api_key

def __init__(self, frequency=""):
self.frequency = frequency
self.data_date = None
self.continent = None
self.country = None
self.name = None
self.longitude = None
self.latitude = None
self.id = None
self.location_id = None
self.elevation = None
self.days = []

Expand Down
8 changes: 4 additions & 4 deletions datapoint/Manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def get_forecast_sites(self):
for jsoned in data['Locations']['Location']:
site = Site()
site.name = jsoned['name']
site.id = jsoned['id']
site.location_id = jsoned['id']
site.latitude = jsoned['latitude']
site.longitude = jsoned['longitude']

Expand Down Expand Up @@ -359,7 +359,7 @@ def get_forecast_for_site(self, site_id, frequency="daily"):
forecast.latitude = data['SiteRep']['DV']['Location']['lat']

if 'i' in data['SiteRep']['DV']['Location']:
forecast.id = data['SiteRep']['DV']['Location']['i']
forecast.location_id = data['SiteRep']['DV']['Location']['i']

if 'elevation' in data['SiteRep']['DV']['Location']:
forecast.elevation = data['SiteRep']['DV']['Location']['elevation']
Expand Down Expand Up @@ -467,7 +467,7 @@ def get_observation_sites(self):
for jsoned in data['Locations']['Location']:
site = Site()
site.name = jsoned['name']
site.id = jsoned['id']
site.location_id = jsoned['id']
site.latitude = jsoned['latitude']
site.longitude = jsoned['longitude']

Expand Down Expand Up @@ -553,7 +553,7 @@ def get_observations_for_site(self, site_id, frequency='hourly'):
observation.latitude = data['SiteRep']['DV']['Location']['lat']

if 'i' in data['SiteRep']['DV']['Location']:
observation.id = data['SiteRep']['DV']['Location']['i']
observation.location_id = data['SiteRep']['DV']['Location']['i']

if 'elevation' in data['SiteRep']['DV']['Location']:
observation.elevation = data['SiteRep']['DV']['Location']['elevation']
Expand Down
11 changes: 4 additions & 7 deletions datapoint/Observation.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import datetime

class Observation():
def __init__(self, api_key=""):
self.api_key = api_key

def __init__(self):
self.data_date = None
self.continent = None
self.country = None
self.name = None
self.longitude = None
self.latitude = None
self.id = None
self.location_id = None
self.elevation = None
# Stores a list of observations in days
self.days = []

def now(self):
"""
Return the final timestep available. This is the most recent observation.
Return the final timestep available. This is the most recent
observation.
"""

return self.days[-1].timesteps[-1]
6 changes: 2 additions & 4 deletions datapoint/Site.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class Site():
def __init__(self, api_key=""):
self.api_key = api_key

def __init__(self):
self.name = None
self.id = None
self.location_id = None
self.elevation = None
self.latitude = None
self.longitude = None
Expand Down
4 changes: 1 addition & 3 deletions datapoint/Timestep.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from .Element import Element

class Timestep():
def __init__(self, api_key=""):
self.api_key = api_key

def __init__(self):
self.name = None
self.date = None
self.weather = None
Expand Down
2 changes: 1 addition & 1 deletion datapoint/regions/RegionManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def get_all_regions(self):
regions = []
for location in response['Locations']['Location']:
region = Site()
region.id = location['@id']
region.location_id = location['@id']
region.region = location['@name']
region.name = REGION_NAMES[location['@name']]
regions.append(region)
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Let’s call another of the Manager Object’s methods to give us a

::

forecast = conn.get_forecast_for_site(site.id, "3hourly")
forecast = conn.get_forecast_for_site(site.location_id, "3hourly")

We’ve given this method two parameters, the site ID for the forecast we want and
also a forecast type of “3hourly”. We’ll discuss the forecast types later on.
Expand Down
2 changes: 1 addition & 1 deletion examples/current_weather/current_weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
print(site.name)

# Get a forecast for the nearest site
forecast = conn.get_forecast_for_site(site.id, "3hourly")
forecast = conn.get_forecast_for_site(site.location_id, "3hourly")

# Get the current timestep using now() and print out some info
now = forecast.now()
Expand Down
2 changes: 1 addition & 1 deletion examples/postcodes_to_lat_lng/postcodes_to_lat_lng.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
print(site.name)

# Get a forecast for the nearest site
forecast = conn.get_forecast_for_site(site.id, "3hourly")
forecast = conn.get_forecast_for_site(site.location_id, "3hourly")

# Get the current timestep using now() and print out some info
now = forecast.now()
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_forecast/simple_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
print(site.name)

# Get a forecast for the nearest site
forecast = conn.get_forecast_for_site(site.id, "3hourly")
forecast = conn.get_forecast_for_site(site.location_id, "3hourly")

# Loop through days and print date
for day in forecast.days:
Expand Down
4 changes: 2 additions & 2 deletions examples/text_forecast/text_forecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
# Get all regions and print out their details
regions = conn.regions.get_all_regions()
for region in regions:
print((region.name, region.id, region.region))
print((region.name, region.location_id, region.region))

# Get all forecasts for a specific region
my_region = regions[0]
forecast = conn.regions.get_raw_forecast(my_region.id)['RegionalFcst']
forecast = conn.regions.get_raw_forecast(my_region.location_id)['RegionalFcst']

# Print the forecast details
print('Forecast for {} (issued at {}):'.format(my_region.name, forecast['issuedAt']))
Expand Down
4 changes: 2 additions & 2 deletions examples/tube_bike/tube_bike.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
work = conn.get_nearest_forecast_site(51.5031650, -0.1123050)

# Get a forecast for my house and work
my_house_forecast = conn.get_forecast_for_site(my_house.id, "3hourly")
work_forecast = conn.get_forecast_for_site(work.id, "3hourly")
my_house_forecast = conn.get_forecast_for_site(my_house.location_id, "3hourly")
work_forecast = conn.get_forecast_for_site(work.location_id, "3hourly")

# Get the current timestep for both locations
my_house_now = my_house_forecast.now()
Expand Down
2 changes: 1 addition & 1 deletion examples/umbrella/umbrella.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
print(site.name)

# Get a forecast for the nearest site
forecast = conn.get_forecast_for_site(site.id, "3hourly")
forecast = conn.get_forecast_for_site(site.location_id, "3hourly")

# Loop through all the timesteps in day 0 (today)
for timestep in forecast.days[0].timesteps:
Expand Down
2 changes: 1 addition & 1 deletion examples/washing/washing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


# Get a forecast for the nearest site
forecast = conn.get_forecast_for_site(site.id, "daily")
forecast = conn.get_forecast_for_site(site.location_id, "daily")

# Loop through days
for day in forecast.days:
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/test_datapoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def test_wavertree_hourly(self, mock_request):
MockDateTime.now = classmethod(lambda cls, **kwargs: datetime(2020, 4, 25, 12, tzinfo=timezone.utc))
mock_request.get('/public/data/val/wxfcs/all/json/354107?res=3hourly', text=self.wavertree_hourly)

forecast = self.conn.get_forecast_for_site(self.wavertree.id, "3hourly")
forecast = self.conn.get_forecast_for_site(self.wavertree.location_id, "3hourly")
now = forecast.now()

self.assertEqual(self.wavertree.id, '354107')
self.assertEqual(self.wavertree.location_id, '354107')
self.assertEqual(self.wavertree.name, 'Wavertree')

self.assertEqual(now.date.strftime(DATETIME_FORMAT), '2020-04-25 12:00:00+0000')
Expand Down Expand Up @@ -84,10 +84,10 @@ def test_wavertree_daily(self, mock_request):
MockDateTime.now = classmethod(lambda cls, **kwargs: datetime(2020, 4, 25, 12, tzinfo=timezone.utc))
mock_request.get('/public/data/val/wxfcs/all/json/354107?res=daily', text=self.wavertree_daily)

forecast = self.conn.get_forecast_for_site(self.wavertree.id, "daily")
forecast = self.conn.get_forecast_for_site(self.wavertree.location_id, "daily")
now = forecast.now()

self.assertEqual(self.wavertree.id, '354107')
self.assertEqual(self.wavertree.location_id, '354107')
self.assertEqual(self.wavertree.name, 'Wavertree')

self.assertEqual(now.date.strftime(DATETIME_FORMAT), '2020-04-25 12:00:00+0000')
Expand Down Expand Up @@ -125,10 +125,10 @@ def test_kingslynn_hourly(self, mock_request):
MockDateTime.now = classmethod(lambda cls, **kwargs: datetime(2020, 4, 25, 12, tzinfo=timezone.utc))
mock_request.get('/public/data/val/wxfcs/all/json/322380?res=3hourly', text=self.kingslynn_hourly)

forecast = self.conn.get_forecast_for_site(self.kingslynn.id, "3hourly")
forecast = self.conn.get_forecast_for_site(self.kingslynn.location_id, "3hourly")
now = forecast.now()

self.assertEqual(self.kingslynn.id, '322380')
self.assertEqual(self.kingslynn.location_id, '322380')
self.assertEqual(self.kingslynn.name, "King's Lynn")

self.assertEqual(now.date.strftime(DATETIME_FORMAT), '2020-04-25 12:00:00+0000')
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_get_forecast_sites(self):

def test_get_daily_forecast(self):
site = self.manager.get_nearest_forecast_site(latitude=51.500728, longitude=-0.124626)
forecast = self.manager.get_forecast_for_site(site.id, 'daily')
forecast = self.manager.get_forecast_for_site(site.location_id, 'daily')
self.assertIsInstance(forecast, datapoint.Forecast.Forecast)
self.assertEqual(forecast.continent.upper(), 'EUROPE')
self.assertEqual(forecast.country.upper(), 'ENGLAND')
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_get_daily_forecast(self):

def test_get_3hour_forecast(self):
site = self.manager.get_nearest_forecast_site(latitude=51.500728, longitude=-0.124626)
forecast = self.manager.get_forecast_for_site(site.id, '3hourly')
forecast = self.manager.get_forecast_for_site(site.location_id, '3hourly')
self.assertIsInstance(forecast, datapoint.Forecast.Forecast)
self.assertEqual(forecast.continent.upper(), 'EUROPE')
self.assertEqual(forecast.country.upper(), 'ENGLAND')
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def test_get_all_regions(self):
all_regions = self.regions.get_all_regions()
sample_region = next(
region for region in all_regions
if region.id == '515')
if region.location_id == '515')
self.assertEqual(sample_region.name, 'UK')
self.assertEqual(sample_region.region, 'uk')

def test_get_raw_forecast(self):
sample_region = self.regions.get_all_regions()[0]
response = self.regions.get_raw_forecast(
sample_region.id)['RegionalFcst']
sample_region.location_id)['RegionalFcst']
self.assertEqual(response['regionId'], sample_region.region)

# Based on what Datapoint serves at time of writing...
Expand Down