Skip to content

Instantly share code, notes, and snippets.

View markddavidoff's full-sized avatar
🎯
Focusing on Work, Sorry Side Projects

Mark Davidoff markddavidoff

🎯
Focusing on Work, Sorry Side Projects
View GitHub Profile
# idk if this works but it was an idea that popped into my head, saving for later here.
class SimpleSortedList(list):
def __init__(self, data=None, cmp=None):
if data is not None:
super(SimpleSortedList, self).__init__(data)
else:
super(SimpleSortedList, self).__init__()
self.cmp = cmp
@markddavidoff
markddavidoff / com_extentsion.py
Last active August 8, 2016 19:11
Comtypes wrapper to create an instance of a COM object with IClassFactory2 for objects that require licensing
# based on https://gist.github.com/EBNull/4219140
from uuid import UUID
from comtypes import GUID, IUnknown, CLSCTX_SERVER
from ctypes import OleDLL, WinDLL, c_ulong, byref, WINFUNCTYPE, \
POINTER, c_char_p, c_void_p
from ctypes.wintypes import HRESULT
IID_IClassFactory2 = "{B196B28F-BAB4-101A-B69C-00AA00341D07}"
@markddavidoff
markddavidoff / _code.py
Last active February 1, 2017 21:46
Python 2.7 backport for python Issue 12643 (code.InteractiveConsole ignores sys.excepthook)
"""
An unfixed bug exists in code.InteractiveConsole in Python 2.7. A patch was
applied for Python 3.3, this is a backport for python 2.7
bugs.python.org bug tracker:
http://bugs.python.org/issue12643
Issue 12643
Title: code.InteractiveConsole ignores sys.excepthook
Below code is copied from Python 2.7 `code` module and modified with changes
@markddavidoff
markddavidoff / pycon2017pt1.MARKDOWN
Last active May 23, 2017 19:00
Tidings from Pycon 2017 Pt1

Take Aways (And Some Other Cool Stuff) From Pycon 2017
Part 1: Performance

Some TL;DW summaries of useful things I saw at Pycon.


Cython

Pycon Talk: Cython as a Game Changer for Efficiency

You should use Cython for cheap, (surprisingly) easy to write performance improvements.

Cython is a superset of python with additional typing and metadata which allows you to get C-like performance with code which is mostly written in Python.

@markddavidoff
markddavidoff / Aeon-Labs-Smart-Strip.smartapp.groovy
Last active September 3, 2017 21:38
Aeon-Labs-Smart-Strip.smartapp.groovy
/**
* AEON Power Strip Binding
* This app allows you to bind 4 Virtual On/Off Tiles to the 4 switchable outlets.
*
* Author: chrisb
* Date: 12/19/2013
*/
// Automatically generated. Make future change here.
definition(
@markddavidoff
markddavidoff / auth.py
Created March 22, 2018 23:23
Fix Django Rest Framework DRF BasicAuthentication request is None eror
from rest_framework.authentication import BasicAuthentication
class CustomBasicAuthentication(BasicAuthentication):
"""
DRF.authentication.BasicAuthentication HTTP Basic authentication against username/password with a minor change
to pass request to django.contrib.auth.authenticate as for some reason DRF doesn't pass request...
"""
def authenticate(self, request):
self._current_request = request
def wrap_text(text, wrap_point, line_break=u'<br />', line_prefix=u'',):
point = wrap_point
left_to_process = text
wrapped_str = u''
while left_to_process:
wrapped_str += u"{line_prefix}{line_str}{line_break}".format(
line_prefix=line_prefix,
line_str=left_to_process[:wrap_point-len(line_prefix)],
line_break=line_break,
)
@markddavidoff
markddavidoff / gist:df3171e05b7dfaac2b524cecdfeda4ed
Created August 1, 2018 20:01
Find All most recent Historical Models objects for a Model tracked by simple history
hists = HistoricalModel.objects.values('id').annotate(max_pk=Max('pk')).order_by()
most_recent_hist_model_for_model = dict(hists.values_list('id', 'max_pk'))
@markddavidoff
markddavidoff / prepare-commit-msg
Last active September 11, 2018 23:11
prepare-commit-msg to add branch name to commit
#!/bin/sh
# put this in .git/hooks/prepare-commit-msg
# you can customize which branches should be skipped when
# prepending commit message.
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(development)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_NAME="${BRANCH_NAME##*/}"
@markddavidoff
markddavidoff / custom_auth.py
Last active December 13, 2018 20:20
Django Rest Framework BasicAuthentication infinite loop workaround
from rest_framework.request import Request as DRFRequest
class CustomBasicAuthentication(BasicAuthentication):
"""
NOTE: This is not the default method used to authenticate, this is to be used with DRF authentication_classes for
any views that need HTTP Basic Auth
DRF.authentication.BasicAuthentication HTTP Basic authentication against username/password with a minor change
to show messages returned in response_context
"""