Skip to content

Commit 3303211

Browse files
authored
Merge branch 'master' into arcade-platformer
2 parents bab6940 + 0b39a95 commit 3303211

27 files changed

+689
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
venv
2+
__pycache__
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: gunicorn app:app
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Source files supporting the [Deploying a Python Flask Example Application Using Heroku](https://realpython.com/python-flask-example-heroku/) article on [Real Python](https://realpython.com/).

python-flask-example-heroku/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import os
2+
from flask import Flask
3+
4+
app = Flask(__name__)
5+
env_config = os.getenv("APP_SETTINGS", "config.DevelopmentConfig")
6+
app.config.from_object(env_config)
7+
8+
9+
@app.route("/")
10+
def index():
11+
secret_key = app.config.get("SECRET_KEY")
12+
return f"The configured secret key is {secret_key}."
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os
2+
3+
4+
class Config:
5+
DEBUG = False
6+
DEVELOPMENT = False
7+
SECRET_KEY = os.getenv("SECRET_KEY", "this-is-the-default-key")
8+
9+
10+
class ProductionConfig(Config):
11+
pass
12+
13+
14+
class StagingConfig(Config):
15+
DEBUG = True
16+
17+
18+
class DevelopmentConfig(Config):
19+
DEBUG = True
20+
DEVELOPMENT = True
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
click==7.1.2
2+
Flask==1.1.2
3+
gunicorn==20.0.4
4+
itsdangerous==1.1.0
5+
Jinja2==2.11.2
6+
MarkupSafe==1.1.1
7+
Werkzeug==1.0.1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Python Interview Problems – Parsing CSV Files
2+
3+
Corresponding code for ["Python Interview Problems – Parsing CSV Files."](https://realpython.com/python-interview-problems-parsing-csv-python-files/)
4+
5+
The `skeleton_code` directory contains pytest fixtures and module files to get you set up to run pytest. There are no tests in the files, which pytest tells you when you run it:
6+
7+
```console
8+
$ pytest test_weather_v1.py
9+
======================================= test session starts ========================================
10+
platform linux -- Python 3.7.1, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
11+
rootdir: /home/jima/coding/materials_realpy/python-interview-problems-parsing-csv/skeleton_code
12+
collected 0 items
13+
14+
====================================== no tests ran in 0.00s =======================================
15+
```
16+
17+
The `full_code` directory contains the source files we used to generate the examples in the article.
18+
19+
Good luck!
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env python3
2+
""" Reusable CSV parser for both football and weather data. """
3+
import csv
4+
5+
6+
def get_next_result(csv_file, func):
7+
for stats in csv.DictReader(csv_file):
8+
yield func(stats)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python3
2+
""" Find Minimum Goal Differential
3+
Write a program that takes a filename on the command line and processes the
4+
CSV contents. The contents will be a CSV file with end-of-season football
5+
standings for the English Premier League.
6+
Determine which team had the smallest goal differential that season.
7+
The first line of the CSV file will be column headers:
8+
9+
Team,Games,Wins,Losses,Draws,Goals For,Goals Against
10+
11+
Write unit tests with Pytest to test your program.
12+
"""
13+
import csv_reader
14+
15+
16+
def get_name_and_diff(team_stats):
17+
diff = int(team_stats["Goals For"]) - int(team_stats["Goals Against"])
18+
return team_stats["Team"], abs(diff)
19+
20+
21+
def get_min_score_difference(filename):
22+
with open(filename, "r", newline="") as csv_data:
23+
return min(
24+
csv_reader.get_next_result(csv_data, get_name_and_diff),
25+
key=lambda item: item[1],
26+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pandas as pd
2+
3+
4+
def read_data(csv_file):
5+
return (
6+
pd.read_csv(csv_file)
7+
.rename(
8+
columns={
9+
"Team": "team_name",
10+
"Goals For": "goals",
11+
"Goals Against": "goals_allowed",
12+
}
13+
)
14+
.assign(goal_difference=lambda df: abs(df.goals - df.goals_allowed))
15+
)
16+
17+
18+
def get_min_difference(parsed_data):
19+
return parsed_data.goal_difference.min()
20+
21+
22+
def get_team(parsed_data, min_score_difference):
23+
return (
24+
parsed_data.query(f"goal_difference == {min_score_difference}")
25+
.reset_index()
26+
.loc[0, "team_name"]
27+
)
28+
29+
30+
def get_min_score_difference(csv_file):
31+
df = read_data(csv_file)
32+
min_diff = get_min_difference(df)
33+
team = get_team(df, min_diff)
34+
return team, min_diff

0 commit comments

Comments
 (0)