Pyrate
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!
SQLite: prevent SQL injection
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.
2012 in review
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.
A name generator in Python
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.
Python uses timsort for sorting
TIL that Python uses timsort as its standard sorting algorithm.
Some links:
Python.org Mercurial Repositories
Pycoder’s Weekly archive
http://us4.campaign-archive2.com/home/?u=9735795484d2e4c204da82a29&id=64134e0a27
Another one: http://pycoders.com/archive.html.
Main page: http://pycoders.com/.
Install a package with pip from a github repository
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
Measuring ping latency of a server
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.
You must be logged in to post a comment.