Skip to content

Instantly share code, notes, and snippets.

View agronholm's full-sized avatar

Alex Grönholm agronholm

  • NextDay Solutions Oy
  • Nurmijärvi, Finland
View GitHub Profile
@agronholm
agronholm / serialization.py
Last active August 24, 2017 23:31
WAMP serialization module
import codecs
import json
import sys
from base64 import b64decode, b64encode
from codecs import CodecInfo
from functools import partial
from json import JSONDecoder, scanner
import six
@agronholm
agronholm / Dockerfile
Created May 6, 2017 12:17
Slimmer pypy3 dockerfile for crossbar
FROM debian:jessie
ARG CROSSBAR_VERSION
ARG PYPY_VERSION=5.7.1
ARG PYPY_SHA256SUM=2abaa54d88c9b70b64c37083e7e430a1d3a8f78f8de92e484a988b7aca1e50a7
# install dependencies and Crossbar.io
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
ca-certificates \
@agronholm
agronholm / subscriber.py
Created February 24, 2017 23:22
Sample WAMP subscriber
# Install asphalt-wamp first:
# pip install git+https://github.com/asphalt-framework/asphalt-wamp.git@932183440a064
from pprint import pprint
from asphalt.core import Context, run_application, ContainerComponent
def handle_event(ctx, event):
pprint(event)
@agronholm
agronholm / events.py
Created January 14, 2017 10:44
Broadcasting SQLAlchemy model events
from sqlalchemy.orm.util import object_mapper
def get_primary_key(obj):
"""Returns the primary key of the given persistent object."""
mapper = object_mapper(obj)
pk = mapper.primary_key_from_instance(obj)
return pk[0] if len(pk) == 1 else pk
@agronholm
agronholm / reader.py
Created January 7, 2017 19:48
Small script to trigger error in StreamReader
import asyncio
async def execute():
process = await asyncio.create_subprocess_exec(
"timeout", "0.1", "cat", "/dev/urandom", stdout=asyncio.subprocess.PIPE)
while True:
data = await process.stdout.read(65536)
print('read %d bytes' % len(data))
@agronholm
agronholm / gist:48d5eda810588dfed2db
Last active August 29, 2015 14:16
Jython standalone -> Java Web Start jar converter
#!/bin/sh
set -e
# Converts a Jython standalone jar into something usable with Java Web Start
if [ -z $1 ]; then
echo "Please give the path to the standalone jar as the first argument."
exit 1
fi
CURRDIR=$(pwd)
@agronholm
agronholm / gist:d037822f5f3669e7fab1
Last active August 29, 2015 14:15
Jython 2.7b3 weirdness
Error in sys.excepthook:
Traceback (most recent call last):
File "__pyclasspath__/etikettu/errorhandler.py", line 63, in exceptHook
UnboundLocalError: local variable 'traceback' referenced before assignment
Original exception was:
TypeError: canImport() takes exactly 3 arguments (2 given)
@agronholm
agronholm / gist:3370b33316b20c6185bf
Created December 5, 2014 18:17
Creating tables in a transaction
from sqlalchemy import create_engine, Table, Column, Integer, String, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('postgresql:///testdb', echo=True)
Base = declarative_base()
SALT = dict(
phrase=64,
bytes=32,
)
@agronholm
agronholm / gist:a41ec3d990a28ed65a0b
Last active August 29, 2015 14:07
Dotted path resolution and tests
def get_callable_name(func):
"""
Returns the best available display name for the given function/callable.
:rtype: str
"""
# the easy case (on Python 3.3+)
if hasattr(func, '__qualname__'):
return func.__qualname__
@agronholm
agronholm / gist:879c18d186079c9de75c
Created July 7, 2014 18:57
Enums with integer values
class IntegerEnumWrapper(TypeDecorator):
impl = Integer
def __init__(self, cls):
TypeDecorator.__init__(self)
self.wrapped = cls
def process_bind_param(self, value, dialect):
if value is None:
return None