Category Archives: Python

There is a better way to implement X

One of the code reviews comments that I’ve been seeing recently is “There is a better way to do this” and without further explanation.

By far this is one of the killer comments you can leave to someone. It is not only telling the coder that his code is not the best. but also leaving him with two challenging questions.

  • What is wrong about this implementation?
  • What are those better ways?

My advice to any reviewer who writes such comment, Try to write a more constructive review. One of the goals of code review is to identify the problems in the code and propose better ideas for implementation. so If you see something that is not at its best. Identify why it is not. and what are the better ways to implements it? always give the requestor something to learn, and go home with.

And If you are requesting a review then received this comment, don’t be defensive about your code. just take a deep breath, and ask the 2 questions above.

Where I am ?

Looks like i have stopped blogging for a while. except from some trips and tricks I am keeping for my reference and public users.

well that’s not the case but since the team I am working with grew to around 22 developer, which i believe considered the biggest php team in one company in Egypt, things were going so crazy, lots of projects going, lots of new approaches, new integrations, and i have to be on top of it all the time.

meanwhile i am driven by four ideas for projects that i believe is gonna make a little difference in their business areas, I am prototyping each one of them, jumping from one to other, I launched one of them already which is www.lamptag.com, which is simply a crawler for LAMP resources and related technologies, after less than a month from launching he prototype, the feedback is good, the twitter page reached 100 follower. and the number of clicks on the links is good too.

Soon i will blog about the recent challenged i have encountered in the field, weather they are technically or management related.

Manage your notes / Code snippets with Snippely

It has been a challenge for me to keep tracking code snippets I want to remember, I have always used text files, or sending my self email with the snippet that I feel I am gonna need to look at again in the future, same for notes also.

Today I have found this nice Air App called Snippely, it allow you to manage your notes / code snippets, It is enough for me and better than text files and emails, I believe I am gonna stick to it for now.

Snippely

Only Two options I wish to see in the coming version, to backup / restore the data & to sync the data across many machines, but like I said, I am happy with what I have now.

You can get Snippely from their Google Code page : http://code.google.com/p/snippely/

I hope you will find it as much useful as I did.

How to Add & Subtract time code in Python

I have been developing some tools for the media production lately, i found a neat way to add or subtract certain amount of seconds to a given time code.

for example i have a video that starts from 12:45:05 and i want to add 59 seconds to it and use the new time.

from datetime import datetime
from datetime import timedelta

time = datetime(2009, 1, 1, 12, 45, 05, 0)

diff = timedelta(seconds = 59)

final_time = time + diff

Installing Python & PyGTK on Mac OsX

Recently i have been asked to develop an application that will work on Linux, and Mac os, so i started developing it on my Ubuntu, i chosed Python as the programming language and PyGTK as the GUI for it.

The application worked fine on my Ubuntu, but when i moved it to Mac OSx (Leopard), i had a problem importing the PyGTK module and the program GUI didn’t run. so i tried to install PyGTK from sources with all its dependencies, but after spending a day doing so i ended up with many errors in compiling the dependencies and finding them.

after some search i found out that i can do this using MacPorts.

so here is the steps i followed :

– Install X11 http://guide.macports.org/#installing.x11

– Install Xcode tools http://guide.macports.org/#installing.xcode

– Install MacPorts http://guide.macports.org/#installing.macports

– Make sure the paths are configured in your shell profile http://guide.macports.org/#installing.shell

– Restart your shell, and follow the next commands :

1- Install Python

sudo port -v install python25

2- Install Python select

sudo port -v install python_select

3- Activate our installed python version

sudo python_select python25

4- Install pygtk

sudo port -v install py25-gtk

5- Install some gnome themes and engines

sudo port -v install gnome-themes
sudo port -v install gtk-nodoka-engine
sudo port -v install gtk-smooth-engine
sudo port -v install gtk2-aurora
sudo port -v install gtk2-clearlooks
sudo port -v install gtk2-extra
sudo port -v install gtk2-industrial
sudo port -v install gtk2-murrine

6- Install theme switch

sudo port -v install gtk-theme-switch

7- Select which theme to be used by your applications by running the theme switch

switch2

Now you can have the applications you develop using python and pygtk running smoothly on mac os leopard.

Appcelerator :: Building RIA Apps and Use Cases with ease

Yesterday i was browsing Zend Framework Webinars, and i stopped by the title :

Get “Rich” Quick : Building Ajax-based RIAs with the Zend Framework and Appcelerator

i didn’t hear about the Appcelerator thing before, but the title was really interesting as i use Zend framework extensively and it is a great idea to add some interactivity and UI to my apps.

after watching the webinars, i really liked the idea of being able to build the full prototype and have it working with no server side code, and then in the development phase with some slight changes to the prototype, it becomes the working front end interacting with the server side code with no problems @ all.

i went to try.appcelerator.org and gave it a try on the fly, that got me more interested. so i downloaded the sdk installer for MAC and installed it, the installation was at ease, even creating the projects and testing.

Appcelerator supports what we call “technology driven agile development” and we call that process Interactive Use Cases.

Install mysql support for python on linux ubuntu

to add the MySQL database support on Linux Ubuntu use the following command :

sudo apt-get install python-mysqldb

and to import this module into your scripts use this command :

me2resh@me2resh-desktop:~/Desktop/MySQL-python-1.2.2$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
>>> 

and enjoy the power of MySQL in your python applications 😉