Skip to content

Instantly share code, notes, and snippets.

View mgedmin's full-sized avatar

Marius Gedminas mgedmin

View GitHub Profile
@mgedmin
mgedmin / euler14.py
Last active September 4, 2015 12:44
Project Euler problem 14
# refactoring of http://sandrotosi.blogspot.com/2010/03/project-euler-problem-14.html
cache = {1: 1}
def collatz(n):
if n not in cache:
if n % 2 == 0:
cache[n] = 1 + collatz(n/2)
else:
cache[n] = 1 + collatz(3*n + 1)
@mgedmin
mgedmin / .pythonrc
Created December 30, 2010 20:37
snippets for export PYTHONSTARTUP=~/.pythonrc
# This is the actual file that combines all of the other snippets
# I've been using it for a while, so it's officially Bug Free (TM)
# Python startup script. vim: set ft=python :
# from http://www.norvig.com/python-iaq.html
# also see Tarek Ziade's _Expert_Pythom_Programming_ page 19
import os, sys
# Coloured prompt
if os.getenv('TERM') in ('xterm', 'vt100', 'rxvt', 'Eterm', 'putty'):
diff collectd-4.10.1.orig/configure.in collectd-4.10.1/configure.in
--- collectd-4.10.1.orig/configure.in
+++ collectd-4.10.1/configure.in
@@ -1626,7 +1626,7 @@
# Check for the iptc_init symbol in the library.
if test "x$with_libiptc" = "xyes" && test "x$with_own_libiptc" = "xno"
then
- AC_CHECK_LIB(iptc, iptc_init,
+ AC_CHECK_LIB(ip4tc, iptc_init,
[
@mgedmin
mgedmin / buildout.cfg
Created January 3, 2012 18:40
buildout-versions case-sensitivity bug?
[buildout]
extensions = buildout-versions
versions = versions
parts = foo
[foo]
recipe = zc.recipe.egg:scripts
eggs = Pyramid
zilch
scripts = zilch-web
@mgedmin
mgedmin / whatsmodified.sh
Created January 8, 2012 22:58
Find all svn/bzr/git/hg checkouts under ~/src and see if any have uncommitted changes.
#!/bin/bash
verbose=0
unknown=0
svn=0
bzr=0
git=0
hg=0
for arg in "$@"; do
@mgedmin
mgedmin / update-tcp-ports-html.py
Created January 14, 2012 00:49
Script to convert netstat -tln output into pretty HTML
#!/usr/bin/python
"""
Update TCP port assignments page in /var/www/HOSTNAME/ports/index.html.
"""
import datetime
import optparse
import os
import pwd
import socket
@mgedmin
mgedmin / logpain.ini
Created January 19, 2012 21:35
Python logging insanity
[loggers]
keys = root, foo
[logger_root]
handlers = stdout
level = DEBUG
[logger_foo]
qualname = foo
handlers =
@mgedmin
mgedmin / imageslicer.py
Created January 28, 2012 13:19
Hacky tool to slice a large image into tiles
import sys
from PIL import Image
tile_size = (1024, 1024)
def main():
img = Image.open(sys.argv[1])
tile_w, tile_h = tile_size
img_w, img_h = img.size
@mgedmin
mgedmin / show-all-256-colors.py
Last active August 24, 2023 16:23
Script to show all 256 colors supported by xterms
#!/usr/bin/python
"""Print a swatch using all 256 colors of 256-color-capable terminals."""
__author__ = "Marius Gedminas <marius@gedmin.as>"
__url__ = "https://gist.github.com/mgedmin/2762225"
__version__ = '2.0'
def hrun(start, width, padding=0):
@mgedmin
mgedmin / email_fields.py
Created May 22, 2012 14:12
Custom zope.schema fields for email addresses
class CustomValidationErrorMixin(object):
"""Schema field with a custom validation error message.
Mix this class with your schema fields and specify
which exception they should raise by overriding the
_validation_exception attribute.
"""
_validation_exception = ConstraintNotSatisfied