Archive

Archive for December, 2012

Pyrate

December 31, 2012 Leave a comment

Today I came across a new word: pyrate, which means a python hacker. I love this word!

Happy New Year everyone. Happy hacking in 2013!

Categories: python Tags: ,

SQLite: prevent SQL injection

December 31, 2012 2 comments

DON’T do this:

cmd = "update people set name='{0}' where id='{1}'".format(name, id)
curs.execute(cmd)

DO this instead:

cmd = "update people set name=? where id=?"
curs.execute(cmd, (name, id))

If you are using MySQL or PostgreSQL, use %s (even for numbers and other non-string values!) and if you are using SQLite, use ?.

Tip from here.

Categories: python Tags: , , ,

2012 in review

December 31, 2012 Leave a comment

The WordPress.com stats helper monkeys prepared a 2012 annual report for this blog.

Here’s an excerpt:

19,000 people fit into the new Barclays Center to see Jay-Z perform. This blog was viewed about 70,000 times in 2012. If it were a concert at the Barclays Center, it would take about 4 sold-out performances for that many people to see it.

Click here to see the complete report.

Categories: python Tags: , ,

A name generator in Python

December 30, 2012 Leave a comment

Problem
Sometimes you need a name for a registration or for a new profile in a game. What name to choose?

Solution
Use a name generator. Here is one in Python: Markov chains name generator in Python.

Categories: python Tags: ,

Python uses timsort for sorting

December 30, 2012 Leave a comment

TIL that Python uses timsort as its standard sorting algorithm.

Some links:

Python.org Mercurial Repositories

December 30, 2012 Leave a comment
Categories: python Tags: , , ,

Pycoder’s Weekly archive

December 28, 2012 Leave a comment
Categories: python Tags: ,

Install a package with pip from a github repository

December 27, 2012 Leave a comment

Problem
You want to install a package from a GitHub repository via pip.

Solution
Let’s take an example. Say you want to install requests whose github address is https://github.com/kennethreitz/requests.

With pip you can istall it like this:

sudo pip install -U https://github.com/kennethreitz/requests/zipball/master
Categories: python Tags: ,

Measuring ping latency of a server

December 27, 2012 Leave a comment

Problem
You have a list of servers (e.g. proxies) and you want to choose the fastest one. How to measure the ping latencies?

Solution
There is a nice Unix command for this called “fping” (sudo apt-get install fping). “Unlike ping, fping is meant to be used in scripts, so its output is designed to be easy to parse.

Example:

$ fping 221.130.199.121 -C 3 -q
221.130.199.121 : 389.08 411.15 411.82

It sends 3 packets and after the colon it prints each response time. If a response time could not be measured then you will see a “-” in its place.

In Python, here is my solution:

import shlex  
from subprocess import Popen, PIPE, STDOUT

def get_simple_cmd_output(cmd, stderr=STDOUT):
    """
    Execute a simple external command and get its output.
    """
    args = shlex.split(cmd)
    return Popen(args, stdout=PIPE, stderr=stderr).communicate()[0]

def get_ping_time(host):
    host = host.split(':')[0]
    cmd = "fping {host} -C 3 -q".format(host=host)
    res = [float(x) for x in process.get_simple_cmd_output(cmd).strip().split(':')[-1].split() if x != '-']
    if len(res) > 0:
        return sum(res) / len(res)
    else:
        return 999999

It calculates the average of the response times. If no response time could be measured then it returns a big value.

I sent this solution to SO too.

Guido leaves Google for Dropbox

December 9, 2012 Leave a comment
Categories: python Tags: , , ,