Skip to content

Instantly share code, notes, and snippets.

@oakfang
oakfang / js.py
Last active April 7, 2023 04:45
How to import js modules into python
import sys
import jiphy # pip install
from os import path
from types import ModuleType
class JsFinder(object):
def find_module(self, name, m_path):
name += '.js'
if m_path is not None:
@oakfang
oakfang / fetchmid.js
Created February 16, 2016 09:49
Redux middleware for simple fetches
const asyncFetchMiddleware = store => next => action => {
let {type, request} = action;
if (type.endsWith('_ASYNC')) {
fetch(request)
.then(response => response.json())
.then(payload => store.dispatch({
type: type.replace(/ASYNC$/, 'SUCCESS'),
payload
}))
.catch(payload => store.dispatch({
@oakfang
oakfang / rux.js
Created March 6, 2016 06:36
Exrtact s State Stream from a redux store
import { BehaviourSubject } from 'rx';
export function getStateStream(store) {
const sub = new BehaviourSubject(store.getState());
store.subscribe(state => sub.toNext(sub));
return sub.asObservable();
}
@oakfang
oakfang / test.py
Last active March 14, 2016 14:35
Using facade.dll to run rust code via python
from facade import dll
import utils
print utils.multi_sum(3)
@oakfang
oakfang / hashtags.js
Created March 16, 2016 14:20
Extract unicode hashtags from text
'use strict';
const twitter = require('twitter-text');
function extractTags(text) {
const tags = twitter.extractHashtagsWithIndices(text);
let buffer = '';
let i = 0;
for (let t of tags) {
buffer += text.substring(i, t.indices[0]);
@oakfang
oakfang / main.py
Last active April 3, 2016 07:13
quest main server
from journey import System, import_all_hooks
from journey.sources import tcp_server_source
import_all_hooks()
sys = System(tcp_server_source('', 3030))
data = sys.run()
print(data['statistics'])
@oakfang
oakfang / xain-el.js
Last active May 1, 2016 13:08
Use Xain for elements
'use strict';
const {reactive, link, pipe, observable, observe} = require('xain');
function Xain(state) {
class _Xain {
constructor(props={}) {
const reaction = this.constructor.reaction || (() => ({}));
this.props = reactive(Object.assign({}, reaction(state), props));
observe(this.props, this.__renderToScreen.bind(this));
@oakfang
oakfang / util.py
Created May 4, 2016 07:09
Add an unbound method to a class (Python 2)
from types import MethodType
LAMBDA_NAME = '<lambda>'
def extend(cls, method, name=None):
method_name = method.__name__
if method_name == LAMBDA_NAME and name is None:
raise NameError('Please provide a usable name for a lambda method')
elif name is not None:
method_name = name
@oakfang
oakfang / tact.py
Last active May 4, 2016 12:11
Using python 3.5 typing module and some magic to enforce actual and semantic types
import typing
import functools
from abc import ABCMeta
from inspect import signature, getcallargs
class InterfaceMeta(ABCMeta):
def __new__(cls, name, bases, namespace, **kwargs):
if bases:
namespace['__specs__'] = {name: signature(namespace[name]) for name in namespace if name not in ('__qualname__', '__module__')}
@oakfang
oakfang / gn.sh
Created June 13, 2016 14:39
One command to rule them all.
# Source this from your .bashrc/.zshrc
GIT_CORE="$(git --exec-path)"
gn() {
command=$1
shift
if [ -f "${GIT_CORE}/git-${command}" ]; then
git "$command" "$@"
else