Skip to content

Instantly share code, notes, and snippets.

View BugVanquisher's full-sized avatar
🐢
Learn something new everyday

BugVanquisher BugVanquisher

🐢
Learn something new everyday
View GitHub Profile
@BugVanquisher
BugVanquisher / rate_limiter.py
Created October 2, 2025 05:16
[Python] Rate limiter
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
"""
@BugVanquisher
BugVanquisher / context_manager_for_temp_file_ops.py
Last active October 2, 2025 05:17
[Python] Context manager for temporary file operations
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:
@BugVanquisher
BugVanquisher / batch_processor.py
Created October 1, 2025 16:01
[Python] Batch Processor
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
@BugVanquisher
BugVanquisher / memoization_decorator.py
Created October 1, 2025 15:59
[Python] Memoization / Caching Decorator
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]
@BugVanquisher
BugVanquisher / performance_timer.py
Created October 1, 2025 15:58
[Python] Performance Timer Decorator
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
@BugVanquisher
BugVanquisher / retry_decorator_expo_backoff.py
Created October 1, 2025 15:56
[Python] Retry Decorator with Exponential Backoff
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
@BugVanquisher
BugVanquisher / split_files.py
Last active March 30, 2020 18:01
Split csv files by a specific column
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: