Skip to content

Instantly share code, notes, and snippets.

@night-crawler
night-crawler / RouteLink.kt
Created February 25, 2020 10:51
Importing react-router RouterContext in Kotlin/JS
@night-crawler
night-crawler / kotlinReduxCustomEnhancer.kt
Last active February 18, 2020 19:48
Fixed version of rEnhancer function
fun <S> customEnhancer(): Enhancer<S, Action, Action, RAction, WrapperAction> = { next ->
{ reducer, initialState ->
fun wrapperReducer(reducer: Reducer<S, RAction>): Reducer<S, WrapperAction> = { state, action ->
if (!action.asDynamic().isKotlin as Boolean) {
reducer(state, action.asDynamic().unsafeCast<RAction>())
} else {
reducer(state, action.action)
}
}
@night-crawler
night-crawler / build.gradle.kts
Created February 15, 2020 23:23
Sufficient Kotlin/JS compilation settings
kotlin {
target {
browser {
compilations.all {
kotlinOptions {
friendModulesDisabled = false
metaInfo = true
sourceMap = true
sourceMapEmbedSources = "always"
moduleKind = "commonjs"
@night-crawler
night-crawler / data_validators.py
Last active February 14, 2017 09:33
Validation voluptuous example
from . import exceptions
from voluptuous import Schema
from voluptuous import MultipleInvalid
from voluptuous import Required, All, Length, Range, Coerce
# https://www.postgresql.org/docs/9.1/static/datatype-numeric.html
PG_MAX_BIG_INT = 9223372036854775807
PG_MAX_BIG_INT_LENGTH = 20
PG_INT_RANGE = Range(min=1, max=PG_MAX_BIG_INT)
# -*- coding: utf-8 -*-
import asyncio
import aiozmq.rpc
@asyncio.coroutine
def get_rpc_connection(connect='', loop=None):
timeout = 1
connection = yield from aiozmq.rpc.connect_rpc(connect=connect, timeout=timeout, loop=loop)
return connection
@night-crawler
night-crawler / floppyforms_widgets.py
Created October 30, 2013 07:58
A small gist to change all django's default formfield widgets to floppyforms.widgets via widgets=floppyforms_widgets(YourModel) in ModelForm's Meta.
from django.forms.models import fields_for_model
import floppyforms
def widgets_for_model(model):
return {k: v.widget.__class__.__name__ for k, v in fields_for_model(model).items()}
def floppyforms_widgets(model):
return {field: getattr(floppyforms, widget) for field, widget in widgets_for_model(model).items()}
@night-crawler
night-crawler / optional_args.py
Created September 8, 2013 11:22
Optional arguments in django urls.conf
# urls.py
url(r'^editor/instance/'
r'(?:forum/(?P<forum_id>\d+)/)?'
r'(?:topic/(?P<topic_id>\d+)/)?'
r'(?:post/(?P<post_id>\d+)/)?$', 'forum.views.mked_post', name='mked_post'),
# views.py
def mked_post(request, **kwargs):
return HttpPreResponse(
@night-crawler
night-crawler / imvi.py
Created September 7, 2013 13:54
A small tool for making video from images present by dlink cam.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import glob
import shutil
import subprocess
import os
IMG_PATTERN = '/cam/*/*/*.jpg'
VID_PATH = '/cam/video'
TMP_PATH = '/tmp/frame_symlinks'
@night-crawler
night-crawler / telnet_websocket_proxy.py
Created August 12, 2013 18:26
A simple telnet-websocket proxy on python using Tornado. Tested with "Sphere of Worlds MUD". Replaces IAC and GA.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
from tornado.web import RequestHandler, Application
from tornado.websocket import WebSocketHandler
from tornado.ioloop import IOLoop
import socket
import threading
import Queue
@night-crawler
night-crawler / last_visited.py
Created August 9, 2013 13:50
Save last visited pages with title and last access time in Django templates. Gist provides two methods: "save_visited" and "load_visited"
# -*- coding: utf-8 -*-
from django import template
from django.utils.encoding import force_unicode
from django.utils import timezone
from django.core.cache import cache
# import datetime
# from django.conf import settings
# from django.utils.formats import sanitize_separators
# from django.utils.html import conditional_escape as esc