FMSI 2023-24 AV 01 Python I Unit Testing
FMSI 2023-24 AV 01 Python I Unit Testing
>>> # eksponenciranje
Python primjer – primitivni tipovi
https://docs.python.org/3/tutorial/introduction.html
Python – rad sa stringovima
>>> # stringovi su imutabilni >>> w = 'h' + w[1:]
>>> w[0] = 'h' >>> w
Traceback (most recent call last): 'hello world'
File "<stdin>", line 1, in >>> # w postaje novi string
<module>
TypeError: 'str' object does not
support item assignment
>>> može se kreirati novi string
Python – metode za rad sa stringovima
>>> w = "hELLo WoRLd" False
>>> w.capitalize() >>> 'World' in w
'Hello world’ False
>>> w == 'hello world' >>> 'world' in w
False True
>>> w.lower() >>> w.find('world’)
'hello world' 6
>>> w.lower() == 'hello world' >>> w.find('bye')
True -1
>>> w.count('l') >>> w.find('l', 2)
0 2
>>> w = w.lower() >>> w.find('l', 3)
>>> w.count('l’) 3
3 >>> w.find('l', 4)
>>> w.endswith('rld') 9
True >>> w.find('l', 10)
>>> w.endswith('Rld') -1
False
>>> 'Bye' in w
Python – metode za rad sa stringovima
>>> "The sum of 1 and 2 is {0}, and their difference is {1}.".format(1+2, 1-2)
'The sum of 1 and 2 is 3, and their difference is -1.’
>>> ','.join(['abc', 'def', 'ghi'])
'abc,def,ghi’
>>> 'TestHook'.removeprefix('Test')
'Hook'
>>> 'BaseTestCase'.removeprefix('Test')
'BaseTestCase’
>>> 'MiscTests'.removesuffix('Tests')
'Misc’
>>> 'TmpDirMixin'.removesuffix('Tests')
'TmpDirMixin’
>>> 'What is it that it is which is that which isn\'t?'.replace('is', 'needs')
"What needs it that it needs which needs that which needsn’t?”
Python – metode za rad sa stringovima
>>> a = '1a,2b,3c'.split(',')
>>> a[0]
'1a'
>>> a[1]
'2b'
>>> a[2]
'3c'
Python primjer – varijable
>>> a = 2
>>> b = a * 4
>>> a * b
16
Python primjer – varijable
def is_prime(number):
for i in range(2, int(number ** 0.5) + 1):
if number % i == 0:
return False
return True
python main.py
Enter a number: 23
[2, 3, 5, 7, 11, 13, 17, 19, 23]
Python primjer – ispis prostih brojeva #3
user_input = input("Enter a number: ")
user_input = int(user_input)
numbers = range(2, user_input + 1)
def is_prime(number):
return all(number % i != 0 for i in range(2, int(number ** 0.5) + 1))
primes = [number for number in numbers if is_prime(number)]
print(primes)
python main.py
Enter a number: 23
[2, 3, 5, 7, 11, 13, 17, 19, 23]
Python kolekcije
• Lista (list), skup (set), mapa (dictionary), n-torka (tuple), string (string)
{'a': 1, 'b': 2, '3': 'c', 'd:': [1, 2, 3], 'e': {'f': 4, 'g': 5}, 'f': [1, 2, 3], 'h': None, 'i': True, 'j': False}
Domaća zadaća
• Pročitati Python Tutorial i Python FAQ
• Tok kontrole, lambda izrazi, strukture podataka, moduli, I/O, izuzeci,
klase, standardna biblioteka i virtuelna okruženja
• Rad sa argumentima komandne linije: argparse + argparse tutorijal
Formalne metode u softverskom inženjerstvu
def test_f():
assert f.f() == False
Domaća zadaća
• Definisati pytest testove sa adekvatnim tvrdnjama za funkciju koja
izračunava ocjenu na osnovu broja bodova, a zatim definisati tu
funkciju tako da su tvrdnje u testovima ispunjene.