Skip to content

Instantly share code, notes, and snippets.

@deedy5
deedy5 / django_admin_export_csv_stream.py
Created November 18, 2023 22:54
Django admin export - very fast csv streaming
from django.db import models
from django.contrib import admin
from django.http import StreamingHttpResponse
import csv
class ExampleModel(models.Model):
field1 = models.CharField(max_length=100)
field2 = models.CharField(max_length=100)
field3 = models.EmailField(max_length=100)
# ... other fields ...
@deedy5
deedy5 / lrucache.py
Last active May 9, 2023 11:02
LRUCache (capacity + optional thread-safe)
import threading
class LRUCache:
def __init__(self, capacity, concurrent=False):
self._capacity = capacity
self._concurrent = concurrent
self._cache = {}
self._lock = threading.Lock()
@deedy5
deedy5 / df_reduce_memory.py
Created August 28, 2022 11:34
Reduce pandas dataframe memory size
def df_reduce_memory(df):
"""Reduce pandas dataframe memory size
Args:
df (pd.DataFrame): pandas dataframe
Returns:
pd.DataFrame: reduced pandas dataframe
"""
# Example: df = pd.read_csv(data_dir, parse_dates=True, keep_date_col=True)