This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split_files(df): | |
''' | |
Split files by TICKER. If the corresponding csv file doesn't exist, create one; | |
otherwise append new data to current csv file | |
:param df: dataframe | |
:return: None | |
''' | |
grouped = df.groupby('TICKER') | |
# loop through the grouped dataframe | |
for ticker, group in grouped: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import functools | |
def retry(max_attempts=3, delay=1, backoff=2, exceptions=(Exception,)): | |
""" | |
Retry decorator with exponential backoff | |
Args: | |
max_attempts: Maximum number of retry attempts | |
delay: Initial delay between retries in seconds |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def timer(func): | |
"""Decorator to measure function execution time""" | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
start = time.perf_counter() | |
result = func(*args, **kwargs) | |
end = time.perf_counter() | |
print(f"{func.__name__} took {end - start:.4f} seconds") | |
return result | |
return wrapper |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def memoize(func): | |
"""Simple memoization decorator for functions with hashable arguments""" | |
cache = {} | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
key = (args, tuple(sorted(kwargs.items()))) | |
if key not in cache: | |
cache[key] = func(*args, **kwargs) | |
return cache[key] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def batch_process(items, batch_size=100): | |
""" | |
Process items in batches using a generator | |
Args: | |
items: Iterable of items to process | |
batch_size: Number of items per batch | |
Yields: | |
Lists of items with length up to batch_size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import tempfile | |
from contextlib import contextmanager | |
@contextmanager | |
def temporary_file(suffix='', prefix='tmp', dir=None): | |
""" | |
Context manager for creating and automatically cleaning up temporary files | |
Usage: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
class RateLimiter: | |
""" | |
Simple rate limiter using token bucket algorithm | |
Args: | |
max_calls: Maximum number of calls allowed | |
time_window: Time window in seconds | |
""" |