Skip to content

Commit

Permalink
update demos; remove pygames
Browse files Browse the repository at this point in the history
  • Loading branch information
rambasnet committed Nov 8, 2024
1 parent 043edc2 commit c2760d8
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 127 deletions.
14 changes: 9 additions & 5 deletions demos/fibos/fibo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
$ python3 fibo.py 10
"""

import math
import sys

def fib(n):

def fib(n: int) -> None:
"""
prints Fibonacci series up to nth term
"""
Expand All @@ -19,7 +20,8 @@ def fib(n):
i += 1
print()

def fib2(n):

def fib2(n: int) -> list[int]:
"""
returns list of Fibonacci series up to nth term
"""
Expand All @@ -31,12 +33,14 @@ def fib2(n):
return result


def main():
import sys
def main() -> None:
"""Main function
"""
fib(5)
print(fib2(5))
fib(int(sys.argv[1]))
print(fib2(int(sys.argv[1])))


if __name__ == "__main__":
main()
23 changes: 19 additions & 4 deletions demos/function_unittest/add.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@

def add(a, b) -> int:
def add(a: int, b: int) -> int:
"""Add two numbers
Args:
a (int): first number
b (int): second number
Returns:
int: sum of a and b
"""
return a+b


def test_add():
def test_add() -> None:
"""Test add function
"""
ans = add(10, 20)
expected = 30
assert ans == expected


def test_add2():
def test_add2() -> None:
"""Test add function
"""
ans = add(5, 99)
expected = 104
assert ans == expected


def test_add3():
def test_add3() -> None:
"""Test add function
"""
ans = add(0, -10)
excepted = -10
assert ans == excepted
14 changes: 11 additions & 3 deletions demos/function_unittest/test_main.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
from main import answer, add


def test_answer():
def test_answer() -> None:
"""Test answer function
"""
expected = "Hello World!"
ans = answer()
assert ans == expected


def test_add():
def test_add() -> None:
"""Test add function
"""
ans = add(10, 20)
expected = 30
assert ans == expected


def test_add2():
def test_add2() -> None:
"""Test add function
"""
ans = add(5, 99)
expected = 104
assert ans == expected


def test_add3():
"""Test add function
"""
ans = add(0, -10)
excepted = -10
assert ans == excepted
46 changes: 29 additions & 17 deletions demos/modules/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@
Declares question, answer, and areaOfCircle function
"""

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

# global variables
question = "What is the meaning of Life, the Universe, and Everything?"
answer = 42
# global variables/CONSTANTS
QUESTION = "What is the meaning of Life, the Universe, and Everything?"
ANSWER = 42

# a function
def areaOfCircle(radius):


def area_of_circle(radius):
"""
Given radius of a circle, finds and returns its area.
"""
return math.pi*radius**2

def printModuleInfo():

def print_module_info() -> None:
"""Prints module information
"""
import module2
print(__doc__)
print(__file__)
Expand All @@ -30,16 +35,23 @@ def printModuleInfo():
print('module2.__name__ = ', module2.__name__)
print('module2.__package__ = ', module2.__package__)

def accessModule2Names():
print(module2.question)
print(module2.answer)

def accessThisModuleNames():
print(question)
print(answer)
area = areaOfCircle(3)
print("area = {:.2f}".format(area))
def access_module2_names() -> None:
"""Access names defined in module2
"""
print(module2.QUESTION)
print(module2.ANSWER)


def access_this_module_names() -> None:
"""Access names defined in this module
"""
print(QUESTION)
print(ANSWER)
area = area_of_circle(3)
print(f"area = {area:.2f}")


if __name__ == '__main__':
accessModule2Names()
accessThisModuleNames()
access_module2_names()
access_this_module_names()
14 changes: 8 additions & 6 deletions demos/modules/module2.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
circumference function
"""
import math
#import module1
# import module1

question = "What is your quest?"
answer = "To seek the holy grail."
QUESTION = "What is your quest?"
ANSWER = "To seek the holy grail."


def circumference(radius):
Expand All @@ -15,13 +15,15 @@ def circumference(radius):
"""
return math.pi*radius*2


def test():
"""
Function to test names defined in this module.
"""
print(question)
print(answer)
print("circumference = {:.2f}".format(circumference(3)))
print(QUESTION)
print(ANSWER)
print(f"circumference={circumference(3)}")


# what happens if the import guard is not used and this module is imported?
# comment out the following if statement and directly call test() and run main.py module to find out!
Expand Down
Binary file removed demos/pygamedemos/ball.png
Binary file not shown.
37 changes: 0 additions & 37 deletions demos/pygamedemos/demo1.py

This file was deleted.

55 changes: 0 additions & 55 deletions demos/pygamedemos/demo2.py

This file was deleted.

0 comments on commit c2760d8

Please sign in to comment.