Skip to content

Instantly share code, notes, and snippets.

View EBNull's full-sized avatar

EBNull EBNull

  • New York, NY
View GitHub Profile
@EBNull
EBNull / clipboard.py
Created September 4, 2012 21:03
Copy / Retreive text to / from windows clipboard
def clipcopy(z):
#http://stackoverflow.com/questions/579687
z = unicode(z)
try:
import win32clipboard
except ImportError:
from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
@EBNull
EBNull / reversem2m.py
Created September 28, 2012 17:43
Django reverse M2M field (for admin, etc)
#This class lets you add the 'other side' of a m2m to a django model.
#Why this is better than just using related_name:
# -Admin sees it as a real field, gives you the filter_horizontal widget if you ask
#
#Issues:
# -Probably breaks syncdb (table tries to be created twice)
#
#Non-issues:
# -Doesn't break south (custom introspection rule makes this field ignored)
@EBNull
EBNull / tempdb.py
Created October 16, 2012 20:56
In django, create a database definition that only exists for the current session.
"""
Example:
import django.db
from cStringIO import StringIO
def inspect_mdb(filename):
db_dict = {'ENGINE': 'access.pyodbc', 'OPTIONS': {'driver': 'access'}, 'NAME': filename}
with temp_db(db_dict, 'test') as using:
django.db.connections[using].cursor() #Connect immediately
@EBNull
EBNull / iclassfactory2.py
Created November 19, 2012 21:53
Create an instance of a COM object with IClassFactory2 for objects that require licensing.
import pythoncom
import win32com.client
from uuid import UUID
from ctypes import OleDLL, WinDLL, c_long, c_ulong, byref, WINFUNCTYPE, POINTER, c_char_p, pointer
from ctypes.wintypes import HRESULT
IID_IClassFactory2 = "{B196B28F-BAB4-101A-B69C-00AA00341D07}"
def CoCreateInstanceLicenced(clsid_class, iid_interface=pythoncom.IID_IDispatch, key='', dwClsContext=pythoncom.CLSCTX_SERVER, pythoncom_iid_interface=pythoncom.IID_IDispatch, pythoncom_wrapdisp=True):
@EBNull
EBNull / getobject.py
Created December 5, 2012 20:19
"Missing" win32com utilities for getting object instances from DLLs or from run-time licenced servers
__all__ = (
####### Class Objects
#CoGetClassObject - Normal, not wrapped
'CoDllGetClassObject', #Get ClassObject from a DLL file
####### ClassFactory::CreateInstance Wrappers
'CoCreateInstanceFromFactory', #Create an object via IClassFactory::CreateInstance
'CoCreateInstanceFromFactoryLicenced', #Create a licenced object via IClassFactory2::CreateInstanceLic
@EBNull
EBNull / comfind.py
Created December 5, 2012 21:18
Given a directory, find files containing COM objects
import os
import sys
import glob
import ctypes
import logging
log = logging.getLogger(__name__)
from collections import namedtuple
def path_iter(path, root=None, dirsortkey=None):
@EBNull
EBNull / normaldictreader.py
Created January 14, 2013 17:39
Replacement for csv.reader that reads encoded input into a dict based on column definitions.
import csv
class StreamedDataConverter(object):
"""A conversion description that can convert one list of data into a processed dict using the cols attribute.
>>> class MyConverter(StreamedDataConverter):
... cols = ['a', 'b', 'c']
...
>>> c = MyConverter(iter([[4,5,6],[6,7,8]]))
>>> c.map_fieldnames(['a','b','c'])
@EBNull
EBNull / 4096_tc.py
Last active December 15, 2015 04:19
Testcase of Python issue 706263, with a workaround.
#This file is a testcase of http://bugs.python.org/issue706263 with a workaround.
#Z:\>python 4096_tc.py
#Z:\>cat result.txt
#stdin: 0 stdout: 1 stderr: 2
#Bytes printed: 199998
#Z:\>pythonw 4096_tc.py
#Z:\>cat result.txt
#stdin: -2 stdout: -2 stderr: -2
#Bytes printed before exception was raised: 4096
#Traceback (most recent call last):
@EBNull
EBNull / screenon.py
Created March 27, 2013 22:11
Forces Windows to keep the display on (temporarily disables sleep mode while running). Netflix's silverlight app failed to do this for me :/
import os
import sys
import ctypes
ES_AWAYMODE_REQUIRED = 0x00000040
ES_CONTINUOUS = 0x80000000
ES_DISPLAY_REQUIRED = 0x2 #Forces the display to be on by resetting the display idle timer.
ES_SYSTEM_REQUIRED = 0x1 #Forces the system to be in the working state by resetting the system idle timer.
@EBNull
EBNull / PyQt4.pth
Created July 17, 2013 14:15
Add PyQt4 to a virtualenv when it's installed on the system. Place PyQt4.pth into your local site-packages dir.
import sys; import os; _p = os.path.join(sys.real_prefix, 'lib', 'site-packages'); sys.path.append(_p); import sip; import PyQt4; sys.path.remove(_p)