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

Add type hints #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
37 changes: 23 additions & 14 deletions code/ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
- store, data as CSV and/or JSON
"""

from typing import Iterable, List, Sequence, Union

# https://docs.python.org/3/library/xml.etree.elementtree.html
import xml.etree
import xml.etree.ElementTree

# https://docs.python.org/3/library/argparse.html
import argparse
Expand All @@ -38,14 +41,15 @@

# https://www.tutorialspoint.com/matplotlib/matplotlib_bar_plot.htm
import numpy as np
import matplotlib
import matplotlib.pyplot as plt

from datetime import date

URL = "https://www.sheffield.ac.uk/autumn-term-2020/covid-19-statistics/"


def main():
def main() -> None:
# Argument Parsing
parser = argparse.ArgumentParser()
parser.add_argument(
Expand Down Expand Up @@ -87,7 +91,7 @@ def main():
f.write(json.dumps(data))


def transform(rows):
def transform(rows: Iterable[Sequence[str]]) -> List[List[Union[str, int]]]:
"""
The input is a list of rows, each row is a list of strings.
The return value is a list of rows, each row is a list of
Expand All @@ -102,14 +106,14 @@ def transform(rows):
result = []
for row in rows:
iso_date = str(dateutil.parser.parse(row[0]).date())
out = [iso_date]
out.extend(int(x) for x in row[1:])
out: List[Union[str, int]] = [iso_date]
out.extend([int(x) for x in row[1:]])
result.append(out)

return sorted(result)


def extract(dom):
def extract(dom: xml.etree.ElementTree.Element) -> List[List[str]]:
"""
Extract all the rows that plausibly contain data,
and return them as a list of list of strings.
Expand All @@ -119,12 +123,12 @@ def extract(dom):

result = []
for row in rows:
result.append([el.text for el in row])
result.append([el.text for el in row if el.text])

return result


def fetch():
def fetch() -> xml.etree.ElementTree.Element:
"""
Fetch the web page and return it as a parsed DOM object.
"""
Expand All @@ -135,7 +139,7 @@ def fetch():
return dom


def validate(table):
def validate(table: Iterable[Sequence[str]]) -> Sequence[Sequence[str]]:
"""
`table` should be a table of strings in list of list format.
Each row is checked to see if it is of the expected format.
Expand All @@ -152,16 +156,19 @@ def validate(table):
assert "New student" in row[2]
continue

row = [cell_value[:-1] if cell_value.endswith('*') else cell_value for cell_value in row]
row = [
cell_value[:-1] if cell_value.endswith("*") else cell_value
for cell_value in row
]
validated.append(row)

return validated


def create_visualisations(data):
def create_visualisations(data: Iterable[Sequence[Union[int, str]]]) -> None:
date_column = 0
staff_column = 1
studentColumn = 2
student_column = 2

dates = []
staff_values = []
Expand All @@ -170,13 +177,13 @@ def create_visualisations(data):
for row in data:
dates.append(row[date_column])
staff_values.append(row[staff_column])
student_values.append(row[studentColumn])
student_values.append(row[student_column])

# Similar implementation to https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/barchart.html
locations = np.arange(len(dates))
bar_width = 0.35

figure, axes = plt.subplots()
_, axes = plt.subplots()

staff_bars = axes.bar(
locations - bar_width / 2, staff_values, bar_width, label="Staff"
Expand Down Expand Up @@ -205,7 +212,9 @@ def create_visualisations(data):
plt.savefig(filename, dpi=600)


def add_column_labels(bars, axes):
def add_column_labels(
bars: matplotlib.container.BarContainer, axes: matplotlib.axes.Axes
) -> None:
for bar in bars:
height = bar.get_height()
axes.annotate(
Expand Down
2 changes: 1 addition & 1 deletion contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ like Sphinx encourages.

## [Typehints](https://docs.python.org/3/library/typing.html)

* Do not use typehints.
* Please use typehints.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:trollface:


## Tests

Expand Down