Skip to content

Instantly share code, notes, and snippets.

View edelooff's full-sized avatar

Elmer de Looff edelooff

View GitHub Profile
@edelooff
edelooff / gist:1d280ec2745248295dae
Created June 9, 2014 18:56
reST article to demonstrate pelican issue #1369

Automatic post summary causes weird line numbers in code-block

date

2014/06/09

tags

Pelican, reStructuredText

This article is a demonstration for Pelican issue #1369. The SUMMARY_MAX_LENGTH is set to 100 words, which means that index pages will list the first 100 words of this article. This includes a portion of the below code snippet, which configured to have its line numbers in a table.

@edelooff
edelooff / tileable_hexagons.py
Created October 1, 2014 21:14
Drawing tileable hexagons with PIL + Aggdraw
"""Module for drawing of randomly colored, tileable hexagons."""
import math
import random
from PIL import Image
from aggdraw import Draw, Brush
class HexagonGenerator(object):
@edelooff
edelooff / integrity_example.py
Created June 5, 2015 18:44
Catching and handling IntegrityError in SQLAlchemy
import sys
import sqlalchemy as sa
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
base = declarative_base()
print 'SQLAlchemy version {}'.format(sa.__version__)
@edelooff
edelooff / app.py
Created August 16, 2016 16:16
Pyramid 1.7 view deriver to alert on view_configs that lack a permission setting.
def permission_check(view, info):
"""View deriver to verify all view configs define a 'permission'.
This deriver prints a message for each view config that lacks a permission
setting. Regardless of this setting, the original view is returned.
"""
opts = info.options
if opts.get('permission') is None:
name_parts = [opts['view'].__module__, opts['view'].__name__]
if opts['attr'] is not None:
@edelooff
edelooff / feistel.py
Last active November 10, 2021 00:24
Simple Python Feistel encoder/decoder
"""A simple 8-round Feistel network to encode/decode 8-bit integers.
See https://en.wikipedia.org/wiki/Feistel_cipher for construction details.
"""
from random import randint
ROUNDS = 8
KEYS = [(randint(11, 19), randint(1, 200)) for _ in range(ROUNDS)]
@edelooff
edelooff / declarative.py
Created June 2, 2018 10:14
Example uses of Kalpa
from kalpa import Root, Node, branch
USERS = {...}
class Root(Root):
"""Traversal root for Pyramid application."""
users = branch('UserCollection')
@edelooff
edelooff / duplicate_perf.py
Last active October 12, 2018 09:23
Collection length checking before determining duplicate items
import timeit
NO_DUPLICATES = (
'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve')
WITH_DUPLICATES = (
'one', 'two', 'three', 'four', 'five', 'six',
'seven', 'one', 'nine', 'two', 'eleven', 'three')
@edelooff
edelooff / merge_benchmark.py
Created April 11, 2020 14:57
Benchmarking functions to merge two sorted lists
"""Merging two sorted lists in Python in four ways.
1. The "stupid naive" way of adding them together and sorting the whole thing.
This should be pretty fast as timsort is designed to be fast on partially
sorted lists. However, it uses more memory and might not scale very well?
2. The "picking an algorithm designed for this" way of using heapq.merge. This
provides a generator that walks the two lists without additional use of
memory.
3. The "C-way" of indexing our way through two lists. This being Python though,
we will be yielding the values though (wrapping the resulting generator in
@edelooff
edelooff / sqla_secondary.py
Created April 24, 2020 09:14
SQLAlchemy secondary join
from sqlalchemy import (
Column,
ForeignKey,
Integer,
Text,
create_engine)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import (
relationship,
sessionmaker)
@edelooff
edelooff / column_flag.py
Last active May 12, 2020 19:36
SQLAlchemy columns with a hybrid property boolean flag to check for presence (non-nullness)
from sqlalchemy import (
Column,
Integer,
DateTime,
Text,
create_engine,
func)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.inspection import inspect