Skip to content

Instantly share code, notes, and snippets.

@c0ldlimit
c0ldlimit / gist:4089134
Created November 16, 2012 17:18
Python: Advanced String Formatting
# http://www.python.org/dev/peps/pep-3101/
# "My name is Fred"
"My name is {0}".format('Fred')
"My name is {0.name}".format(open('out.txt', 'w'))
"My name is {0[name]}".format(dict(name='Fred'))
@c0ldlimit
c0ldlimit / gist:4089136
Last active October 12, 2015 21:27
Python: Append Dict to Dict #python #dict #append
# append dict extra to dict orig
orig.update(extra)
# make new dict with blend of both
dest = dict(orig) # or dict(**orig) or dict.copy()
dest.update(extra)
# Reference:
# http://stackoverflow.com/questions/8930915/python-append-dictionary-to-dictionary
@c0ldlimit
c0ldlimit / gist:4089145
Last active October 12, 2015 21:27
Python: Importing MATLAB .mat files #python #mat #matlab #c0ldlimit
# Importing MATLAB .mat files into Python
import scipy.io
mat = scipy.io.loadmat('file.mat')
@c0ldlimit
c0ldlimit / gist:4091273
Last active November 6, 2020 22:04
Python: How to use *args and **kwargs #python #args #kwargs #c0ldlimit
# This example passes one formal (positional) argument, and two more variable length arguments.
def test_var_args(farg, *args):
print "formal arg:", farg
for arg in args:
print "another arg:", arg
test_var_args(1, "two", 3)
# Here is an example of how to use the keyworded form. Again, one formal argument and two keyworded variable arguments are passed.
def test_var_kwargs(farg, **kwargs):
@c0ldlimit
c0ldlimit / gist:4101461
Last active October 12, 2015 23:08
Python: Stripping all whitespace #python #c0ldlimit #whitespace
s = " \t foo \n bar "
"".join(s.split())
#http://stackoverflow.com/questions/3739909/how-to-strip-all-whitespace-from-string
@c0ldlimit
c0ldlimit / gist:4106640
Created November 18, 2012 18:20
Git: Viewing Commit Tree
gitk --all
git log --graph --all --oneline
@c0ldlimit
c0ldlimit / gist:4106647
Created November 18, 2012 18:22
Git: Merging
# will attempt to merge into the current/active branch
git merge [branch_name]
@c0ldlimit
c0ldlimit / gist:4106731
Last active October 12, 2015 23:57
Git: Amending a Commit #git #c0ldlimit
git commit --ammend
# what this does is add the content of the staging area to the latest commit
# you'll then be asked for a commit message. This commit and its message will override the previous commit - but CREATES A NEW HEASH
@c0ldlimit
c0ldlimit / gist:4106750
Last active October 12, 2015 23:58
Git: Hard Reset #git #c0ldlimit
git reset --hard HEAD
# the --hard flag puts the content of whatever is in the targeted commit (in this case, HEAD) into both the working directory and staging area
# roll back individual files
git checkout
git checkout HEAD file.ext
@c0ldlimit
c0ldlimit / gist:4287824
Last active September 5, 2022 07:47
Converting None to NaN in numpy when casting a list to numpy arrays #numpy #c0ldlimit #python
import numpy as np
x = array([3,4,None,55])
y = np.array(x,dtype=float)
# OR
y = np.array(x)
z = y.astype(float)