Archive for the 'Python' Category

Choosing the right (open-source) tools
June 22, 2008

From the announcement of the open source reddit:
There are only five of us who work on reddit; we couldn’t have made this site if it weren’t for a great community of developers. In no particular order, here’s a quick list of the open source products that reddit is built and runs upon: Debian, lighttpd, HAProxy, [...]

How to install Infogami (+ PostgreSQL 8.2) on Ubuntu Gutsy Gibbon (7.10)
February 24, 2008

Step 1: PostgreSQL (8.2)
Installing postgresql (8.2)
sudo apt-get install postgresql-8.2
Setting up the password for postgres’ postgres user
sudo -u postgres psql template1
ALTER USER postgres WITH PASSWORD ‘your-password’;
\q
Configure postgres’ authentication method :
sudo cp /etc/postgresql/8.2/main/pg_hba.conf /etc/postgresql/8.2/main/pg_hba.conf_bak
sudo nano /etc/postgresql/8.2/main/pg_hba.conf
Add the following at the bottom of the file
# TYPE DATABASE USER [...]

The Truth About Python
January 18, 2008

from: Python Is Not Java (dirtSimple)
So, if you don’t feel like you’re at least ten times more productive with Python than Java, chances are good that you’ve been forgetting to use the time machine! (And if you miss your Java IDE, consider the possibility that it’s because your Python program is much more complex than [...]

Python Safe Calculator
December 17, 2007

Web-Safe Arithmetic Expressions Evaluator

import math
import re

whitelist = ‘|’.join(
# oprators, digits
['-', '\+', '/', '\\', '\*', '\^', '\*\*', '\(', '\)', '\d+']
# functions of math module (ex. __xxx__)
+ [f for f in dir(math) if f[:2] != ‘__’])

valid = lambda exp: re.match(whitelist, exp)

>>> [...]

Fibonacci, Generators and Python
December 16, 2007

Python is so beautiful and elegant
See this:
def fib():
a, b = 0, 1
while 1:
yield b
a, b = b, a+b

if __name__ == ‘__main__’:
f = fib()
for [...]

Accumulator Generator
December 13, 2007

Revenge of the Nerds yielded a collection of canonical solutions to the same problem in a number of languages.

The problem: Write a function foo that takes a number n and returns a function that takes a number i, and returns n incremented by i.

Note: (a) that’s number, not integer, (b) that’s incremented by, not plus.

A simple python tcp server
November 8, 2007

# a simple tcp server

import SocketServer

class EchoRequestHandler(SocketServer.BaseRequestHandler ):
def setup(self):
print self.client_address, ‘connected!’
self.request.send(’hi ‘ + str(self.client_address) + ‘\n’)

def handle(self):
data = ‘dummy’
[...]

Python vs. Lisp (Your favorite dish)
October 3, 2007

Using Python is like eating delicate gourmet foods. You know, great and innovative ideas, small portions, cheering up your mind, and tasting of the “Good Life”. Whereas using Lisp is like cooking that food.

Calc start date and end date of a given week.
July 16, 2007

from datetime import date, timedelta

def calc(year, week):
d = date(year,1,1)
d = d - timedelta(d.weekday())
dlt = timedelta(days = (week-1)*7)
return d + dlt, d + dlt + timedelta(days=6)

Numbers are passion, Python is fascinating, I am amused
July 16, 2007

The following 4 lines says it all:
x = 2
for y in range(2,24):
… x= x*x*y
… print x, ‘\n’
Watching the generated numbers running on the screen get me so high that I feel I can start writing poetry.
Try it for yourself

Count lines of code
June 23, 2007

import os, re, sys
from os.path import join

rootdir = ‘/’
filter = ‘.*\.js$|.*\.py$|.*\.html$|.*\.css$|.*\.sql$|.*\.cs$|.*\.cpp$|.*\.c$|.*\.h|.*\.java$’

def usage():
print ‘USAGE: python loc.py /<rootdir>’

def loc(fname):
try:
f = open(fname)
return len(f.readlines())
except:
[...]

What is this blog all about?
August 7, 2006

I am trying to find my way in the open-source world. So far, for almost a decade, I did all kind of projects mostly database-driven applications, all using Microsoft tools running on Microsoft servers and desktops.
Even though I was generally satisfied with my achievements, I coudn’t ignore this growing community of smart people who twist [...]