Skip to content

Instantly share code, notes, and snippets.

View andreypopp's full-sized avatar
🏠
Working from home

Andrey Popp andreypopp

🏠
Working from home
View GitHub Profile
class PGArrayField(models.Field):
""" Custom field for mapping between postgresql array and python list."""
def __init__(self, element_field, **kwargs):
self.element_field = element_field
models.Field.__init__(self, **kwargs)
def db_type(self, *connection):
return '%s[]' % self.element_field.db_type(*connection)
class TestModel(models.Model):
f = models.TextField()
a = PGArrayField(models.IntegerField(), null=True)
""" Contract2."""
class ValidationError(TypeError):
pass
class Type(object):
def validate(self, value):
raise NotImplementedError()
""" Validating Python data structures."""
__all__ = (
"ValidationError",
"Type", "Or", "And", "Optional", "NullType", "Nullable", "Null",
"IntType", "StringType", "BoolType",
"Int", "String", "Bool",
"Sequence", "Mapping")
class ValidationError(TypeError):
Prelude> let ngrams n seq@(_:tail) | length seq >= n = (take n seq):(ngrams n tail); ngrams _ _ = []
Prelude> ngrams 1 [1,2,3]
[[1],[2],[3]]
Prelude> ngrams 2 [1,2,3]
[[1,2],[2,3]]
Prelude> ngrams 3 [1,2,3]
[[1,2,3]]
class S[A, B, C] {
def apply[A, B, C](f: A => (B => C), g: A => B, x: A): C =
(f(x))(g(x))
}
mpb % python -m timeit -s 'from peg import expr, parse' "parse(expr, '1+1+3+123-12*(1212+2)')"
1000 loops, best of 3: 231 usec per loop
mpb % pypy -m timeit -s 'from peg import expr, parse' "parse(expr, '1+1+3+123-12*(1212+2)')"
1000 loops, best of 3: 30.6 usec per loop
from peg import *
from collections import namedtuple
Select = namedtuple("Select",
["select_list", "from_clause", "where_clause", "offset_clause", "limit_clause"])
From = namedtuple("From", ["table", "joins"])
_Ref = namedtuple("Ref", ["parts"])
Join = namedtuple("Join", ["table", "condition"])
AllColumns = namedtuple("AllColumns", [])
UsingJoinCond = namedtuple("UsingJoinCond", ["columns"])
@andreypopp
andreypopp / gist:2820220
Created May 28, 2012 17:38
text-to-tags ratio based content extractor
#!/usr/bin/env python
"""usage: bte file
arguments:
file filename or - to read from stdin
options:
-h, --help show this message and exit
"" We don't like plain vi
set nocompatible
filetype off
"" Initialize pathogen
call pathogen#helptags()
call pathogen#runtime_append_all_bundles()
"" Preferences
let mapleader=","