Skip to content

Commit c2760d8

Browse files
committed
update demos; remove pygames
1 parent 043edc2 commit c2760d8

File tree

8 files changed

+76
-127
lines changed

8 files changed

+76
-127
lines changed

demos/fibos/fibo.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
$ python3 fibo.py 10
55
"""
66

7-
import math
7+
import sys
88

9-
def fib(n):
9+
10+
def fib(n: int) -> None:
1011
"""
1112
prints Fibonacci series up to nth term
1213
"""
@@ -19,7 +20,8 @@ def fib(n):
1920
i += 1
2021
print()
2122

22-
def fib2(n):
23+
24+
def fib2(n: int) -> list[int]:
2325
"""
2426
returns list of Fibonacci series up to nth term
2527
"""
@@ -31,12 +33,14 @@ def fib2(n):
3133
return result
3234

3335

34-
def main():
35-
import sys
36+
def main() -> None:
37+
"""Main function
38+
"""
3639
fib(5)
3740
print(fib2(5))
3841
fib(int(sys.argv[1]))
3942
print(fib2(int(sys.argv[1])))
4043

44+
4145
if __name__ == "__main__":
4246
main()

demos/function_unittest/add.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11

2-
def add(a, b) -> int:
2+
def add(a: int, b: int) -> int:
3+
"""Add two numbers
4+
5+
Args:
6+
a (int): first number
7+
b (int): second number
8+
9+
Returns:
10+
int: sum of a and b
11+
"""
312
return a+b
413

514

6-
def test_add():
15+
def test_add() -> None:
16+
"""Test add function
17+
"""
718
ans = add(10, 20)
819
expected = 30
920
assert ans == expected
1021

1122

12-
def test_add2():
23+
def test_add2() -> None:
24+
"""Test add function
25+
"""
1326
ans = add(5, 99)
1427
expected = 104
1528
assert ans == expected
1629

1730

18-
def test_add3():
31+
def test_add3() -> None:
32+
"""Test add function
33+
"""
1934
ans = add(0, -10)
2035
excepted = -10
2136
assert ans == excepted

demos/function_unittest/test_main.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,33 @@
11
from main import answer, add
22

33

4-
def test_answer():
4+
def test_answer() -> None:
5+
"""Test answer function
6+
"""
57
expected = "Hello World!"
68
ans = answer()
79
assert ans == expected
810

911

10-
def test_add():
12+
def test_add() -> None:
13+
"""Test add function
14+
"""
1115
ans = add(10, 20)
1216
expected = 30
1317
assert ans == expected
1418

1519

16-
def test_add2():
20+
def test_add2() -> None:
21+
"""Test add function
22+
"""
1723
ans = add(5, 99)
1824
expected = 104
1925
assert ans == expected
2026

2127

2228
def test_add3():
29+
"""Test add function
30+
"""
2331
ans = add(0, -10)
2432
excepted = -10
2533
assert ans == excepted

demos/modules/main.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,28 @@
33
Declares question, answer, and areaOfCircle function
44
"""
55

6-
import math # first import standard library modules
7-
import module2 # then import user-defined modules
6+
import math # first import standard library modules
7+
import module2 # then import user-defined modules
88
# what happens if the imported the names following way? Uncomment, run the module and find out...
99
# from module2 import *
1010

11-
# global variables
12-
question = "What is the meaning of Life, the Universe, and Everything?"
13-
answer = 42
11+
# global variables/CONSTANTS
12+
QUESTION = "What is the meaning of Life, the Universe, and Everything?"
13+
ANSWER = 42
1414

1515
# a function
16-
def areaOfCircle(radius):
16+
17+
18+
def area_of_circle(radius):
1719
"""
1820
Given radius of a circle, finds and returns its area.
1921
"""
2022
return math.pi*radius**2
2123

22-
def printModuleInfo():
24+
25+
def print_module_info() -> None:
26+
"""Prints module information
27+
"""
2328
import module2
2429
print(__doc__)
2530
print(__file__)
@@ -30,16 +35,23 @@ def printModuleInfo():
3035
print('module2.__name__ = ', module2.__name__)
3136
print('module2.__package__ = ', module2.__package__)
3237

33-
def accessModule2Names():
34-
print(module2.question)
35-
print(module2.answer)
3638

37-
def accessThisModuleNames():
38-
print(question)
39-
print(answer)
40-
area = areaOfCircle(3)
41-
print("area = {:.2f}".format(area))
39+
def access_module2_names() -> None:
40+
"""Access names defined in module2
41+
"""
42+
print(module2.QUESTION)
43+
print(module2.ANSWER)
44+
45+
46+
def access_this_module_names() -> None:
47+
"""Access names defined in this module
48+
"""
49+
print(QUESTION)
50+
print(ANSWER)
51+
area = area_of_circle(3)
52+
print(f"area = {area:.2f}")
53+
4254

4355
if __name__ == '__main__':
44-
accessModule2Names()
45-
accessThisModuleNames()
56+
access_module2_names()
57+
access_this_module_names()

demos/modules/module2.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
circumference function
44
"""
55
import math
6-
#import module1
6+
# import module1
77

8-
question = "What is your quest?"
9-
answer = "To seek the holy grail."
8+
QUESTION = "What is your quest?"
9+
ANSWER = "To seek the holy grail."
1010

1111

1212
def circumference(radius):
@@ -15,13 +15,15 @@ def circumference(radius):
1515
"""
1616
return math.pi*radius*2
1717

18+
1819
def test():
1920
"""
2021
Function to test names defined in this module.
2122
"""
22-
print(question)
23-
print(answer)
24-
print("circumference = {:.2f}".format(circumference(3)))
23+
print(QUESTION)
24+
print(ANSWER)
25+
print(f"circumference={circumference(3)}")
26+
2527

2628
# what happens if the import guard is not used and this module is imported?
2729
# comment out the following if statement and directly call test() and run main.py module to find out!

demos/pygamedemos/ball.png

-236 KB
Binary file not shown.

demos/pygamedemos/demo1.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

demos/pygamedemos/demo2.py

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)