Skip to content

Instantly share code, notes, and snippets.

View pzelnip's full-sized avatar
🤷‍♂️

Adam Parkin pzelnip

🤷‍♂️
View GitHub Profile
import random
import timeit
from copy import deepcopy
def inter_reduce(sets):
return reduce(set.intersection, sets)
def inter_for_deepcopy(sets):
sets = iter(sets)
result = deepcopy(next(sets))
@pzelnip
pzelnip / gist:2696496
Created May 14, 2012 20:23
Define all rich comparison operators in terms of lt
class ComparableMixin:
def __eq__(self, other):
return not (self < other or other < self)
def __le__(self, other):
return not other < self
class Foo(ComparableMixin):
def __init__(self, val):
self.val = val
@pzelnip
pzelnip / tryexcept_vs_isinstance.py
Created June 18, 2012 21:50
Showing that try/except is slower than isinstance
'''
Example showing that isinstance checks are faster than doing a "it's better to ask for forgiveness than
permission approach.
'''
from timeit import Timer
# Silly timing code that can be ignored
def timeruns(runs, num_iterations = 100):
return [(label, Timer(cmd, imports).timeit(num_iterations))
@pzelnip
pzelnip / gist:3139645
Created July 18, 2012 23:20
Counting number of elements in a list that meet criteria
from timeit import Timer
if __name__ == "__main__":
setup = 'x = list(range(100000))'
num_iter = 1000
print(Timer('sum(1 for item in x if item % 2 == 0)', setup).timeit(num_iter))
print(Timer('len([item for item in x if item % 2 == 0])', setup).timeit(num_iter))
# prints (on my machine):
# 12.4215...
@pzelnip
pzelnip / gist:5303912
Created April 3, 2013 18:38
Maxing out CPU cores
import java.util.ArrayList;
import java.util.List;
public class TestTest implements Runnable {
public static void main(String [] args) throws InterruptedException {
List<Thread> threads = new ArrayList<Thread>();
for (int x = 0; x < 10; x++) {
@pzelnip
pzelnip / gist:5528969
Created May 6, 2013 23:02
Getting what the grading system gave me.
import sys
lines = []
for i in range(1, len(sys.argv)):
lines.append("================================== START OF sys.argv%s==========================\n" % i)
with open(sys.argv[i]) as fobj:
for line in fobj:
lines.append(line.decode('utf-8'))
lines.append("================================== END OF sys.argv%s==========================\n" % i)
@pzelnip
pzelnip / gist:6080964
Last active December 20, 2015 05:49
Creating a static temporary directory as suitable for use in a JUnit @DataPoints method. Based upon the advice at: http://stackoverflow.com/a/17846991/808804
private static File tmpDir;
@BeforeClass
public static void setUp() {
String dirname = UUID.randomUUID().toString().replace("-", "");
tmpDir = new File(System.getProperty("java.io.tmpdir"), dirname);
if (!tmpDir.exists()) {
tmpDir.mkdir();
}
}
@pzelnip
pzelnip / The MXML Definition of the datagrid
Created August 1, 2014 15:54
Conditional Datagrid cell editing
<mx:DataGrid id="myGrid"
editable="true"
itemEditBeginning="disableEditing(event)">
<!-- definition of columns & such here -->
</mx:DataGrid>
@pzelnip
pzelnip / Example.mxml
Last active August 29, 2015 14:04
Complete example of conditional datagrid editing dependent upon associated value in row
<?xml version="1.0"?>
<!-- itemRenderers\events\EndEditEventPreventEdit.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="init()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;

iTerm2 – Useful Shortcuts (Mac OS X)

with a hat tip to Sublime Text 2 Shortcuts

The Awesome

⌘; autocomplete
⌘⌥B instant replay
⌘⌥E search across all tabs