Skip to content

Instantly share code, notes, and snippets.

@timothyekl
timothyekl / gist:1137985
Created August 10, 2011 19:53
Singleton tuplization of a set wrapper class's elements
class FooIterator:
def __init__(self, underlyingIter):
self.underlying = underlyingIter
def __iter__(self):
return self
def next(self):
try:
return (self.underlying.next(),)
@timothyekl
timothyekl / recompile.py
Created July 27, 2011 15:04
Recompile .tex files based on inotify from Python
import pyinotify
import os
import sys
class Handler(pyinotify.ProcessEvent):
def process_IN_MODIFY(self, event):
if event.name.endswith('.tex'):
print "Recompiling " + event.name + "..."
os.popen('pdflatex ' + event.name).read()
@timothyekl
timothyekl / recompile.rb
Created July 26, 2011 20:33
Watch a directory recursively, recompiling .tex files on changes
require 'rb-inotify'
notifier = INotify::Notifier.new
notifier.watch('.', :modify, :recursive) do |event|
if event.name.end_with? ".tex"
puts "#{`date`.chomp}: Modified #{event.name}; recompiling..."
`pdflatex -interaction batchmode -output-directory #{File.dirname(event.absolute_name)} #{event.absolute_name} > /dev/null 2>&1`
end
end
@timothyekl
timothyekl / smart_split.rb
Created July 14, 2011 13:52
Ruby: split string, respecting double quotes
class String
def smart_split
sa = self.split(/"/).collect { |x| x.strip }
return (1..sa.length).zip(sa).collect { |i,x| (i&1).zero? ? x : x.split }.flatten
end
end
@timothyekl
timothyekl / list2sql.rb
Created July 11, 2011 15:11
Read from stdin, converting to MySQL 'values' tuples
while line = $stdin.gets
puts "(" + line.split.map {|i| "'#{i}'"}.join(',') + "),"
end