If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Tuesday, July 28, 2009

pyhelp

#!/usr/bin/env python

# Show help for Python

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"
__license__ = "BSD"

def main(argv=None):
import sys
from optparse import OptionParser
import webbrowser

argv = argv or sys.argv

parser = OptionParser("%prog [MODULE[.FUNCITON]]")
opts, args = parser.parse_args(argv[1:])

if len(args) not in (0, 1):
parser.error("wrong number of arguments") # Will exit

root = "http://docs.python.org"

if not args:
url = "%s/%s" % (root, "modindex.html")
elif "." in args[0]:
module, function = args[0].split(".")
url = "%s/library/%s.html#%s.%s" % (root, module, module, function)
else:
url = "%s/library/%s.html" % (root, args[0])

webbrowser.open(url)

if __name__ == "__main__":
main()

Tuesday, July 21, 2009

Two gnupg Wrappers

encrypt


#!/bin/bash

# Encrypt input using gnupg and shred it
# THIS DELETES THE ORIGINAL FILE

err=0
for file in $@
do
if [ $file != ${file/.gpg/} ]; then
echo "$file already encrypted"
continue
fi
if [ -d $file ]; then
fname=$file.tar.bz2
tar -cjf $fname $file
find $file -type f -exec shred -u {} \;
rm -r $file
elif [ -f $file ]; then
fname=$file
else
echo "ERROR: Unknown file type $file"
err=1
continue
fi

gpg -r miki -e $fname
if [ $? -ne 0 ]; then
echo "ERROR: can't encrypt"
err=1
continue
fi

shred -u $fname
if [ $? -ne 0 ]; then
echo "ERROR: can't shred"
err=1
continue
fi
done

exit $err



decrypt


#!/bin/bash

# Decrypt a file encrypted with gpg


# Check command line
if [ $# -lt 1 ]; then
echo "usage: `basename $0` FILE[s]"
exit 1
fi

err=0
for file in $@
do
if [ ! -f $file ]; then
echo "$file: Not found"
err=1
continue
fi

# Output to current directory
bfile=`basename $file`
ext=`echo $file | sed -e 's/.*\.\([!.]*\)/\1/'`
if [ "$ext" != "gpg" ] && [ "$ext" != "pgp" ]; then
echo "Cowardly refusing to decrypt a file without gpg/pgp extension"
err=1
continue
fi

outfile=${bfile%.$ext}

echo $file '->' $outfile
gpg -d -o $outfile $file
if [ $? -ne 0 ]; then
echo "error decrypting $file"
err=1
fi
done

exit $err

Friday, July 10, 2009

What's My IP?

#!/usr/bin/env python
'''Find local machine IP (cross platform)'''

from subprocess import Popen, PIPE
from sys import platform
import re

COMMANDS = {
"darwin" : "/sbin/ifconfig",
"linux" : "/sbin/ifconfig",
"linux2" : "/sbin/ifconfig",
"win32" : "ipconfig",
}

def my_ip():
command = COMMANDS.get(platform, "")
assert command, "don't know how to get IP for current platform"

pipe = Popen([command], stdout=PIPE)
pipe.wait()
output = pipe.stdout.read()

for ip in re.findall("(\d+\.\d+\.\d+\.\d+)", output):
if ip.startswith("127.0.0"):
continue
return ip

if __name__ == "__main__":
print my_ip()

Friday, July 03, 2009

Little Genetics


#!/usr/bin/env python
'''Playing with genetic algorithms, see
http://en.wikipedia.org/wiki/Genetic_programming.

The main idea that the "chromosome" represents variables in our algorithm and we
have a fitness function to check how good is it. For each generation we keep the
best and then mutate and crossover some of them.

Since the best chromosomes move from generation to generation, we cache the
fitness function results.

I'm pretty sure I got the basis for this from somewhere on the net, just don't
remeber where :)
'''

from itertools import starmap
from random import random, randint, choice
from sys import stdout

MUTATE_PROBABILITY = 0.1

def mutate_gene(n, range):
if random() > MUTATE_PROBABILITY:
return n

while 1:
# Make sure we mutated something
new = randint(range[0], range[1])
if new != n:
return new

def mutate(chromosome, ranges):
def mutate(gene, range):
return mutate_gene(gene, range)

while 1:
new = tuple(starmap(mutate, zip(chromosome, ranges)))
if new != chromosome:
return new

def crossover(chromosome1, chromosome2):
return tuple(map(choice, zip(chromosome1, chromosome2)))

def make_chromosome(ranges):
return tuple(starmap(randint, ranges))

def breed(population, size, ranges):
new = population[:]
while len(new) < size:
new.append(crossover(choice(population), choice(population)))
new.append(mutate(choice(population), ranges))

return new[:size]

def evaluate(fitness, chromosome, data, cache):
if chromosome not in cache:
cache[chromosome] = fitness(chromosome, data)

return cache[chromosome]

def update_score_cache(population, fitness, data, cache):
for chromosome in population:
if chromosome in cache:
continue
cache[chromosome] = fitness(chromosome, data)

def find_solution(fitness, data, ranges, popsize, nruns, verbose=0):
score_cache = {}
population = [make_chromosome(ranges) for i in range(popsize)]
for generation in xrange(nruns):
update_score_cache(population, fitness, data, score_cache)
population.sort(key=score_cache.get, reverse=1)
if verbose:
best = population[0]
err = score_cache[best]
print "%s: a=%s, b=%s, err=%s" % (generation, best[0], best[1], err)

base = population[:popsize/4]
population = breed(base, popsize, ranges)

population.sort(key=score_cache.get, reverse=1)
return population[0], score_cache[population[0]]

def test(show_graph=1):
'''Try to find a linear equation a*x + b that is closest to log(x)'''
from math import log
xs = range(100)
data = map(lambda i: log(i+1) * 100, xs)
def fitness(chromosome, data):
'''Calculate average error'''
a, b = chromosome
def f(x):
return a * x + b
values = map(f, xs)
diffs = map(lambda i: abs(values[i] - data[i]), xs)

# We want minimal error so return 1/error
return 1 / (sum(diffs) / len(diffs))

# Show a nice plot
(a, b), err = find_solution(fitness, data, ((0, 100), (0, 100)), 10, 100, 1)
print "best: a=%s, b=%s (error=%s)" % (a, b, err)

data2 = map(lambda x: a * x + b, range(100))
if not show_graph:
return

import pylab
l1, l2 = pylab.plot(xs, data, xs, data2)
pylab.legend((l1, l2), ("log(x+1)", "%s * x + %s" % (a, b)))
pylab.show()

if __name__ == "__main__":
test()

Blog Archive