PythonWise

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

Wednesday, December 17, 2014

Calculate Distance in .kmz File

Here's a little script that calculates the distance of a path (series of GPS points) in a .kmz file (which I generate with My Tracks). It combines several elements - Zip file, XML (with namespaces), zip, sum and of course a bit of math.

Sunday, December 07, 2014

The Versatile date Command

*nix systems comes with a date command line utility. Many people use it to view the current time, however there's much more that date can do.

For example we run a daily job to process yesterday data. The job is a Python script that get the date as a paramter in YYYYMMDD format. This translates to one cron line:

@daily /path/to/job.py $(date --date=yesterday +%Y%m%d)

Here are some things you can do with date:


As a bonus, there is also a cal command which displays calender. Note that years are 4 digits so cal 4 14 will diaplay April for the year 14, not 2014.

cal has a handy -w switch that shows the work week as well.

Wednesday, November 26, 2014

Generate QR Code Using Google Charts API

Here's a small utility to generate QR code image using Google Charts API.

Note the hand crafted Python 2/3 support, for more advanced stuff you might want to have a look at six. However for this script I wanted to stay without external dependencies.

Sunday, November 16, 2014

Common Errors

"Experience is the name every one gives to their mistakes."
    - Oscar Wilde

Students are sometimes amazed by how I can tell the cause of exception without looking at the code. This is the result of making many errors in Python myself and also from observing many students doing mistakes. Here's a short list I've compiled of most likely cause of errors:

NameError

  • You forgot to import a module
  • You made a typo

AttributeError: 'NoneType' object has no attribute ...

  • You forgot a return in your function

AttributeError

  • Typo on dot lookup (obj.foo)
  • Object is different type from what you think (str vs int)
  • Object does not implement a dunder method (e.g. __len__)
There's also the 3'rd party didyoumean module, which might be interesting for beginners. It changes the default stack trace to add a hint about what might be the problem.

Monday, November 03, 2014

A Streaming Chart using Flask and flot

I was teaching a course on "Python Analytics" (pandas, scikit-learn, matplotlib ...) and was asked to provide an example of streaming chart - ones that updates periodically. I've showed a couple of examples one by generating image using matplotlib and another with bokeh-server. After a couple of days I remembered another way - using flot to render the chart. Here's a small example using Flask as the web server (the code works both on Python 2 and 3).


Few comments:

  • Don't use debug=True in production :)
  • For simplicity everything is in one file. However for larger application you might want to take the HTML template(s) out
  • Data is in memory, a restart will wipe it out. If you need data persistence - pick a database (shelve, sqlite3 ...)
  • A big shoutout to Continuum Analytics - Anaconda (and conda) has made my life so much easier teaching this workshop.

Tuesday, October 28, 2014

Resolve SSH Host Name

~/.ssh/config let's you give meanigful names to hosts/ips. But sometimes you want the reverse lookup - what's the ip of web1? Here's a little Python script that does that.

EDIT: EAFP > LBYL

Friday, October 24, 2014

Archline Install Steps (on VirtualBox)

My favorite Linux distro for using under VirtualBox is Archlinux with XFCE window manager. It's light, fast and has all the latest shiny new toys (just the way I like it :).

I found myself setting up VMs to try things out and wrote down the steps I use, this is a trimmed down version of the Installation part in the Archlinux beginners guide.

Blog Archive