Skip to content

Instantly share code, notes, and snippets.

View everilae's full-sized avatar

Ilja Everilä everilae

View GitHub Profile
@everilae
everilae / mpy.py
Last active August 27, 2020 07:51
Magic Python, for fun and 0 profit
import ast
_FILENAME = "<undefined>"
def magic(source, filename=_FILENAME):
a = ast.parse(source)
return Magic(filename).visit(a)
@everilae
everilae / division.py
Created March 27, 2018 06:42
Relational division for SQLAlchemy, inspired by jOOQ.
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql.elements import ClauseElement
from sqlalchemy.sql import util as sqlutil
from sqlalchemy.sql.base import Executable, _generative
from sqlalchemy import select, exists, and_
class Divide(Executable, ClauseElement):
def __init__(self, dividend, divisor, on=None, returning=None):
@everilae
everilae / sqlite_json.py
Last active April 4, 2020 02:24
Hacky JSON support for SQLAlchemy SQLite dialect, if https://www.sqlite.org/json1.html extension is available.
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.dialects import plugins
from sqlalchemy.dialects.mysql.json import JSONIndexType, JSONPathType
from sqlalchemy.engine import CreateEnginePlugin
from sqlalchemy.sql.elements import BinaryExpression
from sqlalchemy.sql import sqltypes
from sqlalchemy.sql.operators import \
json_getitem_op, \
json_path_getitem_op
@everilae
everilae / argsfor.py
Created July 28, 2015 12:38
Create argparse.ArgumentParser for given function and apply parsed arguments
import inspect
import argparse
def _helpfor(param):
if param.annotation is not inspect.Parameter.empty:
if isinstance(param.annotation, type):
return param.annotation.__name__
return str(param.annotation)
return param.name
@everilae
everilae / selectinto.py
Created May 25, 2015 15:19
SQLAlchemy SelectInto ORM query support thingie idea test
from sqlalchemy.sql.selectable import HasPrefixes
from sqlalchemy.sql.base import Executable
from sqlalchemy.sql.elements import ClauseElement
from sqlalchemy.ext.compiler import compiles
from sqlalchemy import Table, MetaData
class SelectInto(HasPrefixes, Executable, ClauseElement):
def __init__(self,
@everilae
everilae / mq.erl
Last active August 29, 2015 14:19
Learning Erlang, simple task queue
-module(mq).
-export([
start/0, disp_status/1, get_status/1, add_task/2, add_simple_worker/2,
add_worker/2, send_ack/1, echo_worker/0
]).
-spec start() -> pid().
%% @doc Spawn new queue and return process id.
start() ->
spawn(fun loop/0).
@everilae
everilae / ctris.c
Created April 2, 2015 12:55
tcc -run ctris.c | aplay -r8000
static const char m[] = {
24,24,18,19,
21,21,19,18,
16,16,16,19,
24,24,21,19,
18,18,18,19,
21,21,24,24,
19,19,16,16,
0,0,0,0,
@everilae
everilae / jsoncolumn.py
Created March 27, 2015 10:01
JsonColumn for working with SQLAlchemy JSON and JSONB columns
class JsonColumn(object):
"""
Descriptor class for accessing values from `JSONB` columns.
**In Expressions**:
As class attribute the descriptor resolves the class property
pointing to prepared `JSONB` element using the ``'column'`` value's
:attr:`key` attribute. It then uses the element's item accessor with the
``*keys`` and returns it :attr:`astext`, if not ``'astext'`` is True.
@everilae
everilae / custom.py
Last active August 29, 2015 14:03
An attempt at aggregate FILTER clauses for SQLAlchemy
import itertools
from sqlalchemy import util, and_, case
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.sql import ColumnElement
from sqlalchemy.sql.elements import ClauseList, _clone
from sqlalchemy.sql.functions import FunctionElement
class AggregateFilter(ColumnElement):
"""Represent a FILTER clause.
@everilae
everilae / gist:76e584e611ae9ffec2ea
Created May 16, 2014 11:51
PostgreSQL PL/pgSQL array_foreach
create function array_foreach(text, anyarray) returns anyarray as $$
declare
funcname alias for $1;
vals alias for $2;
results alias for $0;
begin
execute format('select array_agg(s.t) from (select %I(unnest($1))) as s(t)', funcname) into results using vals;
return results;
end;
$$ language plpgsql;