Skip to content

Instantly share code, notes, and snippets.

View urschrei's full-sized avatar

Stephan Hügel urschrei

View GitHub Profile
@urschrei
urschrei / snippet.xml
Created July 9, 2010 22:00
\glossaries modifications for memoir.xslt
<xsl:template match="html:li" mode="glossary">
<xsl:param name="footnoteId"/>
<xsl:if test="parent::html:ol/parent::html:div/@class = 'footnotes'">
<xsl:if test="concat('#',@id) = $footnoteId">
<xsl:apply-templates select="html:span[@class='glossary sort']" mode="glossary"/>
<xsl:apply-templates select="html:span[@class='glossary name']" mode="glossary"/>
<xsl:variable name="glsname">
<xsl:apply-templates select="html:span[@class='glossary name']" mode="glossary"/>
</xsl:variable>
<xsl:text>,</xsl:text>
@urschrei
urschrei / \glossaries modifications for xhtml2latex.xslt
Created July 9, 2010 22:05
\glossaries modifications for xhtml2latex.xslt
<!-- anchors -->
<xsl:template match="html:a[@href]">
<xsl:param name="footnoteId"/>
<xsl:choose>
<!-- footnote (my addition)-->
<xsl:when test="@class = 'footnote'">
<xsl:text>\footnote{</xsl:text>
<xsl:apply-templates select="/html:html/html:body/html:div[@class]/html:ol/html:li[@id]" mode="footnote">
<xsl:with-param name="footnoteId" select="@href"/>
</xsl:apply-templates>
@urschrei
urschrei / snippet.sh
Created July 31, 2010 19:03
Automatically link a dir and contents with a Github repo of the same name
#!/bin/bash
#######################################################################################
## save as "github" in your path, and chmod+x
## Takes one optional argument: a directory name (new or existing)
## in which to create the repo (script assumes it's the same name as the Github repo)
## if called without an argument, assumes you wish to use the current working directory
## example: "github foo" creates (if it doesn't exist) dir foo in current working dir
## switches to foo initialises git repo, adds files, performs initial commit,
## sets remote master to [email protected]:github_user/foo.git and
## attempts to push to it. If the push fails, .git dir is removed and the script exits
@urschrei
urschrei / nested_dicts_lists.py
Created November 12, 2010 23:46
Do things with lists in nested dictionaries
import sys
import os
import itertools
things = {
'thing_1' : {
'country':'France',
'people':'authors',
'people_names':['Stendhal', 'Flaubert']
@urschrei
urschrei / twitrend
Created February 1, 2011 00:14
retrieve trending topics from a WOEID location
#!/usr/bin/env python
# encoding: utf-8
"""
twitrend.py
"""
import sys
import os
import tweepy
@urschrei
urschrei / tweepy_trend_output
Created February 1, 2011 00:43
Output of Tweepy's trends_location method
[{'created_at': '2011-02-01T00:36:34Z', 'trends': [{'url': 'http://search.twitter.com/search?q=%23wishuwould', 'query': '%23wishuwould', 'events': None, 'promoted_content': None, 'name': '#wishuwould'}, {'url': 'http://search.twitter.com/search?q=%23questionsidontlike', 'query': '%23questionsidontlike', 'events': None, 'promoted_content': None, 'name': '#questionsidontlike'}, {'url': 'http://search.twitter.com/search?q=%23februarywish', 'query': '%23februarywish', 'events': None, 'promoted_content': None, 'name': '#februarywish'}, {'url': 'http://search.twitter.com/search?q=Purp+%26+Patron', 'query': 'Purp+%26+Patron', 'events': None, 'promoted_content': None, 'name': 'Purp & Patron'}, {'url': 'http://search.twitter.com/search?q=Egyptians', 'query': 'Egyptians', 'events': None, 'promoted_content': None, 'name': 'Egyptians'}, {'url': 'http://search.twitter.com/search?q=Kool+Herc', 'query': 'Kool+Herc', 'events': None, 'promoted_content': None, 'name': 'Kool Herc'}, {'url': 'http://search.twitter.com/search?q=A
@urschrei
urschrei / urllib2 mock
Created March 14, 2011 10:17
Build and install an OpenDirector instance to mock urllib2.open() http(s) calls
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2
from StringIO import StringIO
def mock_response(req, resp_obj, resp_code):
""" Mock response for MyHTTPSHandler
"""
@urschrei
urschrei / mmd3_proc.py
Created April 13, 2011 18:37
Convert a MultimarkDown file into a .tex using MMD3, and compile into a PDF using XeLaTeX, Biber and Biblatex
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
mmd3_proc.py
Convert a MultiMarkdown file into a latex file using MMD3,
and process it using XeLaTeX, Biber and Makeglossaries
Takes a single argument: the input file to process
Outputs a PDF with the same basename as as the input file to cwd
@urschrei
urschrei / lists_tuples.py
Created May 1, 2011 22:07
Combining dicts, unpacking tuples etc.
# produce the cartesian product of input dicts
# cartesian product is non-commutative, so order matters
import itertools
output = list(itertools.product(dict_one, dict_two, dict_three)
# now you have a list containing nested tuples of all the input dict combinations:
# [(('text_1', value_1), ('text_2', value_2), ('text_3', value_3)), … ]
# output list of concatenated text and summed values for each top-level tuple
flattened = [[' '.join(text), sum(numbers)] for text, numbers in [zip(*item) for item in output]]
@urschrei
urschrei / excel_things.py
Created May 7, 2011 12:41
Open CSV or Excel files, return the contents as a list of lists, write out to Excel file
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
excel_things.py
Created by Stephan Hügel on 2011-05-07
Read XLS or CSV files, and return contents as unicode strings in nested lists
One row per list item, one column per nested list item:
[[u'foo', u'bar'], [u'baz', u'abc123'], … ]