Skip to content

Instantly share code, notes, and snippets.

export function throttleDebouncePromise(
inner,
{ throttle, debounce, cache, immediateIfCached },
) {
// Ordinary debounce functions cause issues with promises and react-select but this works well.
// Based on: https://stackoverflow.com/questions/35228052/debounce-function-implemented-with-promises
const resolveCallbacks = []
const store = {}
let timeout = null
let time = new Date()
@balazs-endresz
balazs-endresz / storage.py
Last active August 20, 2018 22:22
ManifestStaticFilesStorage with source maps, and ignoring missing files
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
class ManifestStaticFilesStorageWithSourceMaps(ManifestStaticFilesStorage):
"""
Adds js and css source map support for ManifestStaticFilesStorage.
"""
patterns = (
("*.css", (
@balazs-endresz
balazs-endresz / .py
Created March 21, 2018 20:44
Constant memory streaming CSV writer (works with prefetch_related too)
def generator_from_queryset(queryset, chunksize=2000, enumerate=False):
assert queryset.query.order_by is not None
offset = 0
i = 0
finished = False
while not finished:
finished = True
for row in queryset[offset:offset + chunksize]:
finished = False
if enumerate:
@balazs-endresz
balazs-endresz / .py
Created September 26, 2017 14:23
cast and round django database functions
from django.db.models import ExpressionWrapper, FloatField, Func
class Cast(Func):
function = 'CAST'
template = '%(function)s(%(expressions)s AS %(db_type)s)'
def __init__(self, expression, db_type):
# convert second positional argument to kwarg to be used in function template
super(Cast, self).__init__(expression, db_type=db_type)
@balazs-endresz
balazs-endresz / slack-hide-status-bar.user.js
Last active December 15, 2016 12:05
Hide status bar on Slack
// ==UserScript==
// @name Hide status bar on Slack
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://*.slack.com/*
// @grant none
// ==/UserScript==
@balazs-endresz
balazs-endresz / models.py
Last active April 3, 2024 22:58
analyze explain django queryset
from django.db import connections
from django.db.models.query import QuerySet
def explain(self):
cursor = connections[self.db].cursor()
cursor.execute('set enable_seqscan = off')
query, params = self.query.sql_with_params()
print(query % params)
cursor.execute('EXPLAIN ANALYZE %s' % query, params)
@balazs-endresz
balazs-endresz / .sh
Created September 8, 2016 14:36
send email if new entry is made to kern.log
#!/bin/bash
# http://serverfault.com/questions/24428/monitoring-dmesg-output
MAILTO=root
LOG=/var/log/kern.log
OFFSET_FILE=$0.offset
@balazs-endresz
balazs-endresz / .py
Created September 6, 2016 14:58
Decorator to help updating the appropriate wagtail page and/or revision objects
from functools import wraps
def update_page_and_or_revsion(func):
@wraps(func)
def func_wrapper(page):
if page.live:
if page.has_unpublished_changes:
# update live page without creating new revsions
func(page)
page.save()
@balazs-endresz
balazs-endresz / .sh
Created May 16, 2016 14:32
Find local css/js includes that don't use the static tag
find templates/ -name *.html -exec sh -c "cat {}|grep '<script' | grep -v '{% static' | grep -v 'src=\"//' | grep -v '<script>' | grep -v 'src=\"http'" \; -print
find templates/ -name *.html -exec sh -c "cat {}|grep 'stylesheet' | grep -v '{% static' | grep -v 'href=\"//' | grep -v 'href=\"http' | grep -v -P 'href=\047//' | grep -v -P 'href=\047http'" \; -print
@balazs-endresz
balazs-endresz / all_versions.py
Last active April 13, 2016 21:04
Figure out what verions of python packages are compatible with each other
import sys
import json
import urllib2
from distutils.version import StrictVersion
def versions(package_name):
url = "https://pypi.python.org/pypi/%s/json" % (package_name,)
data = json.load(urllib2.urlopen(urllib2.Request(url)))
versions = data["releases"].keys()
try: