Practical Applications
Large Dataset Processing
def csv_reader(file_path):
for row in open(file_path, 'r'):
yield row.strip().split(',')
## Memory-efficient CSV processing
def process_large_csv(file_path):
for row in csv_reader(file_path):
## Process each row without loading entire file
yield row[0], row[1]
Infinite Sequence Generation
def infinite_counter(start=0):
while True:
yield start
start += 1
## Controlled infinite sequence
counter = infinite_counter()
limited_values = [next(counter) for _ in range(5)]
Data Pipeline Construction
graph LR
A[Raw Data] --> B[Generator 1]
B --> C[Generator 2]
C --> D[Generator 3]
D --> E[Final Output]
Stream Processing Example
def data_transformer(data):
for item in data:
yield item.upper()
def data_filter(data):
for item in data:
if len(item) > 3:
yield item
## Chained generators
raw_data = ['apple', 'banana', 'cat', 'dog']
processed = list(data_filter(data_transformer(raw_data)))
Approach |
Memory Usage |
Processing Speed |
List Comprehension |
High |
Fast |
Generator |
Low |
Slower |
Generator Pipeline |
Optimal |
Moderate |
Async Data Processing
async def async_generator():
for i in range(10):
await asyncio.sleep(0.1)
yield i
async def main():
async for value in async_generator():
print(value)
Real-world Scenarios
- Log File Analysis
- Network Stream Processing
- Machine Learning Data Loading
- Configuration Management
Memory Profiling
import sys
def memory_efficient_range(n):
for i in range(n):
yield i
## Compare memory usage
list_memory = sys.getsizeof([x for x in range(1000000)])
gen_memory = sys.getsizeof(range(1000000))
At LabEx, we recommend mastering generator applications for scalable Python programming.