Skip to content
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
7 changes: 6 additions & 1 deletion src/context_manager/lab1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
# Zaimplementuj context manager za pomocą klasy

class MyContextManager:
pass

def __enter__(self):
print('__enter__')
return self

def __exit__(self, exc_type, exc_val, exc_tb):
print(f'__exit__({exc_type}, {exc_val}, {exc_tb})')
15 changes: 13 additions & 2 deletions src/context_manager/lab2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@
# Zadanie polega wykorzystaniu context managera jako timera. Tak uzupełnij poniższa klasę aby przeszedł test
# /tests/context_manager/test_lab2.py

class Timer:
pass
import time


class Timer:
def __init__(self):
self.time = None
self.enter = None
self.exit = None

def __enter__(self):
self.enter = int(time.time())
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.exit = int(time.time())
self.time = self.exit-self.enter

return self
14 changes: 13 additions & 1 deletion src/context_manager/lab3.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
# https://docs.python.org/3/library/sqlite3.html
# /tests/context_manager/test_lab3.py

import sqlite3 as sql
from contextlib import contextmanager

@contextmanager
def open_db(file_name: str):
pass
try:
con = sql.connect(file_name)
yield con.cursor()
except ConnectionError as e:
print(e)
print('Błąd połączenia z bazą')
raise
finally:
con.close()



Expand Down
35 changes: 23 additions & 12 deletions src/decorators/decorator_1.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
"""
Zaimplementuj dekorator, który sprawdzi, czy dekorowana funkcja ma zdefiniowane typingi (dla zmiennych oraz zwracanego obiektu)
Jeżeli brak jakiegokolwiek typingu, to udekorowana funkcja przy próbie wywołania nie powinna się wykonać,
powinna natomiast zwrócić string, z komunikatem:
"add typings to function <nazwa_funkcji>, please!"
gdzie nazwa_funkcji jest nazwą dekorowanej funkcji.
"""
from functools import wraps


def require_typing(fn):
pass
# """
# Zaimplementuj dekorator, który sprawdzi, czy dekorowana funkcja ma zdefiniowane typingi (dla zmiennych oraz zwracanego obiektu)
# Jeżeli brak jakiegokolwiek typingu, to udekorowana funkcja przy próbie wywołania nie powinna się wykonać,
# powinna natomiast zwrócić string, z komunikatem:
# "add typings to function <nazwa_funkcji>, please!"
# gdzie nazwa_funkcji jest nazwą dekorowanej funkcji.
# """
from inspect import signature


def require_typing(func):
def wrapper(*args, **kwargs):
func_signature = signature(func)

if func_signature.return_annotation is func_signature.empty:
return f'Add typing to function {func.__name__}, please!'

for parameter in func_signature.parameters.items():
if parameter[1].annotation is func_signature.empty:
return f'Add typing to function {func.__name__}, please!'

return func(*args, **kwargs)
return wrapper
22 changes: 14 additions & 8 deletions src/decorators/decorator_2.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
"""
Zaimplementuj dekorator klas, który automatycznie uzupełni docstringi wszystkich utworzonych metod w dekorowanej klasie.
Tekst, którym zostaną uzupełnione docstringi będzie przekazywany jako parametr do dekoratora (funkcji tworzącej dekoratory).
Nie zmieniaj docstringów metod specjalnych (takich jak __init__, czy __repr__).
"""


# """
# Zaimplementuj dekorator klas, który automatycznie uzupełni docstringi wszystkich utworzonych metod w dekorowanej klasie.
# Tekst, którym zostaną uzupełnione docstringi będzie przekazywany jako parametr do dekoratora (funkcji tworzącej dekoratory).
# Nie zmieniaj docstringów metod specjalnych (takich jak __init__, czy __repr__).
# """
def deco_doc(new_docstring):
pass
def dec(cls):
method_list = [attribute for attribute in dir(cls) if
callable(getattr(cls, attribute))
and attribute.startswith('__') is False]

for method in method_list:
getattr(cls, method).__doc__ = new_docstring
return cls
return dec
3 changes: 2 additions & 1 deletion src/generators/HFWC.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@


def even_numbers(n):
pass
generator_comprehension = (i for i in range(n + 1) if i % 3 and not i % 2)
return generator_comprehension
35 changes: 22 additions & 13 deletions src/iterators/HFWC.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"""
Klasa ClientsList, obsługuje bazę klientów, baza zawiera informacje o tym jaki jest
numer pesel klienta, oraz czy jest aktywnym klientem, jej funkcjonalności są następujące:

- Metoda add_client przyjmuje za argument numer pesel, powoduje dodanie klienta do
listy klientów wraz z przypisaniem mu wartości True do flagi is_active
- Metoda remove_client przyjumuje za argument numer pesel i zmienia wartośc flagi
is_active na False nie powoduje usunięcia rekordu dla
- Zadanie polega na zaimplenentowaniu metod dzięki którym podczas iterowania po
obiekcie klasy ClientsList będą zwracane tylko numery pesel dla osób które są
aktywnymi klientami (wartość flagi is_active to True)
"""

# """
# Klasa ClientsList, obsługuje bazę klientów, baza zawiera informacje o tym jaki jest
# numer pesel klienta, oraz czy jest aktywnym klientem, jej funkcjonalności są następujące:
#
# - Metoda add_client przyjmuje za argument numer pesel, powoduje dodanie klienta do
# listy klientów wraz z przypisaniem mu wartości True do flagi is_active
# - Metoda remove_client przyjumuje za argument numer pesel i zmienia wartośc flagi
# is_active na False nie powoduje usunięcia rekordu dla
# - Zadanie polega na zaimplenentowaniu metod dzięki którym podczas iterowania po
# obiekcie klasy ClientsList będą zwracane tylko numery pesel dla osób które są
# aktywnymi klientami (wartość flagi is_active to True)
# """

class ClientsList:
clients: list
Expand All @@ -26,3 +25,13 @@ def remove_client(self, pesel: str):
if self.clients[i]["pesel"] == pesel:
self.clients[i]["is_active"] = False
break

def __iter__(self):
self.__clients_iterator = iter(self.clients)
return self

def __next__(self):
while True:
client = next(self.__clients_iterator)
if client['is_active']:
return client['pesel']
4 changes: 2 additions & 2 deletions tests/context_manager/test_lab1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from context_manager.lab1 import MyContextManager
from src.context_manager.lab1 import MyContextManager


def test_my_context_manager():
with MyContextManager():
print('Run')
print('Run')
4 changes: 1 addition & 3 deletions tests/context_manager/test_lab2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import time
from src.context_manager.lab2 import Timer

from context_manager.lab2 import Timer

def test_timer():

with Timer() as timer:
time.sleep(1)

assert timer.time == 1

Binary file added tests/context_manager/test_lab3.db
Binary file not shown.
10 changes: 5 additions & 5 deletions tests/context_manager/test_lab3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@


def test_open_db():
with open_db(file_name="./test.db") as cursor:
cursor.execute('CREATE TABLE IF NOT EXISTS "test"( "message" TEXT )')
cursor.execute('INSERT INTO "test"("message") VALUES("python is ok")')
cursor.execute('SELECT * FROM "test"')
print(cursor.fetchall())
with open_db(file_name="./test_lab3.db") as cursor:
cursor.execute('CREATE TABLE IF NOT EXISTS "test_lab3"( "message" TEXT )')
cursor.execute('INSERT INTO "test_lab3"("message") VALUES("python is ok")')
cursor.execute('SELECT * FROM "test_lab3"')
print(cursor.fetchall())