Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 898100e

Browse files
committed
Test cleanup
1 parent b2b4cf9 commit 898100e

27 files changed

+1408
-1221
lines changed

.circleci/config.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
version: 1
2+
jobs:
3+
build:
4+
docker:
5+
- image: circleci/python:3.6
6+
7+
working_directory: ~/fsf-api-access-python
8+
9+
steps:
10+
- checkout
11+
12+
- restore_cache:
13+
keys:
14+
- v1-dependencies-{{ checksum "requirements.txt" }}
15+
16+
- run:
17+
name: install dependencies
18+
command: |
19+
python3 -m venv venv
20+
. venv/bin/activate
21+
pip install -e .[testing]
22+
23+
- save_cache:
24+
paths:
25+
- ./venv
26+
key: v1-dependencies-{{ checksum "requirements.txt" }}
27+
28+
- run:
29+
name: run tests
30+
command: |
31+
. venv/bin/activate
32+
pytest tests --basetemp=C:/Users/Lyetenth/Documents/FSF/flood_lab/tests/temp
33+
34+
- store_artifacts:
35+
path: test-reports
36+
destination: test-reports

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
# First Street Foundation API Access Documentation (Python)
2+
[![CircleCI](https://circleci.com/gh/FirstStreet/firststreet-python.svg?style=svg&circle-token=9f4c22fd34a87690970add09e6454f17764812b4)](https://circleci.com/gh/FirstStreet/firststreet-python)
3+
24
The First Street Foundation API Access (Python) is a wrapper used to bulk extract flood data from the First Street Foundation API
35

6+
7+
### Current Release:
8+
Current release: 0.1
9+
10+
**Notice:** This API wrapper is subject to change.
11+
412
# Table of contents
513
- **[Installation](#installation)**
614
* [Running the Project - Method 1: Through the Command Line](#method1)
@@ -20,6 +28,7 @@ The First Street Foundation API Access (Python) is a wrapper used to bulk extrac
2028
- [CSV File Name:](#csv-name)
2129
- [CSV File Content](#csv-content)
2230
- **[Updating the Project to the Newest Version:](#updating)**
31+
- **[License](#license)**
2332

2433
<a name="installation"></a>
2534
# Installation
@@ -434,6 +443,31 @@ If an update is made to this project, you will need to pull the changes from git
434443

435444
3. The project should now be updated to the newest version
436445

446+
<a name="license"></a>
447+
# License
448+
```
449+
MIT License
450+
451+
Copyright (c) 2020 First Street Foundation
452+
453+
Permission is hereby granted, free of charge, to any person obtaining a copy
454+
of this software and associated documentation files (the "Software"), to deal
455+
in the Software without restriction, including without limitation the rights
456+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
457+
copies of the Software, and to permit persons to whom the Software is
458+
furnished to do so, subject to the following conditions:
459+
460+
The above copyright notice and this permission notice shall be included in all
461+
copies or substantial portions of the Software.
462+
463+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
464+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
465+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
466+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
467+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
468+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
469+
SOFTWARE.
470+
```
437471

438472

439473
[git]: <https://git-scm.com/downloads>

conftest.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,24 @@
1+
# Author: Kelvin Lai <kelvin@firststreet.org>
2+
# Copyright: This module is owned by First Street Foundation
3+
4+
# External Imports
5+
import pytest
6+
17
pytest_plugins = 'aiohttp.pytest_plugin'
8+
9+
10+
def pytest_addoption(parser):
11+
"""Sets up command line arguments for pytest to skip the stress test
12+
"""
13+
parser.addoption("--runstress", action="store_true", default=False, help="runs the stress test")
14+
15+
16+
def pytest_collection_modifyitems(config, items):
17+
"""Skips the stress test if the argument is not set
18+
"""
19+
if config.getoption("--runstress"):
20+
return
21+
skip_stress = pytest.mark.skip(reason="need --runstress option to run")
22+
for item in items:
23+
if "stress" in item.keywords:
24+
item.add_marker(skip_stress)

firststreet/__main__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import os
77
import logging
88
from distutils.util import strtobool
9-
10-
# Internal Imports
119
import sys
1210

11+
# Internal Imports
1312
import firststreet
1413
from firststreet.errors import InvalidArgument
1514
from firststreet.util import read_fsid_file

firststreet/api/adaptation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ class Adaptation(Api):
1919
get_summary: Retrieves a list of Adaptation Summary for the given list of IDs
2020
"""
2121

22-
def get_detail(self, fsids, csv=False, limit=100):
22+
def get_detail(self, fsids, csv=False, limit=100, output_dir=None):
2323
"""Retrieves adaptation detail product data from the First Street Foundation API given a list of FSIDs and
2424
returns a list of Adaptation Detail objects.
2525
2626
Args:
2727
fsids (list/file): A First Street Foundation IDs or a file of First Street Foundation IDs
2828
csv (bool): To output extracted data to a csv or not
2929
limit (int): max number of connections to make
30+
output_dir (str): The output directory to save the generated csvs
3031
Returns:
3132
A list of Adaptation Detail
3233
"""
@@ -35,13 +36,13 @@ def get_detail(self, fsids, csv=False, limit=100):
3536
product = [AdaptationDetail(api_data) for api_data in api_datas]
3637

3738
if csv:
38-
csv_format.to_csv(product, "adaptation", "detail")
39+
csv_format.to_csv(product, "adaptation", "detail", output_dir=output_dir)
3940

4041
logging.info("Adaptation Detail Data Ready.")
4142

4243
return product
4344

44-
def get_summary(self, fsids, location_type, csv=False, limit=100):
45+
def get_summary(self, fsids, location_type, csv=False, limit=100, output_dir=None):
4546
"""Retrieves adaptation summary product data from the First Street Foundation API given a list of FSIDs and
4647
returns a list of Adaptation Summary objects.
4748
@@ -50,6 +51,7 @@ def get_summary(self, fsids, location_type, csv=False, limit=100):
5051
location_type (str): The location lookup type
5152
csv (bool): To output extracted data to a csv or not
5253
limit (int): max number of connections to make
54+
output_dir (str): The output directory to save the generated csvs
5355
Returns:
5456
A list of Adaptation Summary
5557
"""
@@ -64,7 +66,7 @@ def get_summary(self, fsids, location_type, csv=False, limit=100):
6466
product = [AdaptationSummary(api_data) for api_data in api_datas]
6567

6668
if csv:
67-
csv_format.to_csv(product, "adaptation", "summary", location_type)
69+
csv_format.to_csv(product, "adaptation", "summary", location_type, output_dir=output_dir)
6870

6971
logging.info("Adaptation Summary Data Ready.")
7072

firststreet/api/api.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ def call_api(self, fsids, product, product_subtype, location, limit=100):
3838
"""
3939
if not isinstance(fsids, list):
4040
if isinstance(fsids, str):
41-
if os.path.isfile(fsids):
42-
fsids = read_fsid_file(fsids)
41+
if os.path.isfile(os.getcwd() + "\\" + fsids):
42+
fsids = read_fsid_file(os.getcwd() + "\\" + fsids)
4343
else:
44-
raise InvalidArgument("File provided is not a list or a valid file. Please check the file name.")
44+
raise InvalidArgument("File provided is not a valid file. "
45+
"Please check the file name. '{}'".format(os.path.curdir + str(fsids)))
4546
else:
46-
raise InvalidArgument("File provided is not a list or a valid file. Please check the file name.")
47+
raise InvalidArgument("File provided is not a list or a valid file. "
48+
"Please check the file name. '{}'".format(os.path.curdir + str(fsids)))
4749

4850
if not fsids:
4951
raise InvalidArgument(fsids)

firststreet/api/csv_format.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
import pandas as pd
1010

1111

12-
def to_csv(data, product, product_subtype, location_type=None):
12+
def to_csv(data, product, product_subtype, location_type=None, output_dir=None):
1313
"""Receives a list of data, a product, a product subtype, and a location to create a CSV
1414
1515
Args:
1616
data (list): A list of FSF object
1717
product (str): The overall product to call
1818
product_subtype (str): The product subtype (if suitable)
1919
location_type (str): The location lookup type (if suitable)
20+
output_dir (str): The output directory to save the generated csvs
2021
"""
2122

2223
date = datetime.datetime.today().strftime('%Y_%m_%d_%H_%M_%S')
@@ -27,7 +28,8 @@ def to_csv(data, product, product_subtype, location_type=None):
2728
else:
2829
file_name = "_".join([date, product, product_subtype]) + ".csv"
2930

30-
output_dir = "/".join([os.getcwd(), "data_csv"])
31+
if not output_dir:
32+
output_dir = os.getcwd() + "\\data_csv"
3133

3234
if not os.path.exists(output_dir):
3335
os.makedirs(output_dir)
@@ -162,7 +164,7 @@ def format_adaptation_summary(data):
162164
"""
163165
df = pd.DataFrame([vars(o) for o in data]).explode('adaptation').reset_index(drop=True)
164166
df['fsid'] = df['fsid'].apply(str)
165-
return df[['fsid', 'adaptation']]
167+
return df[['fsid', 'adaptation', 'properties']]
166168

167169

168170
def format_probability_chance(data):

firststreet/api/environmental.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ class Environmental(Api):
1717
get_precipitation: Retrieves a list of Environmental Precipitation for the given list of IDs
1818
"""
1919

20-
def get_precipitation(self, fsids, csv=False, limit=100):
20+
def get_precipitation(self, fsids, csv=False, limit=100, output_dir=None):
2121
"""Retrieves environmental precipitation product data from the First Street Foundation API given a list of FSIDs
2222
and returns a list of Environmental Precipitation objects.
2323
2424
Args:
2525
fsids (list/file): A First Street Foundation IDs or a file of First Street Foundation IDs
2626
csv (bool): To output extracted data to a csv or not
2727
limit (int): max number of connections to make
28+
output_dir (str): The output directory to save the generated csvs
2829
Returns:
2930
A list of Adaptation Detail
3031
"""
@@ -34,7 +35,7 @@ def get_precipitation(self, fsids, csv=False, limit=100):
3435
product = [EnvironmentalPrecipitation(api_data) for api_data in api_datas]
3536

3637
if csv:
37-
csv_format.to_csv(product, "environmental", "precipitation", "county")
38+
csv_format.to_csv(product, "environmental", "precipitation", "county", output_dir=output_dir)
3839

3940
logging.info("Environmental Precipitation Data Ready.")
4041

firststreet/api/fema.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Fema(Api):
1818
get_nfip: Retrieves a list of Fema Nfip for the given list of IDs
1919
"""
2020

21-
def get_nfip(self, fsids, location_type, csv=False, limit=100):
21+
def get_nfip(self, fsids, location_type, csv=False, limit=100, output_dir=None):
2222
"""Retrieves fema nfip product data from the First Street Foundation API given a list of FSIDs and
2323
returns a list of Fema Nfip objects.
2424
@@ -27,6 +27,7 @@ def get_nfip(self, fsids, location_type, csv=False, limit=100):
2727
location_type (str): The location lookup type
2828
csv (bool): To output extracted data to a csv or not
2929
limit (int): max number of connections to make
30+
output_dir (str): The output directory to save the generated csvs
3031
Returns:
3132
A list of Fema Nfip
3233
Raises:
@@ -44,7 +45,7 @@ def get_nfip(self, fsids, location_type, csv=False, limit=100):
4445
product = [FemaNfip(api_data) for api_data in api_datas]
4546

4647
if csv:
47-
csv_format.to_csv(product, "fema", "nfip", location_type)
48+
csv_format.to_csv(product, "fema", "nfip", location_type, output_dir=output_dir)
4849

4950
logging.info("Fema Nfip Data Ready.")
5051

firststreet/api/historic.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ class Historic(Api):
1919
get_summary: Retrieves a list of Historic Summary for the given list of IDs
2020
"""
2121

22-
def get_event(self, fsids, csv=False, limit=100):
22+
def get_event(self, fsids, csv=False, limit=100, output_dir=None):
2323
"""Retrieves historic event product data from the First Street Foundation API given a list of FSIDs and
2424
returns a list of Historic Event objects.
2525
2626
Args:
2727
fsids (list/file): A First Street Foundation IDs or a file of First Street Foundation IDs
2828
csv (bool): To output extracted data to a csv or not
2929
limit (int): max number of connections to make
30+
output_dir (str): The output directory to save the generated csvs
3031
Returns:
3132
A list of Historic Event
3233
"""
@@ -36,13 +37,13 @@ def get_event(self, fsids, csv=False, limit=100):
3637
product = [HistoricEvent(api_data) for api_data in api_datas]
3738

3839
if csv:
39-
csv_format.to_csv(product, "historic", "event")
40+
csv_format.to_csv(product, "historic", "event", output_dir=output_dir)
4041

4142
logging.info("Historic Event Data Ready.")
4243

4344
return product
4445

45-
def get_summary(self, fsids, location_type, csv=False, limit=100):
46+
def get_summary(self, fsids, location_type, csv=False, limit=100, output_dir=None):
4647
"""Retrieves historic summary product data from the First Street Foundation API given a list of FSIDs and
4748
returns a list of Historic Summary objects.
4849
@@ -51,6 +52,7 @@ def get_summary(self, fsids, location_type, csv=False, limit=100):
5152
location_type (str): The location lookup type
5253
csv (bool): To output extracted data to a csv or not
5354
limit (int): max number of connections to make
55+
output_dir (str): The output directory to save the generated csvs
5456
Returns:
5557
A list of Historic Summary
5658
"""
@@ -65,7 +67,7 @@ def get_summary(self, fsids, location_type, csv=False, limit=100):
6567
product = [HistoricSummary(api_data) for api_data in api_datas]
6668

6769
if csv:
68-
csv_format.to_csv(product, "historic", "summary", location_type)
70+
csv_format.to_csv(product, "historic", "summary", location_type, output_dir=output_dir)
6971

7072
logging.info("Historic Summary Data Ready.")
7173

0 commit comments

Comments
 (0)