Tuesday, December 16, 2008

Python 2.6 : ez_setup and setuptools, pylint on Windows

After i installed 2.6 i notice there is no setuptools windows installer in the python.org site.
So to manually install it you need to :-

1. Download this egg file :
http://pypi.python.org/packages/2.6/s/setuptools/

2. Make sure u have download ez_setup.py from
http://peak.telecommunity.com/dist/

3. ez_setup setuptools-0.6c9-py2.6.egg
**
PJ Eby :
Actually, you can just download the source, unzip it, and do "setup.py bdist_wininst" to build your own .exe installer.
**

Now you need to download the pylint for checking /integrating with ur ide.
And again, u notice that u will fail to get it installed via "easy_install pylint", so here
is the manual way.

http://www.logilab.org/view?rql=Any%20X%20WHERE%20X%20eid%20848
http://www.logilab.org/view?rql=Any%20X%20WHERE%20X%20eid%20856
http://www.logilab.org/view?rql=Any%20X%20WHERE%20X%20eid%20857
(all the download links are at the upper right hand corner)

Download and unzip to all the folders, then goto each of them, common, astng, pylint and run :
python setup.py install


Case close.




.py association in Vista/Windows Missing

I was playing  with version 2.6 Python without uninstalling version 2.5. 
And for some reason i realize that when i execute a module without specifying the python.exe, the module could not get the arguments that was passed in.
For eg.

test.py argument1 argument 2
(argument1 and argument2 is missing from the sys.argv)

But
python test.py argument1 argument2 : works

Initially i tried "assoc .py" followed by "ftype Python.File" and the association was CORRECT!
It shows : Python.File="C:\Python26\python.exe" "%1" %*

Later i found out it was because there is still a registry key that is mapping wrongly.
Changing that registry key solves the problem.

Refer to the picture.





Sunday, November 2, 2008

Python Gotchas and Pitfalls for Windows Programmers

The no.1 pitfall i face is the "\r\n" that windows uses for its text "open" file where else in linux it will be "\n". 
Although this might seems obvious or easy to fix by using "open("xxxxx", mode="wb")" when writing a string to a file, its not as simple as it sounds.

This is because many of the functions that is supposed to be ported to Windows will failed to work, one example is the mbox and maildir, if you use the "import mailbox" and attempt to use maildir to store the files, even if you issue a "rb" on an eml, when you store it via :

    dest_mbox = mailbox.Maildir(path + folder, create=True)   
    msgdata = email.message_from_string(emailtext)        
    new_key = dest_mbox.add(msgdata)        
    
The stored eml message contains "\r\r\n". This will then be a problem when you try to parse it via the email module.

Who knows what other python provided modules works this way too...


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

Thursday, July 31, 2008

Py2exe and email.generator, email.blah blah errors

Create this file :-

import email
msg = email.MIMEText.MIMEText("dsafdafdasfA")
print "ok"

Create setup.py like below :

from distutils.core import setup
import py2exe
setup(console=['testmime.py'])


Run python setup.py py2exe and you will get errors

The following modules appear to be missing['email.Generator', 'email.Iterators', 'email.Utils']

And your program will bombed out with errors like ""Import error : No module name text"


To solve this i just added " print self.__name__" under __init__.py of \email package.
And it seems the LazyImporter is confusing py2exe.

Solution ?

import email
import email.mime.text
import email.iterators
import email.generator
import email.utils

Thursday, July 10, 2008

Python : POP3 SSL Gmail Proxy

There was a friend of mine who went to Macau and his company uses Gmail for hosting corporate emails. He has a local mailserver that would want to download from this gmail and it doesnt support POP3 over SSL (port 995).

So once again Python comes to the rescue, 2 hours later, its done. A generic POP3SSL proxy that will allow any mailserver/client to collect mails from gmail via POP3SSL.

Just run : pop3ssl.exe host port (ur local ip and port that u want to use as proxy)
Example : pop3ssl 10.8.0.1 110 (or if ur mailserver is in the same machine, use another port)

Download it here : pop3ssl.zip

Note although its free for use, pls leave a comment here on how this program helped you.