IntermediateAdvanced PythonPython
Run this module
Advanced Python – Decorators and Generators¶
Overview¶
Decorators and Generators are powerful Python features that separate professional code from beginner scripts. Decorators allow you to modify function behavior cleanly, while Generators enable memory-efficient processing of large financial datasets.
Key Concepts¶
Decorators @wrapper¶
- Function Wrappers: Modify input/output without changing code
- Cross-Cutting Concerns: Logging, timing, authentication, error handling
- Syntax Sugar:
@decoratoris equivalent tofunc = decorator(func) functools.wraps: Preserves original function metadata
Generators yield¶
- Lazy Evaluation: Compute values only when needed
- Memory Efficient: Process terabytes of data with minimal RAM
- Infinite Streams: Model real-time data feeds
- Pipelines: Chain generators for modular data processing
Key Examples¶
Timing Decorator¶
def timer(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
result = func(*args, **kwargs)
print(f"Took {time.time() - start:.4f}s")
return result
return wrapper
@timer
def heavy_calc():
# ...
Simple Generator¶
def price_stream():
price = 100
while True:
price += random.uniform(-1, 1)
yield price
# Usage
stream = price_stream()
print(next(stream)) # 100.5
print(next(stream)) # 99.8
Generator Pipeline¶
raw_data = read_csv_generator("trades.csv")
filtered = (t for t in raw_data if t['symbol'] == 'AAPL')
processed = (process_trade(t) for t in filtered)
for trade in processed:
save_to_db(trade)
Files¶
decorators_generators_tutorial.py: Interactive tutorial
How to Run¶
Financial Applications¶
1. Robust API Calls (Retry Decorator)¶
Automatically retry failed API requests with exponential backoff:
2. Caching (Memoization)¶
Cache expensive calculations (like implied volatility) to speed up backtests:
3. Streaming Backtest (Generators)¶
Process tick data year-by-year without loading everything into RAM:
4. Event-Driven Systems¶
Use coroutines (generators that accept input) to model strategy logic:
Best Practices¶
- Use
yield from: Delegate to sub-generators. - Avoid Side Effects: Decorators should generally be transparent.
- Generator Expressions: Use
(x for x in data)instead of[x for x in data]for large sequences. - Debugging: Decorators can make stack traces harder to read; use
functools.wraps.
Master these advanced features to write professional, scalable, and efficient financial software!
Continue in Advanced Python¶
-
In quantitative finance, speed is edge. Python's
asynciolibrary allows for concurrency, letting your program handle multiple tasks (like fetching data from 10 different exchanges) at once, rather than waiting for one to finish before starting the next. -
Advanced Python - Context Managers
Context Managers are a powerful Python feature for resource management. They allow you to allocate and release resources precisely when you want to. The most common usage is the
withstatement. -
Advanced Python - Error Handling
Robust error handling is what separates a script that crashes overnight from a professional trading system that runs for years. This module teaches you how to anticipate, catch, and manage errors gracefully.
-
Advanced Python - Multiprocessing
Python Global Interpreter Lock prevents multiple threads from executing Python bytecode at the same time. This makes threads useless for intense algorithmic work. The multiprocessing module bypasses the lock entirely by spawning separate operating system processes. Each process has its own Python interpreter and memory space, enabling true parallelism across all processing cores.
-
Object-Oriented Programming (OOP) is essential for building scalable, maintainable trading systems and financial applications. Learn to organize code using classes, objects, and OOP principles.