Monday, August 11, 2008

Pylint On WingIDE Check on Save!

Now lets face it, for a data typeless language like python, its easy to get errors that other languages IDE would have picked up easily. For example, if you were to point "ooo = hellu()" and hellu() is actually a typo, it should have been "hello()". You wont get any error on the IDE until you run it.

This becomes pretty irritating when u have a number of these erorrs occuring when you are running your applications and you wished such flimsy bugs would have been picked by the IDE before you ever ran it.

Solution : Use Pylint.
Install it and goto "tools"->pylint and you will see a window on the right handside with "pylint" as the tab caption. (pls refer to other documentation on where to get and install pylint)

And what you need now is to have Pylint autocheck everytime u save a module so that all modules gets pre-checked and those irritating typos i mentioned earlier and other bugs will get caught.

Here is what you do, open up "editor-extensions.py" found in ur WingIDE installed directory under "scripts" (make a backup of this file first to somewhere else). Add the following lines at the end :-


def _connect_to_savepoint(doc):


def _on_savepoint(val):

if val == 0 :
return

# Get editor and do action
ed = wingapi.gApplication.GetActiveDocument()
if ed == None :
return

wingapi.gApplication.ExecuteCommand('pylint-execute')

connect_id = doc.Connect('save-point', _on_savepoint)


def _savepointinit():
wingapi.gApplication.Connect('document-open', _connect_to_savepoint)
for doc in wingapi.gApplication.GetOpenDocuments():
_connect_to_savepoint(doc)

_savepointinit()



And there you have it! Pylint total integration. Notice that if you open new modules and save it sometimes u get the "tried to execute an unavailable command .internal.gui.save". Just ignore this, the autosave in Pylint makes the IDE notice that you dont need to save the file again.

Thursday, August 7, 2008

Python : Working Directory Woes

Python has unittest that allows you to have a framework for testing your modules.
There is also nosetests. If you are using Wing IDE, there is a built in Test panel.
All is beautiful.

The problem is, if your project uses calls to "locate directory of where your script ran" you're in for a ride. Unlike windows api where u have a definite api to get this, the various ways in python to get the value resides in os.sys.path[0], sys.argv[0] and os.getcwd().

ALL the values you have seen will not get you the values you want, in unix when its run by using python or using the crond or when you run it from another script located elsewhere or in windows when you run it with "unittest, nosetests and even the Wing's Test panel" these values are going to give you a different result.

And then there is the py2exe ....which will give you yet another surprise as the path return is "\dist\library.zip"

Solution :

Temporarily put some guards for tests enviroment and use the following :-

def GetCurrentWorkingDir () :

if "Wing" in sys.argv[0] : return os.sys.path[0] + os.sep
if "nosetests" in sys.argv[0] : return os.sys.path[0] + os.sep

return os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]))) + os.sep