Skip to content

Instantly share code, notes, and snippets.

@un-def
un-def / pytest_inline_fixtures.py
Last active August 1, 2018 17:34
pytest inline fixtures
from inspect import isfunction, signature, Parameter
import pytest
def make_fixture(value, scope='function'):
if isfunction(value):
sig = signature(value)
if 'self' in sig.parameters:
fixture = lambda self, **fxs: value(self, **fxs) # noqa: E731
@un-def
un-def / httpaio.py
Created July 25, 2018 18:41
dead simple asyncio http client
from http.client import HTTPResponse
import urllib.parse
import asyncio
import json
import sys
import io
class FakeSocket:
@un-def
un-def / get_basename.lua
Created March 29, 2018 18:15
get_basename lua snippet
local get_basename = function(path)
return path:match('/([^/]*)$') or path
end
@un-def
un-def / proxyMagic.js
Created February 15, 2018 21:04
proxyMagic
function doMagic(obj) {
return new Proxy(obj, {
construct: (target, args) => {
return doMagic(new target(...args))
},
get: (target, name) => {
let prop = Reflect.get(target, name)
if (typeof prop === 'function') {
return prop.bind(target)
}
@un-def
un-def / hm.py
Created December 2, 2017 20:14
Ugly Python-based XML/HTML DSL
#!/usr/bin/env python3
from abc import ABC, ABCMeta, abstractmethod
from types import FunctionType
from collections import OrderedDict
# flake8: noqa
class UnresolvedFreeVarsError(Exception):
@un-def
un-def / i3-window-icons-deb.sh
Created June 10, 2017 13:11
Tool to create (and optionally install) Debian packages of i3 with window icons patch
#!/bin/bash -e
# Tool to create (and optionally install) Debian packages of i3 with
# window icons patch.
# Based on i3-gaps-deb by Gerhard A. Dittes
# Copyright (C) 2017 Gerhard A. Dittes
#
# This program is free software: you can redistribute it and/or modify
@un-def
un-def / base_site.html
Created January 29, 2016 15:42
Django admin site lang switcher
<!-- {templates_dir}/admin/base_site.html -->
{% extends "admin/base.html" %}
{% load i18n %}
{% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1>
{% endblock %}
@un-def
un-def / zip_trim.py
Created January 9, 2016 06:29
zip with trim/fill
def zip_trim(i1, i2, trim='shortest', default=None):
''' trim = 'shortest' | 'longest' | 'first' | 'second'
'''
i1_iter = iter(i1)
i2_iter = iter(i2)
while True:
stop = False
try:
e1 = next(i1_iter)
except StopIteration:
@un-def
un-def / instancelinkwidget.py
Last active November 23, 2015 08:38
Django InstanceLinkWidget
from django import forms
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse
class InstanceLinkWidget(forms.Widget):
def __init__(self, obj, attrs=None):
self.obj = obj
super(InstanceLinkWidget, self).__init__(attrs)
@un-def
un-def / uniq_mixin.py
Created September 12, 2015 10:51
`uniq` method mixin
class UniqMixin:
def uniq(self):
uniq_list = []
for el in self:
if el not in uniq_list:
uniq_list.append(el)
return self.__class__(uniq_list)