Skip to content

Instantly share code, notes, and snippets.

Avatar

BBB yxy

  • gggg
View GitHub Profile
@yxy
yxy / sample_usage.py
Created Jun 24, 2016 — forked from luke14free/sample_usage.py
Simple type checked objects in Python
View sample_usage.py
#!/usr/bin/env python
from type_checked_entities import entity_factory
Giraffe = entity_factory( # let's define what is a giraffe!
"giraffe",
name=str, # my name is a string
age=float, # my age is an int
eats=object, # I eat pretty much everything.
)
View multimethod.py
"""
A mutlimethod decorator, dispatch based on the input types.
"""
registry = {}
def multimethod(*types):
def wrapper(fn):
# registry key=>func
_types = tuple(t for t in types)
registry[_types] = fn
@yxy
yxy / flatten.js
Last active May 13, 2016
flatten array snippet
View flatten.js
flatten = function (a, b) {
if (!Array.isArray(a)) {
a = [a];
}
return a.concat(b);
}
// usage
var aa = [[1], [2, 3], [4, 5]];
View aserver.py
# server.py
# fib microservice
from socket import *
# from threading import Thread
from collections import deque
from select import select
from concurrent.futures import ThreadPoolExecutor as Pool
from concurrent.futures import ProcessPoolExecutor as Pool
@yxy
yxy / wc.l
Created Feb 29, 2016
unix tool wc written in lex
View wc.l
%{
int nchar, nword, nline;
%}
%%
\n { nline++; nchar++ ;}
[^ \t\n]+ { nword++; nchar += yyleng; }
. { nchar++; }
%%
View scanner.py
#-*- encoding: utf8 -*-
from sre_parse import Pattern, SubPattern, parse
from sre_compile import compile as sre_compile
from sre_constants import BRANCH, SUBPATTERN
class Scanner(object):
def __init__(self, rules, flags=0):
@yxy
yxy / simple_thread_pool.py
Created Oct 29, 2015
simple thread pool workers
View simple_thread_pool.py
#-*- encoding: utf8 -*-
import pickle
import sys
from threading import Thread
PY2 = sys.version_info[0] == 2
if PY2:
from Queue import Queue
@yxy
yxy / SimpleCaptcha.py
Created Oct 9, 2015
A simple captcha generator using PIL
View SimpleCaptcha.py
#-*- encoding: utf8 -*-
from PIL import Image, ImageDraw, ImageFont
import random
import string
class SimpleCaptchaException(Exception):
pass
@yxy
yxy / hstruct.py
Created Apr 28, 2015
This recipe provides a higher level wrapper around the struct module.
View hstruct.py
# encoding: utf-8
"""
This recipe provides a higher level wrapper around the struct module.
It provides a more convenient syntax for defining and using structs,
and adds additional features such as:
- Allows embedding structures within other structures
- Allows defining arrays of items (or other structures)
- Class based syntax, allowing access and updates by field name, not position
@yxy
yxy / 0_reuse_code.js
Last active Aug 29, 2015
Here are some things you can do with Gists in GistBox.
View 0_reuse_code.js
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console