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

[32.3] [Introduction to Python] #55

Open
wants to merge 4 commits 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
2 changes: 2 additions & 0 deletions computer-science/32.3/examples/divide/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.venv
/__pycache__
2 changes: 2 additions & 0 deletions computer-science/32.3/examples/divide/divide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def divide(a_number, other_number):
return a_number / other_number
7 changes: 7 additions & 0 deletions computer-science/32.3/examples/divide/test_divide.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest
from divide import divide


def test_divide_when_other_number_is_zero_raises_an_exception():
with pytest.raises(ZeroDivisionError, match="division by zero"):
divide(2, 0)
2 changes: 2 additions & 0 deletions computer-science/32.3/examples/odd/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.venv
/__pycache__
2 changes: 2 additions & 0 deletions computer-science/32.3/examples/odd/is_odd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def is_odd(number):
return number % 2 != 0
9 changes: 9 additions & 0 deletions computer-science/32.3/examples/odd/test_is_odd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from is_odd import is_odd


def test_is_odd_when_number_is_odd_returns_true():
assert is_odd(3) is True


def test_is_odd_when_number_is_even_returns_false():
assert is_odd(2) is False
2 changes: 2 additions & 0 deletions computer-science/32.3/examples/pokemons/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.venv
/__pycache__
10 changes: 10 additions & 0 deletions computer-science/32.3/examples/pokemons/pokemon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import json


def retrieve_pokemons_by_type(type, filepath):
with open(filepath) as file:
pokemons = json.load(file)["results"]
pokemons_by_type = [
pokemon for pokemon in pokemons if type in pokemon["type"]
]
return pokemons_by_type
38 changes: 38 additions & 0 deletions computer-science/32.3/examples/pokemons/test_pokemon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
from unittest.mock import mock_open, patch
from pokemon import retrieve_pokemons_by_type


def test_retrieve_pokemons_by_type():
grass_type_pokemon = {
"national_number": "001",
"evolution": None,
"name": "Bulbasaur",
"type": ["Grass", "Poison"],
"total": 318,
"hp": 45,
"attack": 49,
"defense": 49,
"sp_atk": 65,
"sp_def": 65,
"speed": 45,
}
water_type_pokemon = {
"national_number": "007",
"evolution": None,
"name": "Squirtle",
"type": ["Water"],
"total": 314,
"hp": 44,
"attack": 48,
"defense": 65,
"sp_atk": 50,
"sp_def": 64,
"speed": 43,
}
pokemon_json_content = json.dumps(
{"results": [grass_type_pokemon, water_type_pokemon]})
with patch("builtins.open", mock_open(read_data=pokemon_json_content)):
assert retrieve_pokemons_by_type("Grass", "dummy") == [
grass_type_pokemon
]