staticmethod in python

Going through byte of python 3, I tried to make sense of staticmethod in the section talking about class and Object variables.

In the Robot class, there is a howMany class method, declared as below

def howMany():
  print("we have {0:d} robots".format(Robot.population))
howMany = staticmethod(howMany)

or using decorator, it will be

@staticmethod
def howMany():
  print("we have {0:d} robots".format(Robot.population))
howMany = staticmethod(howMany)

It was then called by the Robot class,

Robot.howMany()

I couldn’t really understand what is the staticmethod role here though.
According to the Python documentation,

A static method does not receive an implicit first argument.
It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

That explain a little bit more, seems a staticmethod can be called either by class or instance.
Then I try something like this,

class Human:
  def communicate():
    print("I talk")
  def move():
    print("I walk")
  move = staticmethod(move)

So a Human class have two class methods, communicate and move, but move is a staticmethod.

Steve = Human()
Steve.communicate()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: communicate() takes no arguments (1 given)
#communicate() is a class method, when object Steve try to call it, an error message is saying communicate take no arguments, but 1 is given. I guess that is object Steve trying to pass self to communicate()?
Steve.walk()
#It duly print out "I walk"

Seems to me, staticmethod is a way for object to access class method that otherwise wouldn’t be available to them. Is that the correct perception?

First video from ai-class

Yipe, first video from the online ai-class. Just an introduction, but enough to be excited about. :-)

Openshot 1.4

I tried to get Openshot working again. There are suggestion that locking libcairo2 at version 1.8 would fix the problem, but select libcairo and Ctrl + E in Synaptic didn’t give me option to choose and lock it at any particular version. Maybe I miss out some steps. So I figure maybe I should try uninstall and reinstall libcairo, big mistake. After the installation done, it just wouldn’t let me go into the desktop environment.

Maybe there are ways to fix this without reinstalling Ubuntu, but since I will be having problem anyway with Openshot even I fix this issue, I just go for a clean installation instead.

I pull out the 11.04 installation disc put into the CDROM, and at about 2 hours later, I got Ubuntu back, sure enough, installing and running the spanking new Openshot 1.4 without a single problem.

A noob journey on video editing

Due to the really diversified nature of my job ;-) , somehow I ended up being assigned to create a video for someone in higher management for a presentation which gonna happen pretty soon, in 2 weeks time to be exact.

I do have some very basic experience with video editing stuff before, but nothing too complicated really. So what follow is more like a journey into searching for suitable video editing tool from the point of view of a noob.

MovieMaker

Seeing that the only tools that I can use immediately within the office environment is movie maker, so I don’t really have much choice. Hey, it comes bundled with Windows, what can I do :-) . The editing interface is easy enough to start with, dropping in simple effect like fade in fade out, sweeping from the left, fly out, fly in, almost make you feel like working in a PowerPoint environment (that might not necessary be a good thing for a lot of people though)

The problem arise when I try to publish the project into a proper video format after almost half a day of effort, it start to complain that it could not locate some of the source files!! Reason could be some of the files are moved, or renamed while I am working on the project file.

Strange thing is I can still view all my editing in Movie Maker, just couldn’t publish it. If I copy the project file and all the source file I am working with to another computer, it couldn’t even play out in Movie Maker, I think that is even stranger.

Worse is, as I search thru the net, most of the suggestion is that there is no way to fix it, other than redo everything.

Lightwork Beta

I tried the open source Lightwork Beta, the interface look really professional, it packed with lot of advance features, which of course means the interface is a lot more complex, and the learning curve is steeper. But the show stopper for me is the limited choice of video format, especially when it wouldn’t work with flv and mp4.

I am not sure if there are plugins or add-on or any other hacks around for the conversion, or I could actually use another conversion tools(video transcoder like handbrake for example) before using Lightwork Beta to deal with the editing, but then again using yet another tools create another layer of complexity. And giving the short timeline I have for the work, I really don’t wish to spend my time doing that.

 After Effect

Then I think of trying out Adobe After Effect, CS5.5 only work with a 64-bit machine, so I started to download the whooping 1G trial version of After Effect CS4 (for some reason I couldn’t find the download direct from Adobe site, and ended up download the trial from ProDesigntools, here). While I am sure After Effect will handle the tasks without any issues as it is a tool used by a lot of professionals, but eventually I have been turned off by the complexity of After Effect as well.

Looking at all the buttons and menus available, I just couldn’t stop feeling lost. I am still hugely interested in learning about After Effect, but then again this video editing thing, according to the higher management, “should not take up too much of your time”, but still “it has to be completed within week”, and with all the bells and whistles required.

Avidemux and Openshot

That leave me thinking, maybe I should do it at home in Ubuntu since I have some experience working with Avidemux before, a rather simple video editing task I must admit. So I come home and set out to look for good video editing tools.

