Spent sometime to learn Python for setting up a Django web application framework. Summarize some useful tips for Python programming as well as share a modified program for feature-based homography tracking using OpenCV python wrapper (the sample codes come with OpenCV source has some bugs that cannot be executed)

Tips for Python Programming

1. Useful basic data types - list, tuple and dictionary

list - each one in the list is numbered, starting from zero. You can remove values from the list, and add new values to the end.

cats = ['Tom', 'Snappy', 'Kitty', 'Jessie', 'Chester'] 

tuple - just like lists, but you can’t change their values. The values that you give it first up, are the values that you are stuck with for the rest of the program. Again, each value is numbered starting from zero.

months = ('Jan', 'Feb', 'Mar', 'Apr')

dictionary - similar to what their name suggests. In a dictionary, you have an ‘index’ of words, and for each of them a definition.

phonebook = {'Andrew Parson':8806336, 'Emily Evert':6784346, 'Peter Power':7658344, 'Lewis Lame':1122345}

2. List Comprehensions

A list comprehension is a construct consisting of logic that builds a list containing the values/objects generated by the logic.

e.g. Let’s say we have a list containing the integers 0 through 9. What if we wanted to increment each number and get all the results back in a list? With list comprehension:

>>> data = [x + 1 for x in range(10)] 
data [1,2,3,4,5,6,7,8,9,10]


>>> even_numbers = [x for x in range(10) if x % 2 ==0]  
even_numbers [0,2,4,6,8]

3. Duck-typing

In computer programming with object-oriented programming languages, duck-typing is a style of dynamic typing in which an object’s methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of a specific interface.

4. Functions are First-Class Objects

In Python, you can treat functions as any other object, such as, store them in container, assign them to different variables, pass them in as arguments to functions, and so forth. The only difference is you can execute function objects, meaning you treat them as a function by appending the usual parentheses and arguments.

import datetime class DiaryEntry(models.Model): 
entry = models.TextField();  
date = models.DateField(default=datetime.date.today)

This is tricky. If we had set default to datetime.date.today(), the function would be called at the time the model was defined, which is not what we want. Instead, we pass the function object; Django is aware of this and calls the function at instance-creation time to generate the value for us.

5. *args and **kwargs

In function calls, * means to unpack a tuple and ** means to unpack a dictionary. In function signature, define variable arguments(varargs) using * representing a “tuple” and ** a “dictionary” as a “shopping bag”.

def daily_sales_total(*all_sales):      
total = 0.0     
for each_sale in all_sales:  
total += float(each_sale)
return total 

OpenCV Python

To reinstall ubuntu, opencv, cuda is always a struggle, but also a progress to learn and know better about all of these. Now working in ubuntu 12.04, opencv and cuda 5.5. First time to try OpenCV python wrapper, and to be surprised by its efficiency and the ease to program. However there are some bugs in the official release.

The Bug is noted in the source (RectSelector Class in common.py), however I don’t quite understand why it happens. Instead of using the class, I rewrite a PlanerTracker Class to perform the tracking by mouse clicking.


The source is uploaded here.


Buy Me A Coffee