PythonWise

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

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.

Thursday, October 09, 2014

Be a Better Developer by Coding in Four Different Types of Lauguages

I like programming languages and find out that every time I learn a new language it improves my coding in the other ones as well. I learn new ways of doing things, different ways of thinking and it's great.

I usually tell new developers they need to write a (small) project in at least four types of languages - mainstream (procedural/OO), functional, logic based and assembly. Each of these types will give you a different way of solving problems and enrich your programming experience by and order of magnitude.

Here are my recommendations for each category.

Main Stream

By "main stream" I mean procedural/OO languages. There are tons of these and its up to what you're working with currently (though it might be a good excuse to learn a new language). I'm a Python expert, but pick anything - Go, JavaScript, Ruby, C, C++, Java, C# ...

(Yeah - I know they differ a lot. But thinking in most of them is probably the same. The main difference will probably be dynamic vs static typing).

Functional

Many choices here as well. Personally I like the Lisp family of languages, mostly Clojure and Scheme but you can check out a Common Lisp implementation (I think SBCL leads the pack currently), Haskell, ML and others.

Logic Based

If you haven't done logic programming - it'll blow your mind! It's a totally different way of thinking. Prolog is the main language, one free implementation is SWI but there are others as well.

Assembly

Learning assembly will give you a better understanding on how computers work and what are the abstractions other programming languages do for you. I recommend picking one that targets the machine you're working on.

Saturday, October 04, 2014

Add That Trailing Comma

Lets say you wrote this simple code and at first things were going well.

Then after a while a friend came in and did a little fix.

But things started falling apart, after a lot of digging in - you find this.

Python will join two string together in this case, not what you wanted. Always leave a trailing comma.

Couple more things:
  • As Dave Cheney pointed out, using this practice has the nice effect that one line change shows as one line change in the diff since you don't have to add a comma to the previous line.
    • Go probably learned from Python and made trailing comma mandatory.
  • You can see more Python "gotchas" here.

Monday, September 29, 2014

draft2gist - Publish draftin.com Documents to gist

I've started playing with draftin.com, so far very nice.

draftin.com lets you publish documents to several sites, and if the site you want to publish to is not on the list - there are WebHooks.

I've written a small AppEngine service that is a WebHook for publishing draftin.com documents to gist. Feel free to use it and let me know if you find any errors.

Below is the server code, rest of the files are here.

Blog Archive