Skip to content

Commit c5a8294

Browse files
committed
Revisited App7 TalkPython Jumpstarter course notes on classes.
1 parent 7f4108b commit c5a8294

File tree

5 files changed

+193
-0
lines changed

5 files changed

+193
-0
lines changed

days/10-12-pytest/my_code/Car.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Car:
2+
3+
def __init__(self, speed=0):
4+
self.speed = speed
5+
self.odometer = 0
6+
self.time = 0
7+
8+
def say_state(self):
9+
print("I'm going {} kph!".format(self.speed))
10+
11+
def accelerate(self):
12+
self.speed += 5
13+
14+
def brake(self):
15+
if self.speed < 5:
16+
self.speed = 0
17+
else:
18+
self.speed -= 5
19+
20+
def step(self):
21+
self.odometer += self.speed
22+
self.time += 1
23+
24+
def average_speed(self):
25+
if self.time != 0:
26+
return self.odometer / self.time
27+
else:
28+
pass
29+
30+
31+
if __name__ == '__main__':
32+
33+
my_car = Car()
34+
print("I'm a car!")
35+
while True:
36+
action = input("What should I do? [A]ccelerate, [B]rake, "
37+
"show [O]dometer, or show average [S]peed?").upper()
38+
if action not in "ABOS" or len(action) != 1:
39+
print("I don't know how to do that")
40+
continue
41+
if action == 'A':
42+
my_car.accelerate()
43+
elif action == 'B':
44+
my_car.brake()
45+
elif action == 'O':
46+
print("The car has driven {} kilometers".format(my_car.odometer))
47+
elif action == 'S':
48+
print("The car's average speed was {} kph".format(my_car.average_speed()))
49+
my_car.step()
50+
my_car.say_state()

days/10-12-pytest/my_code/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
##A First Attempt at PyTest Fixtures and Parametrization (using PyCharm)
2+
3+
![Python version][python-version]
4+
5+
I found the #100daysofcode tutorials shown using Vim quite difficult to follow.
6+
7+
So I looked online for other turtorials knowing to look for pytest fixtures and parametrization.
8+
9+
The tutorial I have used for this is taken directly from the [PyCharm 2019.1 help files](https://www.jetbrains.com/help/pycharm/pytest.html)
10+
11+
This challenge served two purposes for me:
12+
1. Follow a very simple pytest tutorial using PyCharm
13+
2. Complete my first ever Pull Request.
14+
15+
I intend on writing a more advanced PyTest so will issue another PR against this submission later.
16+
17+
When I've finished the #100daysofcode I'll read Python Testing with Pytest by Brian Okken. TDD looks interesting and I'm keen to learn more!
18+
19+
[python-version]:https://img.shields.io/badge/python-3.6.5-brightgreen.svg
20+
21+
22+
23+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import pytest
2+
from Car import Car
3+
4+
speed_data = {45, 50, 55, 100}
5+
6+
# With pytest fixtures you can create small test units that can be reused across the testing module.
7+
# All you need is to mark a reusable unit with @pytest.fixture.
8+
9+
# my_car() is a fixture function that creates a Car instance with the speed value equal to 50.
10+
# It is used in test_car_accelerate and test_car_brake to verify correct execution of the
11+
# corresponding functions in the Car class.
12+
13+
# @pytest.fixture
14+
# def my_car():
15+
# return Car(50)
16+
17+
# def test_car_accelerate(my_car):
18+
# my_car.accelerate()
19+
# assert my_car.speed == 55
20+
21+
# def test_car_brake(my_car):
22+
# my_car.brake()
23+
# assert my_car.speed == 45
24+
25+
# You might want to run your tests on the predefined set of data.
26+
# PyCharm supports test parametrization implemented in pytest through @pytest.mark.parametrize.
27+
28+
@pytest.mark.parametrize("speed_accelerate", speed_data)
29+
def test_car_accelerate(speed_accelerate):
30+
car = Car(50)
31+
car.accelerate()
32+
assert car.speed == speed_accelerate
33+
34+
35+
@pytest.mark.parametrize("speed_brake", speed_data)
36+
def test_car_brake(speed_brake):
37+
car = Car(50)
38+
car.brake()
39+
assert car.speed == speed_brake
40+
41+
42+
43+
44+
45+
46+
47+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
4+
class Wizard:
5+
6+
def __init__(self, name, the_level):
7+
self.name = name
8+
self.level = the_level
9+
10+
11+
class Creature:
12+
# level
13+
# name
14+
15+
# need to generate instance attributes or instance variables
16+
17+
# add a magic method as
18+
19+
def __init__(self, name, the_level):
20+
self.name = name
21+
self.level = the_level
22+
23+
def __repr__(self):
24+
return f"Creature {self.name} of level {self.level}"
25+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# import actors
2+
from actors import Wizard, Creature
3+
4+
5+
def main():
6+
print_header()
7+
game_loop()
8+
9+
10+
def print_header():
11+
print('-------------------------')
12+
print(' WIZARD GAME')
13+
print('-------------------------')
14+
print()
15+
16+
17+
def game_loop():
18+
19+
creatures = [
20+
Creature('Toad', 1),
21+
Creature('Tiger', 12),
22+
Creature('Bat', 3),
23+
Creature('Dragon', 50),
24+
Creature('Evil Wizard', 1000),
25+
]
26+
27+
print(creatures)
28+
29+
hero = Wizard('Gandalf', 75)
30+
31+
32+
while True:
33+
34+
cmd = input('Do you [a]ttack, [l]ookaround or [r]unaway? ')
35+
if cmd == 'a':
36+
print('attack')
37+
elif cmd == 'l':
38+
print('look around')
39+
elif cmd == 'r':
40+
print('run away')
41+
else:
42+
print('OK... exiting game')
43+
break
44+
45+
46+
if __name__ == '__main__':
47+
main()
48+

0 commit comments

Comments
 (0)