I couldn’t remember why I didn’t use Avimux, maybe it seems too basic, I read through reviews in Ubuntu Software Center for video editing apps, it seems Openshot get quite some rave reviews. So I quickly installed Openshot, and the reviews are not too far off, pretty easy to use with some nice effect library. While I am busy editing the videos on hand, the higher management come with another order, to have all video in the same view size. So I try frantically looking for the option to adjust view size in Openshot, I am not sure if it is bad luck that all of a sudden Openshot just refuse to start.

I did a brief research on the net, seems that it might be related to some package being updated that break Openshot. Uninstall and install may or may not help, or updating some packages. But then, I decide to try another apps instead.

update, as I wrote, I read that there is already a newer version of Openshot 1.4 available, from the announcement video, it looks great, I will certainly give it a try soon.

Pitivi

Eventually, I tried Pitivi. Pitivi come with even lesser features than Openshot, or MovieMaker. I couldn’t find any effect or transition library around, adding layer or fading in and out video depending on adding in keyframe and adjusting the opacity accordingly, which is surprisingly easy to use after you get the hang of it.

One neat feature in Pitivi is the mini preview sort of thumbnail picture along the timeline, so it give user a better hints on what scene is on which part.

But still, I am looking for way to adjust view size, and then I found in the net that there is an update version of Pitivi around, and there are tons of new features added in, re-sizing viewer being one of them.

To get the updated version, just do this

sudo add-apt-repository ppa:pitivi/stable
sudo apt-get update
sudo apt-get install pitivi

Then you should get the latest stable version, 0.15. So far I only encountered very few problems with editing, and rendering in Pitivi. I got this useful tips for getting the optimal setup for video rendering in Pitivi, from the ubuntuforum, comment from theSuperman

Set Container to MP4
Audio Codec set to Ffmpeg ALAC encoder
Video Codec to Ffmpeg MPEG-4 part 2 encoder
Change the video codec bit rate to “10300000″

I try similar setup with container change to AVI, and overall quite satisfied with the quality.

Summary

For a lighter and simpler, and of course free video editing tools, so far my experience favor Pitivi, it is easy to use, stable, and in the latest version, it come with quite a lot of useful features. I will definitely try out Openshot latest version 1.4 when I have time, hope it is even better than Pitivi, healthy competition will benefit everyone :-) .

Version Control by Example is here finally

Requested a free copy at about a month ago, finally “Version Control by Example” is here.

Thank you so much, Eric. For a while I almost thought that it wouldn’t be coming, or the delivery might be lost somewhere in the process knowing the postal service in my country, ;-)

destroy every record in a table in rails console

This is really useful, as I keep googling it everytime I wanna do this with the rails console…

Part.find(:all).each{|p|p.destroy}

Got it from the comment section from this post

A much shorter way of doing so, as comment by skim,

Part.destroy_all

Clear screen in python interactive shell mode

Here is how to clear screen when you are working in the python interactive shell mode,

import os
os.system('cls')

A quick google actually yields a few solutions, I find this work best for me.

Redesign 2011

It has been a while I didn’t change anything on the site.
For the past one month or so, I work on this on and off, locally, and finally now, finding the time to get the theme uploaded to the actual site.

You’ll still find plenty of rough edges, in fact rough edges is just an understatement :-) , I think instead of waiting forever to get the redesign done, I will just upload it, and working on it at the side.

First thing you should notice is of course the use of font from typekit, it is lobster for the title, and Adelle for the rest.

So now I got to go back to work and improve it one small bit after another.

Exception handling in python

As I came across the exception handling chapter, both code below should work as stated in the book (The Head First Python book, which is indented for Python 3.X, but I am running this code in Python 2.X),

try:
  with open("man_data.txt") as man_file:
    print >>man_file, man
  with open("other_data") as other_file:
    print >>other_file, other

or

try:
  with open("man_data.txt") as man_file, open("other_data") as other_file:
    print >>man_file, man
    print >>other_file, other

However it seems only the first one is working under Python 2.X, later code will in fact throw up a syntax error message.
Didn’t see anything on the net mentioning this has to do with changes in Python 3.x, so not sure if that is the case.

Print without newline in Python

Going thru the Head First Python book, which is intended for Python 3.X, although majority of the content work just as well in Python 2.X.

First encountered with some variations between version 3.X and 2.X at the chapter about printing out nested lists, with tab indicating different nested level.

To print a tab without a newline, you could do this in Python 3.X,

print("\t",end='')

However, the same code will throw you a syntax error in Python 2.X.
So to do the same in Python 2.X,

print "\t",

Note the final comma, which actually make sure the line will print out with space instead of a newline.
In Python 3.X, print is an actual function but in Python 2.X, print is still a statement.

I found out this from the ever helpful stackoverflow

Older