Skip to content

Instantly share code, notes, and snippets.

import functools
class Dispatcher:
def __init__(self, strict=False):
self.strict = strict
self.__path = []
def __getitem__(self, name):
def getitem(obj):
### current bpython session - make changes and save to reevaluate session.
### lines beginning with ### will be ignored.
### To return to bpython without reevaluating make no changes to this file
### or save an empty file.
class Int(int):
def __call__(self, val):
return Int(self + val)
num = Int(1)
def replace_with_func_name(func):
return func.__name__
@replace_with_func_name
def func(x, y):
return x / y
func
### 'func'
@regqueryvalueex
regqueryvalueex / alternative_constructors.py
Created August 27, 2019 10:38
python alternative constructors
class AlternativeConstructors():
def __init__(self, value):
self.value = value
@classmethod
def from_reversed(cls, value):
return cls(value[::-1])
@classmethod
@regqueryvalueex
regqueryvalueex / coro_without_eventloop.py
Created August 23, 2019 06:31
Run async function without event_loop
async def f1(x):
return x*x
async def f2(x, y):
return await f1(x) + await f1(y)
next(f2(3, 4).__await__())
### Traceback (most recent call last):
### File "<input>", line 1, in <module>
### next(f2(3, 4).__await__())
@regqueryvalueex
regqueryvalueex / measure_exectime.py
Last active August 2, 2017 09:13
Context manager for measuring execution time of the piece of code
import logging
import random
import string
import time
from contextlib import contextmanager
@contextmanager
def measure_exectime(name, extra_context=None):
"""
@regqueryvalueex
regqueryvalueex / fabfile.py
Created June 10, 2016 05:13 — forked from fiee/fabfile.py
fabric fabfile.py for deployment of django apps on Debian servers
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
fabfile for Django
------------------
see http://morethanseven.net/2009/07/27/fabric-django-git-apache-mod_wsgi-virtualenv-and-p/
modified for fabric 0.9/1.0 by Hraban (fiëé visuëlle)
several additions, corrections and customizations, too
@regqueryvalueex
regqueryvalueex / psql_permissions_fix.sh
Created November 11, 2015 13:10
fix permissions problem in postgres
psql database -c "GRANT ALL ON ALL TABLES IN SCHEMA public to user;"
psql database -c "GRANT ALL ON ALL SEQUENCES IN SCHEMA public to user;"
psql database -c "GRANT ALL ON ALL FUNCTIONS IN SCHEMA public to user;"
from django.db.models.query import QuerySet
from pprint import PrettyPrinter
def dprint(object, stream=None, indent=1, width=80, depth=None):
"""
A small addition to pprint that converts any Django model objects to dictionaries so they print prettier.
h3. Example usage
>>> from toolbox.dprint import dprint
@regqueryvalueex
regqueryvalueex / FormData example
Created August 20, 2015 07:19
send files with jquery.ajax
var formdata = new FormData();
for(var i=0; i<fileInput.files.length; i++){
formdata.append('files', fileInput.files.item(i));
}
$.ajax({
url: '/some-url/',
type: 'POST',
data: formdata,
processData: false,