Skip to content

Instantly share code, notes, and snippets.

class DatabaseWrapper:
def __init__(self, params=None):
if params is None:
# read connection params from a config file (?)
params = self.config()
self.connection_params = params
self.connection = None
def config(self):
@embray
embray / win_register_python.py
Created July 19, 2013 22:25
A script to register Python under HKEY_CURRENT_USER in addition to HKEY_LOCAL_MACHINE
#
# script to register Python 2.0 or later for use with win32all
# and other extensions that require Python registry settings
#
# written by Joakim Loew for Secret Labs AB / PythonWare
#
# source:
# http://www.pythonware.com/products/works/articles/regpy20.htm
#
# modified by Valentine Gogichashvili as described in http://www.mail-archive.com/distutils-sig@python.org/msg10512.html
@embray
embray / capture_block.py
Last active June 22, 2020 08:07
Capture the contents of a with statement code block without executing the block
import contextlib
import dis
import inspect
import linecache
import struct
import sys
class SkipBlock(Exception):
pass
@embray
embray / demo.py
Created December 4, 2019 15:23
Demo
def positive(L, x):
c1 = -1
c2 = -1
for c in L:
if c1 >= 0 and c2 >= 0:
break
if c == x and c1 < 0:
continue
@embray
embray / suggest_backports.py
Last active July 26, 2019 11:41
List pull requests merged since the last tag on the given release branch which have not yet been merged into the release branch.
#!/usr/bin/env python
# I wasn't happy with any of the GitHub libraries for Python that I tried so I
# just used the GitHub API directly. If someone would like to rewrite this
# using a library please be my guest
from __future__ import unicode_literals
import argparse
import base64
root@b554a1ceecf7:/# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.4 LTS"
root@b554a1ceecf7:/# python3.6
Python 3.6.5 (default, May 3 2018, 10:08:28)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
@embray
embray / foo.pyx
Last active April 11, 2016 11:19
Example Cython project with sharing between pyx file and plain C file
cdef public void hello():
print "Hello world!"
@embray
embray / win32_is_append_mode.py
Last active December 22, 2015 08:19
A function written with ctypes called is_append_mode_win32 which, given a file object, can determine, on Windows, if it was opened in append mode (regardless of what its mode string says). In UNIX this would be equivalent to fcntl(fd, F_GETFL) & O_APPEND. These are the hoops we have to go through to get the same functionality on Windows.
from ctypes import (cdll, c_size_t, c_void_p, c_int, c_char, Structure,
POINTER, cast)
from ctypes.util import find_msvcrt
msvcrt = cdll.LoadLibrary(find_msvcrt())
# Constants
IOINFO_L2E = 5
@embray
embray / classes.ipynb
Created May 9, 2013 19:21
Notebook for my PyLunch talk on classes. This contains some content that we didn't get to yet in PyLunch, as well as some new content that was added after this was first presented.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.