Skip to content

Instantly share code, notes, and snippets.

@nathania
nathania / git_from_python_script.py
Created May 31, 2012 01:33
Execute git command from Python script
from subprocess import Popen, PIPE
from os import path
git_command = ['/usr/bin/git', 'status']
repository = path.dirname('/path/to/dir/')
git_query = Popen(git_command, cwd=repository, stdout=PIPE, stderr=PIPE)
(git_status, error) = git_query.communicate()
if git_query.poll() == 0:
# Do stuff
@nathania
nathania / type_2_to_type_1.py
Created June 10, 2012 17:44
Python type conversion
# Save one object's type and use as a function later
type_1 = type(type_1_obj)
type_2_to_1 = type_1(type_2_obj)
@nathania
nathania / RCF2.Rakefile.rb
Created August 4, 2012 21:09 — forked from CameronCarroll/RCF2.Rakefile.rb
Rakefile to compile Ruby/C/Fortran extensions
# Ruby/C/Fortran Bridge Rakefile
# Cameron Carroll, July 2012
# Purpose: Rake build script to facilitate calling Fortran code from Ruby in order to outsource
# heavy lifting computation. This script will compile Fortran and C code in present directory
# but leaves implementation and linking up to the user.
#
# Notes: This script may require a little bit of configuration depending on the system.
# 1: The Ruby include flags are specifically for MY system. I haven't found a way to generate them,
# so you have to either tailor them by hand or generate a extconf.rb makefile, enable verbose mode,
# and copy the include flags from the compile command. Yeah, I know, I know...
@nathania
nathania / gist:4162919
Created November 28, 2012 18:04 — forked from gglanzani/gist:3224737
Export your collection of papers in Papers to BibTeX
-- the following variable allows to choose what we want to export
-- it can be "Selected Papers Only", "Selected Collection" or "Entire Library"
set myDesiredSaveRange to "Selected Collection"
-- the following variable allows is the path where we save the bib file
set myPath to "~/Desktop/from_papers.bib"
-- do we also have a prettifier?
set runPrettifier to true
-- path to it
@nathania
nathania / gist:5454262
Created April 24, 2013 18:16
Mission control only shows windows in currently active space
defaults write com.apple.dock wvous-show-windows-in-other-spaces -bool FALSE
killall Dock
@nathania
nathania / setattr_self_key_val.py
Last active December 17, 2015 00:09
Set a variable with the same name as a parameter equal to the argument. Became very tired of writing self.x = x.
class Compact_assignment_from_pparams(object):
def __init__(self, p1, p2, p3, p4, p5, p6):
for key, value in locals().items():
setattr(self, key, value)
class Compact_assignment_from_kwargs(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
@nathania
nathania / port_select_python.sh
Last active December 17, 2015 00:38
Remember to switch the default cython, ipython, pep8, python, sphinx, and virtualenv ports (MacPorts).
# Select python version
port select --list python
port select --set python python33
# Select cython version
port select --list cython
port select --set cython cython33
# Select ipython version
port select --list ipython
@nathania
nathania / add_docstring_decorator.py
Last active December 17, 2015 00:38
Python decorator (factory function) to add a frequently-used docstring to a function. Existing docstrings are not overwritten.
def add_docstring(*args): # decorator factory function
"""
Constructs and appends a docstring to the decorated function's existing docstring.
When several functions share the same docstring(s), can be used to improve code readability.
Also makes life easier should the documentation ever need to be updated.
To understand decorators: simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
'join' a list of strings ('+' building is quadratic time): memonic.com/user/pneff/folder/python/id/1bufp
"""
@nathania
nathania / pre_and_post_incrementing.cpp
Created May 6, 2013 21:15
Pre- and post-increment operators in C++.
#include <iostream>
using std::cout, std::endl;
int x = 0;
int pre = ++x; // ++x incremements x, and returns (the incremented value of) x
cout << pre << ', ' << x << endl; // 1, 1
int post = x++; // x++ returns (the original value of) x, and incremements x
cout << post << ', ' << x << endl; // 1, 2
@nathania
nathania / named_formatting.py
Last active December 17, 2015 05:18
Named formatting in python print statements.
data = dict(state='active', canonical_name='un#ep')
print('Endpoint %(canonical_name)s is %(state)s' % data)
print('Endpoint {canonical_name} is {state}'.format(**data))
foo, bar = 'foo', 'bar'
print('%(foo)s%(bar)s' % locals())
print('{foo}{bar}'.format(**locals()))
print('{foo}{bar}'.format(foo='foo', bar='bar')) # how much faster is % formatting?
# TODO: is nesting possible?