Skip to content

Instantly share code, notes, and snippets.

View gavinwahl's full-sized avatar

Gavin Wahl gavinwahl

  • Fusionbox
  • Colorado
View GitHub Profile
def sa_to_django(model, row, table=None):
"""
Constructs a Django model from a sqlalchemy result row. We could just fetch
the django model by id, but that'd several queries for each row of the
report.
XXX: Does not handle _meta.state.adding (maybe it should?), so don't try to
save the model.
"""
from django.core.cache import cache
class RateLimiter(object):
def __init__(self, timeout, max_failures):
self.timeout = timeout
self.max_failures = max_failures
def get_key(self, t):
return ':'.join(str(s) for s in t)
@gavinwahl
gavinwahl / bulk_object_creator.py
Last active July 19, 2018 21:39
Bulk create your objects, but don't accidentally keep too many in memory
import contextlib
@contextlib.contextmanager
def bulk_object_creator(limit=100):
"""
Allows creation of model objects in bulk, intelligently not using too much memory.
with bulk_object_creator() as foo_creator:
for i in range(100000):
@gavinwahl
gavinwahl / audio.js
Created August 11, 2018 04:25
Audio in React
import React, { Component } from 'react';
import PropTypes from 'prop-types';
const {Provider: AudioContextProvider, Consumer: AudioConsumer} = React.createContext();
class AudioProvider extends Component {
static propTypes = {
children: PropTypes.arrayOf(PropTypes.node).isRequired,
}
@gavinwahl
gavinwahl / main.rs
Created September 24, 2024 19:12
tcp chat server
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::net::TcpListener;
use tokio::sync::mpsc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use slotmap::{DenseSlotMap, DefaultKey};
use tokio::select;