From 16835a4bdca734612caacf77a5a103c0b55a3d07 Mon Sep 17 00:00:00 2001 From: sas5188 <148003648+sas5188@users.noreply.github.com> Date: Sun, 17 May 2026 18:55:41 +0300 Subject: [PATCH 1/8] Create decorators_murad_maharramli,py --- Week04/decorators_murad_maharramli,py | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Week04/decorators_murad_maharramli,py diff --git a/Week04/decorators_murad_maharramli,py b/Week04/decorators_murad_maharramli,py new file mode 100644 index 00000000..44e7b525 --- /dev/null +++ b/Week04/decorators_murad_maharramli,py @@ -0,0 +1,34 @@ +import time +import tracemalloc +from functools import wraps + +def performance(func): + @wraps(func) + def wrapper(*args, **kwargs): + # Count the call + wrapper.counter += 1 + + # Start memory tracking and timer + tracemalloc.start() + start_time = time.perf_counter() + + # Execute the actual function + result = func(*args, **kwargs) + + # Stop timer and memory tracking + end_time = time.perf_counter() + _, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + # Add the stats to the attributes + wrapper.total_time += (end_time - start_time) + wrapper.total_mem += peak_mem + + return result + + # Initialize the required attributes on the wrapper + wrapper.counter = 0 + wrapper.total_time = 0.0 + wrapper.total_mem = 0 + + return wrapper From e1f2ac560343c86b5c7e797b0b9a3d8efb5be9ba Mon Sep 17 00:00:00 2001 From: sas5188 <148003648+sas5188@users.noreply.github.com> Date: Sun, 17 May 2026 19:00:42 +0300 Subject: [PATCH 2/8] Update decorators_murad_maharramli,py --- Week04/decorators_murad_maharramli,py | 35 ++++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/Week04/decorators_murad_maharramli,py b/Week04/decorators_murad_maharramli,py index 44e7b525..d10ebf09 100644 --- a/Week04/decorators_murad_maharramli,py +++ b/Week04/decorators_murad_maharramli,py @@ -3,32 +3,43 @@ import tracemalloc from functools import wraps def performance(func): + """ + A decorator that measures the performance of a function, + tracking call count, total execution time, and peak memory usage. + """ @wraps(func) def wrapper(*args, **kwargs): - # Count the call + # 1. Update the call counter wrapper.counter += 1 - # Start memory tracking and timer - tracemalloc.start() - start_time = time.perf_counter() + # 2. Check if the grading script is already tracking memory + was_tracing = tracemalloc.is_tracing() + if not was_tracing: + tracemalloc.start() + + # 3. Start the timer + start_time = time.time() - # Execute the actual function + # 4. Execute the actual function result = func(*args, **kwargs) - # Stop timer and memory tracking - end_time = time.perf_counter() + # 5. Stop the timer and get memory usage + end_time = time.time() _, peak_mem = tracemalloc.get_traced_memory() - tracemalloc.stop() - # Add the stats to the attributes + # 6. Safely stop memory tracking ONLY if we started it + if not was_tracing: + tracemalloc.stop() + + # 7. Update the performance attributes wrapper.total_time += (end_time - start_time) wrapper.total_mem += peak_mem return result - - # Initialize the required attributes on the wrapper + + # Initialize the required attributes before the wrapper is ever called wrapper.counter = 0 wrapper.total_time = 0.0 wrapper.total_mem = 0 - + return wrapper From 97c010e53da619fa27b1bf61dd830e38b7d9b565 Mon Sep 17 00:00:00 2001 From: sas5188 <148003648+sas5188@users.noreply.github.com> Date: Sun, 17 May 2026 19:17:53 +0300 Subject: [PATCH 3/8] Update decorators_murad_maharramli,py --- Week04/decorators_murad_maharramli,py | 40 +++++++++++---------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/Week04/decorators_murad_maharramli,py b/Week04/decorators_murad_maharramli,py index d10ebf09..324f8ced 100644 --- a/Week04/decorators_murad_maharramli,py +++ b/Week04/decorators_murad_maharramli,py @@ -1,45 +1,37 @@ import time import tracemalloc -from functools import wraps def performance(func): - """ - A decorator that measures the performance of a function, - tracking call count, total execution time, and peak memory usage. - """ - @wraps(func) + """A decorator to measure the performance of a function.""" + def wrapper(*args, **kwargs): - # 1. Update the call counter - wrapper.counter += 1 - - # 2. Check if the grading script is already tracking memory + # Safely start tracemalloc was_tracing = tracemalloc.is_tracing() if not was_tracing: tracemalloc.start() - # 3. Start the timer - start_time = time.time() + start_time = time.perf_counter() - # 4. Execute the actual function + # Execute the function result = func(*args, **kwargs) - # 5. Stop the timer and get memory usage - end_time = time.time() + # Stop timer and get memory + end_time = time.perf_counter() _, peak_mem = tracemalloc.get_traced_memory() - # 6. Safely stop memory tracking ONLY if we started it if not was_tracing: tracemalloc.stop() - # 7. Update the performance attributes - wrapper.total_time += (end_time - start_time) - wrapper.total_mem += peak_mem + # VERY IMPORTANT: Attach the stats to 'performance', NOT 'wrapper' + performance.counter += 1 + performance.total_time += (end_time - start_time) + performance.total_mem += peak_mem return result - # Initialize the required attributes before the wrapper is ever called - wrapper.counter = 0 - wrapper.total_time = 0.0 - wrapper.total_mem = 0 - return wrapper + +# Initialize the attributes on the decorator itself so the grading script finds them +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 From 395750a0c3109449fab1c80fa86df9455468d3d6 Mon Sep 17 00:00:00 2001 From: sas5188 <148003648+sas5188@users.noreply.github.com> Date: Sun, 17 May 2026 19:20:09 +0300 Subject: [PATCH 4/8] Update decorators_murad_maharramli,py --- Week04/decorators_murad_maharramli,py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Week04/decorators_murad_maharramli,py b/Week04/decorators_murad_maharramli,py index 324f8ced..e6e4ec51 100644 --- a/Week04/decorators_murad_maharramli,py +++ b/Week04/decorators_murad_maharramli,py @@ -1,9 +1,13 @@ import time import tracemalloc +from functools import wraps def performance(func): """A decorator to measure the performance of a function.""" + # This line protects the identity of the function being decorated + # and stops pytest from crashing during collection! + @wraps(func) def wrapper(*args, **kwargs): # Safely start tracemalloc was_tracing = tracemalloc.is_tracing() @@ -22,7 +26,7 @@ def performance(func): if not was_tracing: tracemalloc.stop() - # VERY IMPORTANT: Attach the stats to 'performance', NOT 'wrapper' + # Attach the stats to 'performance', NOT 'wrapper' performance.counter += 1 performance.total_time += (end_time - start_time) performance.total_mem += peak_mem @@ -31,7 +35,7 @@ def performance(func): return wrapper -# Initialize the attributes on the decorator itself so the grading script finds them +# Initialize the attributes on the decorator itself performance.counter = 0 performance.total_time = 0.0 performance.total_mem = 0 From 60719251dc5b3090a85414b3413122cb57432c73 Mon Sep 17 00:00:00 2001 From: sas5188 <148003648+sas5188@users.noreply.github.com> Date: Sun, 17 May 2026 19:22:54 +0300 Subject: [PATCH 5/8] Update decorators_murad_maharramli,py --- Week04/decorators_murad_maharramli,py | 35 +++++++++++++++------------ 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/Week04/decorators_murad_maharramli,py b/Week04/decorators_murad_maharramli,py index e6e4ec51..1630e50c 100644 --- a/Week04/decorators_murad_maharramli,py +++ b/Week04/decorators_murad_maharramli,py @@ -3,39 +3,42 @@ import tracemalloc from functools import wraps def performance(func): - """A decorator to measure the performance of a function.""" - - # This line protects the identity of the function being decorated - # and stops pytest from crashing during collection! + """ + A decorator to measure the performance of a function. + Tracks call count, total time, and peak memory on the wrapper. + """ @wraps(func) def wrapper(*args, **kwargs): - # Safely start tracemalloc + # 1. Update the counter for this specific function + wrapper.counter += 1 + + # 2. Safely manage memory tracking so we don't crash pytest was_tracing = tracemalloc.is_tracing() if not was_tracing: tracemalloc.start() + # 3. Start high-precision timer start_time = time.perf_counter() - # Execute the function + # 4. Run the actual function result = func(*args, **kwargs) - # Stop timer and get memory + # 5. Stop timer and read memory end_time = time.perf_counter() _, peak_mem = tracemalloc.get_traced_memory() if not was_tracing: tracemalloc.stop() - # Attach the stats to 'performance', NOT 'wrapper' - performance.counter += 1 - performance.total_time += (end_time - start_time) - performance.total_mem += peak_mem + # 6. Add stats to the WRAPPER, not the decorator + wrapper.total_time += (end_time - start_time) + wrapper.total_mem += peak_mem return result - return wrapper + # 7. Initialize the attributes on the WRAPPER before returning it + wrapper.counter = 0 + wrapper.total_time = 0.0 + wrapper.total_mem = 0 -# Initialize the attributes on the decorator itself -performance.counter = 0 -performance.total_time = 0.0 -performance.total_mem = 0 + return wrapper From 1713ba2f47b293fcd060a7a6820a700ae6038d86 Mon Sep 17 00:00:00 2001 From: sas5188 <148003648+sas5188@users.noreply.github.com> Date: Sun, 17 May 2026 19:46:33 +0300 Subject: [PATCH 6/8] Update decorators_murad_maharramli,py --- Week04/decorators_murad_maharramli,py | 40 ++++++++++++--------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/Week04/decorators_murad_maharramli,py b/Week04/decorators_murad_maharramli,py index 1630e50c..d62c8931 100644 --- a/Week04/decorators_murad_maharramli,py +++ b/Week04/decorators_murad_maharramli,py @@ -4,41 +4,35 @@ from functools import wraps def performance(func): """ - A decorator to measure the performance of a function. - Tracks call count, total time, and peak memory on the wrapper. + A decorator that measures the performance of functions and saves statistics. + Attributes are stored directly on the 'performance' function object to satisfy test assertions. """ @wraps(func) def wrapper(*args, **kwargs): - # 1. Update the counter for this specific function - wrapper.counter += 1 + # Increment the global decorator call counter + performance.counter += 1 - # 2. Safely manage memory tracking so we don't crash pytest - was_tracing = tracemalloc.is_tracing() - if not was_tracing: - tracemalloc.start() - - # 3. Start high-precision timer + # Start tracking memory and time + tracemalloc.start() start_time = time.perf_counter() - # 4. Run the actual function + # Execute the original function result = func(*args, **kwargs) - # 5. Stop timer and read memory + # Stop tracking and calculate consumed resources end_time = time.perf_counter() _, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() - if not was_tracing: - tracemalloc.stop() - - # 6. Add stats to the WRAPPER, not the decorator - wrapper.total_time += (end_time - start_time) - wrapper.total_mem += peak_mem + # Update the global decorator tracking attributes + performance.total_time += (end_time - start_time) + performance.total_mem += peak_mem return result - # 7. Initialize the attributes on the WRAPPER before returning it - wrapper.counter = 0 - wrapper.total_time = 0.0 - wrapper.total_mem = 0 - return wrapper + +# Initialize the required attributes on the decorator itself +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 From 7c018ec417a9da6bfbdfc0d74f43e695153474d1 Mon Sep 17 00:00:00 2001 From: sas5188 <148003648+sas5188@users.noreply.github.com> Date: Sun, 17 May 2026 19:51:14 +0300 Subject: [PATCH 7/8] Update decorators_murad_maharramli,py --- Week04/decorators_murad_maharramli,py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Week04/decorators_murad_maharramli,py b/Week04/decorators_murad_maharramli,py index d62c8931..1e68d2e5 100644 --- a/Week04/decorators_murad_maharramli,py +++ b/Week04/decorators_murad_maharramli,py @@ -5,26 +5,20 @@ from functools import wraps def performance(func): """ A decorator that measures the performance of functions and saves statistics. - Attributes are stored directly on the 'performance' function object to satisfy test assertions. """ @wraps(func) def wrapper(*args, **kwargs): - # Increment the global decorator call counter performance.counter += 1 - # Start tracking memory and time tracemalloc.start() start_time = time.perf_counter() - # Execute the original function result = func(*args, **kwargs) - # Stop tracking and calculate consumed resources end_time = time.perf_counter() _, peak_mem = tracemalloc.get_traced_memory() tracemalloc.stop() - # Update the global decorator tracking attributes performance.total_time += (end_time - start_time) performance.total_mem += peak_mem @@ -32,7 +26,7 @@ def performance(func): return wrapper -# Initialize the required attributes on the decorator itself +# Initialize the required tracking attributes on the decorator performance.counter = 0 performance.total_time = 0.0 performance.total_mem = 0 From 13a14f45e13ab7a3526ab9b49bac0ce188c9af12 Mon Sep 17 00:00:00 2001 From: sas5188 <148003648+sas5188@users.noreply.github.com> Date: Sun, 17 May 2026 19:54:31 +0300 Subject: [PATCH 8/8] Rename decorators_murad_maharramli,py to decorators_murad_maharramli.py --- ...orators_murad_maharramli,py => decorators_murad_maharramli.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Week04/{decorators_murad_maharramli,py => decorators_murad_maharramli.py} (100%) diff --git a/Week04/decorators_murad_maharramli,py b/Week04/decorators_murad_maharramli.py similarity index 100% rename from Week04/decorators_murad_maharramli,py rename to Week04/decorators_murad_maharramli.py