Skip to content

Instantly share code, notes, and snippets.

View gfm.py
import re
from hashlib import md5
def gfm(text):
# Extract pre blocks.
extractions = {}
def pre_extraction_callback(matchobj):
digest = md5(matchobj.group(0)).hexdigest()
extractions[digest] = matchobj.group(0)
return "{gfm-extraction-%s}" % digest
@mvasilkov
mvasilkov / power.py
Created Nov 20, 2011
Tail recursive power function
View power.py
from functools import partial, wraps
def power_tail(x, a, n):
assert n >= 0
if n == 0:
return x
elif n & 1:
return partial(power_tail, x * a, a, n - 1)
else:
return partial(power_tail, x, a * a, n >> 1)
@mvasilkov
mvasilkov / p2i.pl
Created Jan 19, 2012 — forked from Ky6uk/i2p.pl
postfix to infix
View p2i.pl
#!/usr/bin/env perl
use 5.01;
do { say "USAGE: $0 <postfix.txt>"; exit 0 }
unless $ARGV[0];
# --------------- main -----------------
my (@data, $char, $result, @stack);
View generate_infix.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define OPEN_BRACKET_STATE 1
#define CLOSE_BRACKET_STATE 2
#define NUMBER_STATE 3
#define OPERATOR_STATE 4
char OPERATORS[] = {'/', '*', '-', '+'};
@mvasilkov
mvasilkov / restful.py
Created Jan 27, 2012
RestfulView from libanimuchan
View restful.py
from django.shortcuts import render
class RestfulView(object):
allowed_methods = ["GET", "POST"]
def __call__(self, request, *args, **kwargs):
if request.method not in self.allowed_methods or not hasattr(self, request.method):
return self.method_not_allowed(request)
return getattr(self, request.method)(request, *args, **kwargs)
View locale.c
#include <locale.h>
#include <stdio.h>
#include <wchar.h>
int main() {
wchar_t name[10];
wcscpy(name, L"жажа");
setlocale(LC_CTYPE, "en_US.UTF-8");
wprintf(L"%S\n", name);
View hell.cxx
template< class, class >
class Permutation;
template< class OUT_TYPE, class... IN_TYPES, uint32_t... INDEXES >
class Permutation<
std::function< OUT_TYPE (IN_TYPES...) >, // тип целевой функции
UintContainer< INDEXES... > > // перестановка аргументов
{
public:
typedef std::function< OUT_TYPE (IN_TYPES...) > FuncType;
typedef std::function< OUT_TYPE (typename GetNthType< INDEXES, TypeContainer<IN_TYPES...> >::Result...) > NewFuncType;
@mvasilkov
mvasilkov / first_uniq.py
Created Feb 9, 2012
Return first non-repeating (unique) symbol in the string. With tests and timeit.
View first_uniq.py
#!/usr/bin/env python
import re
from timeit import timeit
import unittest
UNIQ_RE = re.compile(r'(.)(?=.*?\1)')
TESTS = {
'test': 'e',
'plperl': 'e',
@mvasilkov
mvasilkov / foo.c
Created Feb 14, 2012
Single-malloc char** allocation.
View foo.c
#include <stdio.h>
#include <stdlib.h>
#define N (3)
const size_t offset = sizeof(char *) * N,
length = (sizeof(char *) + sizeof(char) * N) * N;
int main(int argc, char **argv) {
char **foo = (char **)malloc(length);
@mvasilkov
mvasilkov / uwsgi.conf
Created May 19, 2012
Ricchan (lit.animuchan.net) uWSGI config
View uwsgi.conf
description "uWSGI starter {ricchan}"
start on (local-filesystems
and runlevel [2345])
stop on runlevel [016]
respawn
exec /usr/local/bin/uwsgi --uid rei \
-s 127.0.0.1:9001 -M -p 2 \