-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
32 lines (28 loc) · 1006 Bytes
/
Copy pathmain.py
File metadata and controls
32 lines (28 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import time
from contextlib import contextmanager
# Context Manager for database connection handling
@contextmanager
def database_connection():
print("Connecting to database...")
time.sleep(1) # Simulate connection delay
yield "DB Connection Object" # Mock database connection object
print("Closing database connection...") # Simulate disconnection
# Decorator for logging query execution time
def log_query(func):
def wrapper(*args, **kwargs):
print(f"Executing query: {func.__name__}")
start = time.time()
result = func(*args, **kwargs)
end = time.time()
print(f"Query executed in {end - start:.4f} seconds")
return result
return wrapper
# Example usage
with database_connection() as conn:
@log_query
def fetch_users(connection):
time.sleep(2) # Simulate query delay
print(f"Fetching users from {connection}")
return ["User1", "User2", "User3"]
users = fetch_users(conn)
print(users)