Skip to content

Instantly share code, notes, and snippets.

@gvx
gvx / testveri.py
Last active December 11, 2015 17:49
An experiment with metaclasses, with dynamic type verification
from veri import Verified
class Something(metaclass=Verified):
# a single type
some_int = int
some_str = str
# a 'struct' using tuples
some_tuple = (int, int, str)
# a list of integers
@gvx
gvx / gist:3954076
Created October 25, 2012 17:08
Daily Programmer #107 difficult
import sys
vowels = set('aeiouy')
consonants = set('bcdfghjklmnpqrstvwxz')
impossible_digraphs = ['aa', 'bg', 'bh', 'bk', 'bq', 'bw', 'bx', 'bz', 'cb', 'cf', 'cg', 'cj', 'cm', 'cp', 'cv', 'cw', 'cx', 'cz', 'dx', 'dz', 'fc', 'fd', 'fg', 'fj', 'fk', 'fn', 'fp', 'fq', 'fv', 'fw', 'fx', 'fz', 'gc', 'gj', 'gk', 'gq', 'gv', 'gx', 'gz', 'hj', 'hk', 'hq', 'hv', 'hx', 'hz', 'ij', 'iy', 'jb', 'jc', 'jd', 'jf', 'jg', 'jh', 'jj', 'jk', 'jl', 'jm', 'jn', 'jp', 'jq', 'jr', 'js', 'jt', 'jv', 'jw', 'jx', 'jy', 'jz', 'kq', 'kv', 'kx', 'kz', 'lj', 'lx', 'md', 'mj', 'mk', 'mq', 'mx', 'mz', 'pg', 'pj', 'pq', 'pv', 'px', 'pz', 'qa', 'qb', 'qc', 'qd', 'qe', 'qf', 'qg', 'qh', 'qi', 'qj', 'qk', 'ql', 'qm', 'qn', 'qo', 'qp', 'qq', 'qr', 'qs', 'qt', 'qv', 'qw', 'qx', 'qy', 'qz', 'rx', 'sx', 'sz', 'tq', 'tx', 'uj', 'uq', 'uw', 'uy', 'vb', 'vc', 'vd', 'vf', 'vg', 'vh', 'vj', 'vk', 'vl', 'vm', 'vn', 'vp', 'vq', 'vr', 'vs', 'vt', 'vv', 'vw', 'vx', 'vz', 'wj', 'wk', 'wp', 'wq', 'wv', 'ww', 'wx', 'wz', 'xb', 'xf', 'xg', 'xj', 'xk', 'xm',
@gvx
gvx / units.l
Created October 15, 2012 17:56
Thingy to learn how to use Flex and Bison
%{
#include "units.tab.c"
%}
%option noyywrap
int -?[0-9]+
%%
[ \t] /*empty*/
@gvx
gvx / listref.py
Created October 3, 2012 22:28
Simplistic demonstration of Python GC behaviour
import gc
import weakref
class Object(object): pass
l = [Object()]
l[0].l = l
REFS = weakref.WeakKeyDictionary()
REFS[l[0]] = True
@gvx
gvx / stop.c
Created September 19, 2012 23:01
A small program that goes to sleep right away and when you wake it, goes right back to sleep.
#include <signal.h>
void stop_self()
{
raise(SIGSTOP);
}
int main()
{
struct sigaction act;
@gvx
gvx / bf2c.py
Created May 25, 2012 07:39 — forked from jdp/bf2c.py
Naive brainfuck-to-C transpiler
#!/usr/bin/env python
import argparse
import sys
def tokenize(source):
return list(source)
def parse(tokens):
@gvx
gvx / gist:2277797
Created April 1, 2012 19:06
Metatable abuse
-- kilobyte (and megabyte) constants
-- string like methods
-- implicit multiplication
-- successor function
debug.setmetatable(0, { __index = function(n, k)
if k == 'K' then
return n * 1024
elseif k == 'M' then
return n * 1024 * 1024
else
@gvx
gvx / formula.py
Created March 24, 2012 17:20
Propositional logic library for Python 3, that can valuate an expression and check whether it is a tautology
"""
formula.py - propositional formulas for Python
by Robin Wellner
Hereby, I waive all rights to this library, as described at <http://creativecommons.org/publicdomain/zero/1.0/>
Examples:
foo = Atom('foo')
bar = Atom('bar')
@gvx
gvx / from.lua
Created July 20, 2011 13:20
Pythonesk "from ... import" in Lua, copying parts of a library into the global table
-- CC0, see http://creativecommons.org/publicdomain/zero/1.0/ etcetera, etcetera
function from(source, which)
local t = require(source)
local function import (which)
if which == '*' then
for k, v in pairs(t) do
if _G[k] == nil then
_G[k] = v
end
@gvx
gvx / aliasdict.py
Created June 14, 2011 17:48
Path to Philosophy code
class Alias(object):
def __init__(self, initial):
self._set = {initial}
self.initial = initial
def add(self, alias):
self._set.add(alias)
def merge(self, other):
self._set.update(other._set)
def __iter__(self):
return iter(self._set)