Skip to content

Instantly share code, notes, and snippets.

View durden's full-sized avatar

Luke Lee durden

View GitHub Profile
@durden
durden / msg_ids.txt
Last active July 13, 2021 01:09
pylint message ids
C0102: Black listed name "%s"
C0103: Invalid %s name "%s"
C0111: Missing %s docstring
C0112: Empty %s docstring
C0121: Missing required attribute "%s"
C0202: Class method %s should have cls as first argument
C0203: Metaclass method %s should have mcs as first argument
C0204: Metaclass class method %s should have %s as first argument
C0301: Line too long (%s/%s)
C0302: Too many lines in module (%s)
@durden
durden / pytest_failure.py
Created April 3, 2014 14:44
Demonstrate py.test failure
def func_1():
return ['a', 'b', 'c']
def func_2():
"""
>>> func_1()
['a', 'b', 'c']
"""
pass
@durden
durden / numpy_ordering.py
Created March 14, 2014 14:04
Python numpy array ordering
>>> import numpy
>>> x = numpy.arange(27).reshape((3,3,3))
>>> x
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
@durden
durden / delete_class_methods.py
Created March 7, 2014 23:01
Deleting class methods in Python..a bad idea
>>> class Foo(object):
... def a(self): return 'a'
...
>>> x = Foo()
>>> print x.a()
a
>>>
>>> def b(self):
... return 'b'
...
@durden
durden / partial_obj.py
Created December 6, 2013 16:17
functools.partial returns a 'partial' object, not a function
>>> def func():
... print 'hello'
...
>>> from functools import partial
>>> def func(str_):
... print str_
...
>>> f = partial(func, 'world')
>>> f
<functools.partial object at 0x9fddf04>
@durden
durden / line_profiling.py
Created December 4, 2013 15:52
Awesome decorator using line_profiler with ability to specify which sub-functions to follow.
# Taken from: https://zapier.com/engineering/profiling-python-boss/
try:
from line_profiler import LineProfiler
def do_profile(follow=[]):
def inner(func):
def profiled_func(*args, **kwargs):
try:
profiler = LineProfiler()
@durden
durden / profiler.py
Created December 4, 2013 15:49
Handy little profile decorator with printing capabilities
# Taken from: https://zapier.com/engineering/profiling-python-boss/
import cProfile
def do_cprofile(func):
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
@durden
durden / timer_context_mgr.py
Created December 4, 2013 15:47
Clever implementation of context manager for profiling with checkpoint functionality
# Taken from: https://zapier.com/engineering/profiling-python-boss/
import time
class timewith():
def __init__(self, name=''):
self.name = name
self.start = time.time()
@property
@durden
durden / partial.py
Last active December 30, 2015 06:19
Useful snippet on how to use partial functions in Python to improve readabliity of matplotlib call
import matplotlib.pyplot as plt
from functools import partial
set_title = partial(plt.suptitle, fontweight='bold', fontsize=12)