Skip to content

Commit c2e49d2

Browse files
author
Tania Allard
committed
Keep files up to date
1 parent 04b04c4 commit c2e49d2

File tree

7 files changed

+76
-75
lines changed

7 files changed

+76
-75
lines changed

.gitignore/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Byte-compiled / optimized / DLL files
22
__pycache__
3-
__pycache__
43
*.py[cod]
54
*$py.class
65

@@ -104,3 +103,6 @@ ENV/
104103

105104
/data
106105
*/.DS_Store
106+
107+
# pytest
108+
.pytest_cache/

04_Testing.ipynb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"metadata": {},
2525
"source": [
2626
"There are various approaches to tests software:\n",
27-
"- Assertions: 🦄 == 🦄\n",
28-
"- Exceptions: within the code serve as ⚠️\n",
29-
"- Unit tests: investigate the behaviour of units of code (e.g functions)\n",
30-
"- Regression tests: defends against 🐛\n",
31-
"- Integration tests: ⚙️ checks that the pieces work together as expected"
27+
"- **Assertions**: 🦄 == 🦄\n",
28+
"- **Exceptions**: within the code serve as warnings ⚠️\n",
29+
"- **Unit tests**: investigate the behaviour of units of code (e.g functions)\n",
30+
"- **Regression tests**: defends against 🐛\n",
31+
"- **Integration tests**: ⚙️ checks that the pieces work together as expected"
3232
]
3333
},
3434
{
@@ -50,16 +50,12 @@
5050
"And we will modify this function too:\n",
5151
"```python\n",
5252
"def get_country(filename, country):\n",
53-
" \n",
54-
"\n",
5553
" # Load table\n",
5654
" wine = pd.read_csv(filename)\n",
5755
"\n",
5856
" # Use the country name to subset data\n",
5957
" subset_country = wine[wine['country'] == country ].copy()\n",
6058
"\n",
61-
" # Subset the\n",
62-
"\n",
6359
" # Constructing the fname\n",
6460
" today = datetime.datetime.today().strftime('%Y-%m-%d')\n",
6561
" fname = f'data/processed/{today}-winemag_{country}.csv'\n",
@@ -76,8 +72,9 @@
7672
"cell_type": "markdown",
7773
"metadata": {},
7874
"source": [
79-
"Now we need to create out testing scripts. \n",
80-
"Some resources:\n",
75+
"Now we are going to create our testing suite. \n",
76+
"To run the tests we are going to use **pytest**.\n",
77+
"You can find more information in the following resources:\n",
8178
"- Pytest usage examples can be found [here](http://doc.pytest.org/en/latest/usage.html)\n",
8279
"- Rules for [test discovery](http://doc.pytest.org/en/latest/goodpractices.html)\n",
8380
"\n",
@@ -86,7 +83,7 @@
8683
"$ touch tests/__init__.py\n",
8784
"$ touch test_03_country_subset.py\n",
8885
"```\n",
89-
"Your test scrips should start with `test`"
86+
"Your test scripts name must start with: `test`"
9087
]
9188
},
9289
{

solutions/01_subset-data-GBP.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def process_data_GBP(filename):
2727
2828
Returns:
2929
-----
30-
data_path: st
30+
fname: st
3131
Path to the created data set
3232
"""
3333

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python
2+
3+
4+
import sys
5+
import datetime
6+
7+
import pandas as pd
8+
9+
10+
def get_country(filename, country):
11+
"""
12+
Do a simple analysis per country
13+
Args:
14+
-----
15+
filename: str
16+
Path to the filename containing the wine data
17+
country: str
18+
Country to be used to subset
19+
20+
Returns:
21+
-----
22+
data_path: st
23+
Path to the created data set
24+
"""
25+
26+
# Load table
27+
wine = pd.read_csv(filename)
28+
29+
# Use the country name to subset data
30+
subset_country = wine[wine['country'] == country ].copy()
31+
subset_country.reset_index(drop=True, inplace=True)
32+
33+
# Constructing the fname
34+
today = datetime.datetime.today().strftime('%Y-%m-%d')
35+
fname = f'data/processed/{today}-winemag_{country}.csv'
36+
37+
# Saving the csv
38+
subset_country.to_csv(fname, index =False)
39+
print(fname) # print the fname from here
40+
41+
return(subset_country) #returns the data frame
42+
43+
def get_mean_price(filename):
44+
wine = pd.read_csv(filename)
45+
mean_price = wine['price'].mean()
46+
return round(mean_price, 4)
47+
48+
if __name__ == '__main__':
49+
filename = sys.argv[1]
50+
country = sys.argv[2]
51+
print(f'Subsetting: {filename}')
52+
print(f'Country searched: {country}')
53+
54+
print(get_country(filename, country))

solutions/03_country-subset.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ def get_country(filename, country):
1111
"""
1212
Do a simple analysis per country
1313
Args:
14+
-----
1415
filename: str
1516
Path to the filename containing the wine data
1617
country: str
1718
Country to be used to subset
1819
1920
Returns:
20-
21-
data_path: st
21+
-----
22+
fname: st
2223
Path to the created data set
2324
"""
2425

@@ -37,14 +38,9 @@ def get_country(filename, country):
3738

3839
# Saving the csv
3940
subset_country.to_csv(fname, index =False)
40-
print(fname) # print the fname from here
41-
42-
return(subset_country) #returns the data frame
41+
42+
return(fname)
4343

44-
def get_mean_price(filename):
45-
wine = pd.read_csv(filename)
46-
mean_price = wine['price'].mean()
47-
return round(mean_price, 4)
4844

4945
if __name__ == '__main__':
5046
filename = sys.argv[1]

solutions/test01_subset-data-GBP.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

solutions/tests/test_03_subset-country.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
country = importlib.import_module('.data.03_country-subset', 'src')
77

8-
interim_data = "data/interim/2018-04-30-winemag_priceGBP.csv"
9-
processed_data = "data/processed/2018-05-03-winemag_Chile.csv"
8+
interim_data = "data/interim/2018-05-09-winemag_priceGBP.csv"
9+
processed_data = "data/processed/2018-05-09-winemag_Chile.csv"
10+
1011

1112
def test_get_mean_price():
1213
mean_price = country.get_mean_price(processed_data)
1314
assert mean_price == 20.7865
14-
npt.assert_allclose(country.get_mean_price(processed_data) , 20.787, rtol = 0.01)
15+
npt.assert_allclose(country.get_mean_price(processed_data), 20.787, rtol = 0.01)
1516

1617
def test_get_country():
1718
df = country.get_country(interim_data, 'Chile')

0 commit comments

Comments
 (0)