Skip to content

Instantly share code, notes, and snippets.

View Midnighter's full-sized avatar

Moritz E. Beber Midnighter

View GitHub Profile
@Midnighter
Midnighter / kile_regex_replace.txt
Created June 29, 2012 11:07
Search and replace in Kile using non-greedy pattern matching.
By example, I wanted to replace all inline math $...$ with \( ... \). So I had to match the (escaped) dollar sign \$ then put the text inbetween in a group (this is achieved by the parentheses). The brackets, in usual regex style, form a category of symbols. In this case every symbol but the dollar sign in order to simulate non-greedy matching. This is like using .+? in normal regular expressions. So the search pattern is:
\$([^\$]+)\$
The group given by the parentheses can be referred to in the replacement by \1, so the replacement text is:
\\( \1 \\)
@Midnighter
Midnighter / gist:3017475
Created June 29, 2012 11:31
Often there is a situation when default input arguments need to be converted to a specific type or have a default value of a different type. It's simple to do but can look messy, so I started using this little function.
def convert_argument(arg, new_type, default=None):
"""
Convert an argument to a new type unless it is `None`.
"""
return default if arg is None else new_type(arg)
@Midnighter
Midnighter / gist:3017523
Created June 29, 2012 11:48
A metaclass adds a new instance of a mutable class variable to each subclass of MutableBase without the need to write this attribute into the subclass' definition, i.e., users of MutableBase need not know about it.
class MetaBase(type):
"""
Metaclass for MutableBase.
This metaclass is one of two possible solutions to having a per-class
mutable class attribute. The per-class aspect is tricky because the
attribute is a mutable object. It is thus shared among MutableBase and all
its subclasses.
Shared state of the mutable class attribute is avoided with this metaclass
@Midnighter
Midnighter / gist:3083155
Created July 10, 2012 13:12
Redirecting attribute access in a compound class with __getattr__
def __getattr__(self, attribute):
"""
Defer any attribute access that was unsuccessful to a compound class.
Introduce a safeguard, for example, when unpickling this class, by raising an AttributeError.
"""
if "compound" in self.__dict__:
self.compound.__getattribute__(attribute)
raise AttributeError("'{0}' object has no attribute '{1}'".format(self.__class__.__name__, attribute))
@Midnighter
Midnighter / gist:3161164
Created July 22, 2012 22:07
Dynamic printing of recurring information to a single line.
class ProgressPrinter(object):
"""
Write a recurring message to a stream on the same line.
Warning
-------
Only works properly if the message fits on a single line for the given
stream.
"""
@Midnighter
Midnighter / parallel_tweaking.ipynb
Created February 18, 2013 12:08
Exploration of a parallel implementation of a nested for-loop using IPython's machinery.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Midnighter
Midnighter / rp2_ggplot2_2.3.6_bugs.ipynb
Created April 4, 2013 12:54
IPython Notebook showing problems with axes marks, labels, and legend using the interactive ggplot2.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Midnighter
Midnighter / parsing_regulondb
Created April 5, 2013 21:14
Simple example of parsing a RegulonDB dataset into a network.
{
"metadata": {
"name": "simple_tf_gene_network"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
@Midnighter
Midnighter / plot_usage.ipynb
Created October 21, 2013 13:53
Preferred Usage
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{