Skip to content
Merged

labs #1321

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f0e5190
Create functions_oleksii_rak.py
Lesyk01 Mar 28, 2026
62a158f
Create decorators_oleksii_rak.py
Lesyk01 Mar 28, 2026
48010b9
Merge branch 'canbula:master' into master
Lesyk01 Apr 5, 2026
3b3b8f8
Create awaitme_oleksii_rak.py
Lesyk01 Apr 5, 2026
e84a223
Create timer_oleksii_rak.py
Lesyk01 Apr 5, 2026
a97ebc0
Delete Week04/functions_oleksii_rak.py
Lesyk01 May 5, 2026
c67b591
Merge branch 'canbula:master' into master
Lesyk01 May 5, 2026
67a2cfa
Merge branch 'canbula:master' into master
Lesyk01 Jun 4, 2026
6bc19bb
Add threaded decorator for concurrent function execution
Lesyk01 Jun 4, 2026
cd0f27b
Add awaitme decorator for async function handling
Lesyk01 Jun 4, 2026
e574893
Delete Week04/Week04/decorators_oleksii_rak.py
Lesyk01 Jun 4, 2026
cfeed25
Add performance decorator with memory and time tracking
Lesyk01 Jun 4, 2026
aa1094b
Add custom equation and function call counter
Lesyk01 Jun 4, 2026
99dd701
Update return type annotation for fn_w_counter
Lesyk01 Jun 4, 2026
e6c91d2
Remove unused inspect import and simplify code
Lesyk01 Jun 4, 2026
6f1b856
Refactor performance decorator to track metrics correctly
Lesyk01 Jun 4, 2026
821f6a4
Fix formatting and remove unnecessary whitespace
Lesyk01 Jun 4, 2026
fda35a0
Update functions_oleksii_rak.py
Lesyk01 Jun 4, 2026
7595b0d
Implement dining philosophers problem with threading
Lesyk01 Jun 4, 2026
5c79625
Add multiprocessing implementation for Pi calculation
Lesyk01 Jun 4, 2026
ab957a7
Delete awaitme_oleksii_rak.py
canbula Jun 4, 2026
7bc2202
Delete Week10/multiprocess_pi_oleksii_rak.py
canbula Jun 4, 2026
accb9a7
Delete Week09/philosophers_oleksii_rak.py
canbula Jun 4, 2026
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
28 changes: 28 additions & 0 deletions Week04/decorators_oleksii_rak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import time
import tracemalloc
from functools import wraps

def performance(func):
@wraps(func)
def wrapper(*args, **kwargs):
performance.counter += 1

tracemalloc.start()
start_time = time.perf_counter()

result = func(*args, **kwargs)

end_time = time.perf_counter()
_, peak_mem = tracemalloc.get_traced_memory()
tracemalloc.stop()

performance.total_time += (end_time - start_time)
performance.total_mem += peak_mem

return result

return wrapper

performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0
25 changes: 25 additions & 0 deletions Week04/functions_oleksii_rak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
custom_power = lambda x=0, /, e=1: float(x ** e)

def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
"""
Calculates a custom equation: (x**a + y**b) / c.

:param x: The first base (positional-only).
:param y: The second base (positional-only).
:param a: The first exponent (positional-or-keyword).
:param b: The second exponent (positional-or-keyword).
:param c: The divisor (keyword-only).
:return: The result of the equation as a float.
"""
return float((x ** a + y ** b) / c)

def fn_w_counter() -> (int, dict[str, int]):
if not hasattr(fn_w_counter, 'total_calls'):
fn_w_counter.total_calls = 0
fn_w_counter.callers_dict = {}

fn_w_counter.total_calls += 1

fn_w_counter.callers_dict[__name__] = fn_w_counter.callers_dict.get(__name__, 0) + 1

return fn_w_counter.total_calls, fn_w_counter.callers_dict
11 changes: 11 additions & 0 deletions Week05/awaitme_oleksii_rak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import inspect
from functools import wraps

def awaitme(func):
@wraps(func)
async def wrapper(*args, **kwargs):
if inspect.iscoroutinefunction(func):
return await func(*args, **kwargs)
return func(*args, **kwargs)

return wrapper
13 changes: 13 additions & 0 deletions Week06/timer_oleksii_rak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from time import perf_counter

class Timer:
def __init__(self):
self.start_time = 0.0
self.end_time = 0.0

def __enter__(self):
self.start_time = perf_counter()
return self

def __exit__(self, *args):
self.end_time = perf_counter()
21 changes: 21 additions & 0 deletions Week07/threaded_oleksii_rak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import threading
from functools import wraps

def threaded(n: int):
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
threads = []

for _ in range(n):
t = threading.Thread(target=func, args=args, kwargs=kwargs)
threads.append(t)

for t in threads:
t.start()

for t in threads:
t.join()

return wrapper
return decorator
Loading