Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #23 from ReagentX/develop
Browse files Browse the repository at this point in the history
Release/1.0.5
  • Loading branch information
ReagentX authored Sep 11, 2020
2 parents 2310178 + 7ef4e08 commit 073b149
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ A Python 3.x API Class to turn data from the PurpleAir/ThingSpeak API into a Pan
## Installation

* To use
* Create a virtual environment
* `python -m venv venv`
* Activate the virtual environment
* `source venv/bin/activate`
* Install this package
* `pip install purpleair`
* `pip install purpleair`
* It is a good practice to only install within a [virtual environment](https://docs.python.org/3/library/venv.html)
* To hack
* Clone this repo
* `cd` to the folder
Expand Down
5 changes: 3 additions & 2 deletions purpleair/api_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@

import requests_cache

# Setup cache for requests
# Set up cache for requests
requests_cache.install_cache(expire_after=timedelta(hours=1))
try:
requests_cache.core.remove_expired_responses()
except OperationalError:
print('Unable to purge cache database, cache may contain old data!!!')
requests_cache.core.uninstall_cache()
print('Unable to open cache or purge cache database, requests will not be cached!!!')


API_ROOT = 'https://www.purpleair.com/json'
10 changes: 9 additions & 1 deletion purpleair/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,20 @@ def get_all_data(self):
except JSONDecodeError as err:
raise ValueError(
'Invalid JSON data returned from network!') from err

# Handle rate limit or other error message
if 'results' not in data:
message = data.get('message')
error_message = message if message is not None else data
raise ValueError(
f'No sensor data returned from PurpleAIR: {error_message}')

print(f"Initialized {len(data['results']):,} sensors!")
self.data = data['results']

def to_dataframe(self, sensor_group: str) -> pd.DataFrame:
"""
Converts dictionary representation of a list of sensors to a Pandas Dataframe
Converts dictionary representation of a list of sensors to a Pandas DataFrame
where sensor_group determines which group of sensors are used
"""
if sensor_group not in {'useful', 'outside', 'all'}:
Expand Down
6 changes: 3 additions & 3 deletions purpleair/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_data(self) -> dict:

def setup(self) -> None:
"""
Initiailze metadata and real data for a sensor; for detailed info see docs
Initialize metadata and real data for a sensor; for detailed info see docs
"""
# Meta
self.lat = self.data.get('Lat', None)
Expand Down Expand Up @@ -204,7 +204,7 @@ def as_dict(self) -> dict:
'lat': self.lat,
'lon': self.lon,
'name': self.name,
'locaction_type': self.location_type
'location_type': self.location_type
},
'data': {
'pm_2.5': self.current_pm2_5,
Expand Down Expand Up @@ -247,7 +247,7 @@ def as_dict(self) -> dict:

def as_flat_dict(self) -> dict:
"""
Returns a flat dictionart representation of the Sensor data
Returns a flat dictionary representation of the Sensor data
"""
out_d = {}
src = self.as_dict()
Expand Down
5 changes: 5 additions & 0 deletions scripts/run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
Sample script to run quick tests
"""


from purpleair.network import SensorList
from purpleair.sensor import Sensor

Expand Down
10 changes: 8 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
"""
Install purpleair as a development copy by running this file
`python setup.py develop`
"""


from setuptools import setup, find_packages

with open("README.md", "r") as fh:
LONG_DESCRIPTION = fh.read()

setup(
name='purpleair',
version='1.0.4',
version='1.0.5',
description='Python API Client to get and transform PurpleAir data.',
long_description=LONG_DESCRIPTION,
long_description_content_type="text/markdown",
author='Christopher Sardegna',
author_email='github@reagentx.net',
author_email='purpleair@reagentx.net',
url='https://github.com/ReagentX/purple_air_api/',
packages=find_packages(),
install_requires=['requests', 'requests_cache', 'thingspeak', 'geopy', 'pandas'],
Expand Down
2 changes: 1 addition & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_as_dict(self):
'lat': 0,
'lon': 0,
'name': 0,
'locaction_type': 0
'location_type': 0
},
'data': {
'pm_2.5': 0,
Expand Down

0 comments on commit 073b149

Please sign in to comment.