Skip to content

Instantly share code, notes, and snippets.

@gsakkis
gsakkis / render_query.py
Created January 19, 2013 11:25
Generate an SQL expression string with bound parameters rendered inline for the given SQLAlchemy statement.
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 January 31, 2024 03:00
Porting AES from M2Crypto to Crypto
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 / sync_await.py
Last active January 7, 2024 17:21
Awaiting async code from sync
import asyncio
import threading
from typing import Any, Awaitable, Iterable, Optional, TypeVar
T = TypeVar("T")
class sync_await:
def __enter__(self) -> "sync_await":
self._loop = asyncio.new_event_loop()
'''Manager-based polymorphic model inheritance.
This module provides a non-intrusive approach for accessing polymorphically
instances of a model hierarchy. Non-intrusive means:
- It does not require extending a custom ``Model`` base class or metaclass.
- It does not require a ``ForeignKey`` to ``ContentType`` or the ``contenttypes``
app in general. Instead the real class of an instance is determined based on
the value (**polymorphic identity**) of a user-specified discriminating field
(**polymorphic on**).
- It does not override the default (or any other) model ``Manager`` (unless
import argparse
import multiprocessing
import os
from timeit import default_timer
from typing import Optional, Sequence, Type, Union
import numpy as np
import tiledb
from tiledb.ml.readers.pytorch import PyTorchTileDBDataLoader
@gsakkis
gsakkis / test_quantize.py
Created March 12, 2012 09:11
Python decimal rounding behavior
-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

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 / insert.py
Created October 2, 2012 19:48
SQLAlchemy identity map order
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'
import pkgutil
def recursive_import(package):
for imp, name, ispkg in pkgutil.walk_packages(package.__path__, package.__name__ + '.'):
yield __import__(name, fromlist=[name])
@gsakkis
gsakkis / benchmark.py
Created October 3, 2011 21:36
Slope One
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,