Skip to content

Instantly share code, notes, and snippets.

@mvasilkov
mvasilkov / gfm.py
Created November 22, 2010 21:11 — forked from christian-oudard/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 November 20, 2011 05:14
Tail recursive power function
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 January 19, 2012 22:16 — forked from Ky6uk/i2p.pl
postfix to infix
#!/usr/bin/env perl
use 5.01;
do { say "USAGE: $0 <postfix.txt>"; exit 0 }
unless $ARGV[0];
# --------------- main -----------------
my (@data, $char, $result, @stack);
#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 January 27, 2012 12:31
RestfulView from libanimuchan
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)
#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);
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 February 9, 2012 03:25
Return first non-repeating (unique) symbol in the string. With tests and timeit.
#!/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 February 14, 2012 15:39
Single-malloc char** allocation.
#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 19:00
Ricchan (lit.animuchan.net) uWSGI config
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 \