pip install hatch
code $(hatch config find)
- under
[template.licenses]
- set
headers=false
- set
default=[]
- set
- under
[template.plugins.default]
setsrc-layout = true
hatch new traffic_light_counter
This creates a new folder called traffic_light_counter
, but we actually want to copy the contents out of the folder. You can do it manually, or paste the below into the terminal;
rm traffic-light-counter/README.md; mv traffic-light-counter/* .; rmdir traffic-light-counter/
- Add pandas dependency
dependencies = ["pandas"]
hatch shell
hatch run cov
With the following contents
.pytest_cache
__pycache__
dist
Copy copy the code below into /src/traffic_light_counter/count_red_amber_green.py
def count_red_amber_green(column):
column = column.str.lower().str.strip()
return {
"red" : (
column.str.endswith("red" )
| column.str.startswith("red" )
).sum(),
"amber" : (
column.str.endswith("amber")
| column.str.startswith("amber")
).sum(),
"green" : (
column.str.endswith("green")
| column.str.startswith("green")
).sum(),
}
Copy copy the code below into /src/traffic_light_counter/__init__.py
from .count_red_amber_green import count_red_amber_green
Copy the code below to a new file in the tests
folder:
tests/test_count_red_amber_green.py
def test_count_red_amber_green():
import pandas as pd
from traffic_light_counter import count_red_amber_green
input = pd.Series([
"Amber",
"---red",
"red--",
"green",
"green ",
])
expected_result = {"red": 2, "amber": 1, "green": 2}
actual_result = count_red_amber_green(input)
assert actual_result==expected_result
Get hatch to run tests, including code coverage report;
hatch run cov
NOTE: having problems here to fix try check pyproject.toml is correct:
[tool.hatch.version]
path = "src/traffic_light_counter/__about__.py"
then run
exit
hatch env purge
hatch shell
hatch build
exit
pip install dist/traffic_light_counter-0.0.1-py3-none-any.whl
Ignore this section of the readme
Cleanup workspace
rm -r src; rm -r tests; rm pyproject.toml; rm .gitignore; rm -r -f .pytest_cache; rm -r -f dist;rm .coverage