Wednesday, March 21, 2012

Cycling with Jinga

I was tweaking the layout.html file for Python's Sphinx and found the following random tit-bit of use when using Jinga:


{%- for rellink in rellinks[1:]|reverse %}
  <div class="{{ loop.cycle('buttonPrevious', 'buttonNext') }}">
   <a href="{{ pathto(rellink[0]) }}">
    {{loop.cycle('Previous', 'Next')}}
  </a>
{%- endfor %}




With loop.cycle you can cycle through strings or divs
as the loop iterates..

In the above example, a div is created first for buttonPrevious and then
the next iteration for buttonNext. The same applies to the strings that determine what gets
printed on the button.

Tuesday, March 20, 2012

Stashing in Git

If you're working on a branch and which to switch to another for some reason without committing,
or you wish to try out something on the current branch, you can save your current state to the
stash .
Use
git stash save


When you're ready to continue what you were working on, go back to the branch that you used
stash on and use:
git stash apply

If you don't want to use your stash anymore, and just work from your last commit, use
git stash clear

For a more thorough run-through on git-stashing , check out
http://ariejan.net/2008/04/23/git-using-the-stash

Monday, March 5, 2012

doctest_mode in iPython

If you want to import code into iPython that has been copied from the python console
or code that is to be run as a doctest in Python Sphinx, a useful trick to use in iPython
is

%doctest_mode

For example:

>>> import numpy as np
>>> some_value = 35
>>> np.sqrt(some_value)


You can copy this directly into iPython and it will treat it like normal python code.
No need having to copy every line of code after >>>