Skip to content

Instantly share code, notes, and snippets.

View akaptur's full-sized avatar

Allison Kaptur akaptur

View GitHub Profile
@akaptur
akaptur / gist:3408317
Created August 20, 2012 22:01
A way to get the attributes of an object without using getattr
for attr in dir(obj):
code = 'obj.' + attr
print attr, eval(code)
for attr in [x for x in dir(obj) if not callable(getattr(obj, x))]:
code = 'obj.' + attr
print attr, eval(code)
@akaptur
akaptur / gist:3836483
Created October 4, 2012 21:10
.bashrc settings
alias git=hub
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
GIT_PS1_SHOWDIRTYSTATE=true
GIT_PS1_SHOWSTASHSTATE=true
To dissect the behavior of this server and client, I need two things: more realistic network latency than is produced by making connections directly to localhost, and some way to see a microsecond-bymicrosecond report on what the client and server are doing.
These two goals may initially seem impossible to reconcile. If I run the client and server on the same machine, the network latency will not be realistic. But if I run them on separate servers, then any timestamps that I print will not necessarily agree because of slight differences between the machines’ clocks.
My solution is to run the client and server on a single machine (my Ubuntu laptop, in case you are curious) but to send the connection through a round-trip to another machine (my Ubuntu desktop) by way of an SSH tunnel. See Chapter 16 and the SSH documentation itself for more information about tunnels. The idea is that SSH will open local port 1061 here on my laptop and start accepting connections from clients. Each connection will then be for
@akaptur
akaptur / Traceback
Created November 15, 2012 17:33
simple pdb traceback
Traceback (most recent call last):
File "minimal_pdb_traceback.py", line 7, in <module>
y = "line of code not triggering an error"
AssertionError
socktactoe [master *+]\ ⚲git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: multi_tictactoe.py
# modified: socktactoe_server.py
#
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
@akaptur
akaptur / gist:4348873
Last active December 10, 2015 00:09
A small depiction of issues with floating point arithmetic in python.
def naive_add(lis):
total = 0
for elem in lis:
total += elem
return total
def recursive_add(lis):
if len(lis) == 1:
return lis[0]
elif len(lis) == 0:
@akaptur
akaptur / activate
Created December 23, 2012 23:02
The version of virtualenv's activate on my machine
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly
deactivate () {
unset pydoc
# reset old environment variables
if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
@akaptur
akaptur / gist:4431890
Last active December 10, 2015 12:08
Surprised by stable sort
def two_sort(ex):
ex_copy = ex[:]
ex.sort(key=lambda tup: tup[1])
ex.reverse()
print "Ex: ", ex
ex_copy.sort(key=lambda tup: tup[1], reverse=True)
print "Copy:", ex_copy
@akaptur
akaptur / gist:5239387
Created March 25, 2013 18:26
Trivial generators example
>>> def gen():
... yield 1
... yield 2
... yield 3
...
>>> for i in gen():
... print i
...
1
2
@akaptur
akaptur / gist:5399666
Created April 16, 2013 21:08
python's this.py
s = """Gur Mra bs Clguba, ol Gvz Crgref
Ornhgvshy vf orggre guna htyl.
Rkcyvpvg vf orggre guna vzcyvpvg.
Fvzcyr vf orggre guna pbzcyrk.
Pbzcyrk vf orggre guna pbzcyvpngrq.
Syng vf orggre guna arfgrq.
Fcnefr vf orggre guna qrafr.
Ernqnovyvgl pbhagf.
Fcrpvny pnfrf nera'g fcrpvny rabhtu gb oernx gur ehyrf.