Skip to content

Instantly share code, notes, and snippets.

View keybase.md

Keybase proof

I hereby claim:

  • I am gsakkis on github.
  • I am gsakkis (https://keybase.io/gsakkis) on keybase.
  • I have a public key whose fingerprint is 83E7 7BB2 98CA A4EC 9556 B0C1 66D2 9747 83C4 C52F

To claim this, I am signing this object:

@gsakkis
gsakkis / render_query.py
Created Jan 19, 2013
Generate an SQL expression string with bound parameters rendered inline for the given SQLAlchemy statement.
View render_query.py
from datetime import datetime, date
from sqlalchemy.orm.query import Query
def render_query(statement, bind=None):
"""
Generate an SQL expression string with bound parameters rendered inline
for the given SQLAlchemy statement.
WARNING: This method of escaping is insecure, incomplete, and for debugging
purposes only. Executing SQL statements with inline-rendered user values is
@gsakkis
gsakkis / aes.py
Last active Jan 23, 2020
Porting AES from M2Crypto to Crypto
View aes.py
from binascii import hexlify, unhexlify
from hashlib import md5
from Crypto.Cipher import AES
try:
from M2Crypto import EVP
except ImportError:
EVP = None
def m2_encrypt(plaintext, key, iv, key_as_bytes=False, padding=True):
@gsakkis
gsakkis / insert.py
Created Oct 2, 2012
SQLAlchemy identity map order
View insert.py
import random
from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
Base = declarative_base()
class Author(Base):
__tablename__ = 'author'
@gsakkis
gsakkis / test_quantize.py
Created Mar 12, 2012
Python decimal rounding behavior
View test_quantize.py
-2.695
reduced by ROUND_UP, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_EVEN
increased by ROUND_DOWN, ROUND_05UP, ROUND_CEILING, ROUND_HALF_DOWN
-2.669, -2.699
reduced by ROUND_UP, ROUND_FLOOR, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN
increased by ROUND_DOWN, ROUND_05UP, ROUND_CEILING
-2.665
reduced by ROUND_UP, ROUND_FLOOR, ROUND_HALF_UP
increased by ROUND_DOWN, ROUND_05UP, ROUND_CEILING, ROUND_HALF_DOWN, ROUND_HALF_EVEN
-2.661, -2.691
View recursive_import.py
import pkgutil
def recursive_import(package):
for imp, name, ispkg in pkgutil.walk_packages(package.__path__, package.__name__ + '.'):
yield __import__(name, fromlist=[name])
View benchmark.py
import sys, csv, time
from collections import defaultdict
from itertools import islice
import orig_slopeone, slopeone, numpy_slopeone
classes = [
orig_slopeone.SlopeOne,
#orig_slopeone.SlopeOneNumpy, # XXX: dog slow
slopeone.SlopeOne,
@gsakkis
gsakkis / bincounthist.py
Created Apr 21, 2011
automatic binning for integer data histograms
View bincounthist.py
import sys
from itertools import groupby
from operator import itemgetter
def bincount_histogram(nums, num_bins=sys.maxint, min_bin_count=1):
num_bins = max(num_bins, 1)
min_bin_count = max(min_bin_count, 1)
# initialize the bins of width=1
bin_edges, counts = [], []
@gsakkis
gsakkis / gist:866301
Created Mar 11, 2011
Dumbo remote debugging
View gist:866301
import os, sys
class Winpdb(object):
'''Embed a Winpdb server.
If you have to tunnel to connect remotely from the Winpdb client, run:
ssh -C -N -f -L 51000:localhost:51000 $SERVER_HOST
@gsakkis
gsakkis / gist:631306
Created Oct 17, 2010
Replace classmethod with descriptor
View gist:631306
diff --git a/django/views/generic/base.py b/django/views/generic/base.py
--- a/django/views/generic/base.py
+++ b/django/views/generic/base.py
@@ -9,6 +9,39 @@
logger = getLogger('django.request')
+class AsViewDescriptor(object):
+ def __get__(self, instance, cls):
+ if instance is not None:
You can’t perform that action at this time.