Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Wednesday, April 21, 2010

How to read PDF of Django Documentation on iPod

This is how I got a pdf of the latest Django Documentation onto my iPod. Of course this would work on your iPhone (iPad?) too. Everything shown here is don in Ubuntu. But the steps shouldn't be much different on other OS, be it Windows or Mac OS X, as long as you have root.

Step 1: get the source.
Good news, Django is Open Source! OK, that's kinda silly. But it's the fundamental reason all this is possible. Anyway, more useful info is that Django uses svn as its version control system and the Doc is part of the source in the repository. So let's check the source out:
svn co http://code.djangoproject.com/svn/django/trunk/
Caution: this is a fairly big repo so the check-out takes quite a bit of time!

Step 2: Compile the documentation
Here's the key to the next piece: Django uses a Python documentation generator called Sphinx to produce the Doc. Sphinx compiles reStructuredText into multiple formats, including HTML, CHM and LaTeX. And the source of the documentation is maintained in reStructuredText format. Our final goal here is to get a pdf, obviously we would generate that from LaTex. So, we need the sphinx-build script to compile the source into LaTeX. For Ubuntu users, the sphinx package in the official repo is called python-sphinx, install it:
sudo apt-get install python-sphinx
This will automatically install several related packages. When it's done, the compile should be smooth:
sphinx-build .../path/to/Doc/source/ .../path/to/the/latex/files

Step 3: Get the PDF
Now it's time to turn the LaTeX source code from last step into a PDF file. I'm a daily LaTeX user on Ubuntu, so for others there might be some additional latex-related packages to install from the official repo. But when I tried running pdflatex command, it complains that two style package were missing. I had to find them online: wrapfig.sty, titlesec.sty. According to Ubuntu Community Documentation, I did the following:

mkdir ~/texmf/tex/latex/titlesec
mv titlesec.sty ~/texmf/tex/latex/titlesec/
mkdir ~/texmf/tex/latex/wrapfig
mv wrapfig.sty ~/texmf/tex/latex/wrapfig/
texhash ~/texmf/

Basically, this creates folders for each package under the home path and informs tex of the added packages.

And now it should compile:
cd .../path/to/latex/files
pdflatex django.tex django.toc

When its done, you should find django.pdf lying in the folder, epic.

Step 4: Put the PDF into the iPod/iPhone(/iPad?)

There are tons of PDF reading apps in App Store. The one I've been using is a free app called Good Reader. It lets you read text, pics and PDF. The reason I'm using it is that it supports some really handy ways to transfer the files on to the mobile devices, including Dropbox.

If you haven't used Dropbox to sync files between your different devices I highly recommend that you follow this link to create a account and start using it immediately. For a starter, it's waaaay more convenient than emailing files as attachments. Anyway, once you get Dropbox folder syncing on your box, drop in djang.pdf there.

Open Good Read on your iPod/iPhone, go to Web Downloads -> Connect to Server -> Create New Connection to -> Dropbox. Fill in your username and password, hit "add". Go back to Connect to Server, click the the newly added account to navigate to the Dropbox folder, find django.pdf and download it.

And we are done, enjoy your mobile, beautiful copy of Django Documentation!

Saturday, January 2, 2010

Python Wrapper of Google Language API with GAE Support

Code is hosted here.

Licensed under MIT license.

Usage:

>>> from glanguage import Translator
>>> t = Translator()
>>> t.translate('Hello, World', lang_from='en', lang_to='fr')
'Bonjour tout le monde'
>>> what = t.detect('你好')
>>> what.lang
'Chinese_simplified'
>>> what.lang_code
'zh-CN'
>>> print what
<language: (chinese_simplified)="" zh-cn="">
>>>

If the value of lang_from in the translate() method is omitted,
the language of the original text will be detected automatically.
The value of lang_to is default to 'en'.

Support for Google App Engine is added. The usage stays the same
on GAE. I also did some simplification to the original code
found online.

A lot credit goes to Peteris Krumins (peter@catonmat.net), who
wrote the code originally.

Thursday, January 8, 2009

How to get the reference of a global class by name in Python

1 Useing Globals().
Globals()['classname']

2 Using eval.
eval('classname')


3 Using new-style classes,make up a base class that has a classmethod, something like this:
class Parent(object):
    @classmethod
    def getSubClass(cls,name):
        for c in cls.__subclasses__:
            if c.__name__ == name:
            return c

Everything classes inherits this one, which will enable it possible to dynamically get the references of the sub-classes.

4 Using getattr(), if the module that the the class lives in is known.
getattr(module,classname)

Wednesday, December 17, 2008

Google App Engine's real indexing limit

5000, that the number you are looking for. I tried to fill up a db.ListProperty(int) as much as I could. The SDK stopped working and started complaining about me having too much to be indexed at this point, 5000. So if you have more than 5000 entities to index in a ListProperty, get around. I came up with a solution where you use a db.ListProperty(db.Key) to hold the keys of the actual ListProperty(s) you use to store stuff. And then make some special interface for it so that it loop through its parts automatically. Implementing such kind of interface, including a reading method, a saving method and a deleting method took up about 70 lines of code, empty lines included. And then I have no limits on GAE's indexing any more!

Sunday, July 6, 2008

Launching the default web brower with Python

I found this in standard modules. So I put it here for a record: To open a certain web page(google.com, for example) with the system default web browse programmatically, do the following:
import webbrowser webbrowser.open('http://google.com')
Python didn't let me down this time.....again!

Thursday, July 3, 2008

Using py2exe and wxPython on Windows XP

In my opinion, wxPython is practically the most acceptable platform-crossing GUI toolkit. However neither it nor python are shipped with MS Windows. Since we can't expect our users to have python and wxPython installed, py2exe is no doubt a decent deal to make our python applications standalone. I had some trouble on the first trying it out on Windows XP, so I decided to write the solution here in case someone else encounters the same problem. Let me start at the very beginning. First of all you need to make sure everything needed is installed, for that matter, at least python, wxPython and py2exe must be there in our case. The versions of my installed packages are as following: Python: 2.5.2 py2exe: 0.6.6 for win32 python2.5 wxPython: unicode-2.8.7.1 for python 2.5 Just to be safe, you need to install python before the rest of the two packages. Now, I assume you have already tested & debugged your application into a satisfying status. All you need now is to make it standalone. Another assumption here is you know the path where python is installed.(I will use python25\ and emit the previous part in this essay) To achieve that, you need to write a setup.py script, which takes advantage of the method setup in standard module distutils.core. You can find instructions on py2exe's website for this step. Another good resource to learn it is the samples that come with py2exe, they are under the path Python25\Lib\site-packages\py2exe\samples You may want to refer to the python library reference to learn about distutils.core, but, before you get confused (which I did), I need to point out that py2exe added some additional options to the setup mothed, which will probably be all you'll be using. OK, a simplistic, but generally always working example of the setup.py would be:
from distutils.core import setup import py2exe setup(['MainScript.py']) # Replace MainScript.py with your script
Save the setup.py script in your app dictionary. Next thing is to make sure that you can run python in a command terminal. Which requires the path of python to be added in the Environment Variable PATH: 1 From the desktop, right-click My Computer and click properties. 2 In the System Properties window, click on the Advanced tab. 3 In the Advanced section, click the Environment Variables button. 4 Finally, in the Environment Variables window, highlight the path variable in the Systems Variable section and click edit. Add a semicolon to the end if there isn't one, and then your python path. Open a command prompt(by running cmd), go to your app path, run the setup.py with the argument py2exe:
python setup.py py2exe
A lot of information shows up and then you will find two new dictionary in your app path, here you just need dist. If you wonder what's in there, check out the Official FAQ. But you won't find your exe working, because msvcp71.dll is missing, and changing the name of MSVRP71.DLL won't work. I think this is probably a bug of py2exe. So you need to copy 'Python\Lib\site-packages\wx-2.8-msw-unicode\msvcp71.dll' manually here. That's how I made my wxPython application work so far. Farther information is surely welcomed :).

Wednesday, June 18, 2008

Learn Python On Ubuntu Linux FOR FREE

Dig the repository and you will be able to find some Python caddies. Like this package:
diveintopython
A Python tutorial written by Mark Pilgrim. There's even a Chinese version in the repository:
diveintopython-zh
If you are interested in GUI development, check this out:
python-gtk2-tutorial
Another tutorial on NumPy:
python-numeric-tutorial
What else? Keep digging!:)

Monday, June 2, 2008

Dynamic Indentation Width In Vim

How to auto-indent differently when coding in different programming languages ? I started hacking this problem when I found myself prefer to indent 4 spaces in Python yet 8 for C. As a Vim user, I do this: Of course I put this in my vimrc:
filetype plugin indent on
Then I copy
$VIMRUNTIME/indent/python.vim
which is the original indentation setting file for python, to
~/.vim/indent/
Finally, customize it by adding the following lines in to the new copy:
setlocal shiftwidth=4 setlocal ts=4
The default indentation width in vim is 8 spaces, and when vim detects a Python source file, it automatically changes that to 4 spaces. Problem solved! A hint about $VIMRUNTIME: you can use this variable IN vim, or find the path it stands for by typing
:!ls $VIMRUNTIME
then hit TAB. I'm curious if someone else has solved the same problem. What's your solution in vim? How about other editors? IDE?

Saturday, May 31, 2008

PyGTK and Glade on Windows

It's been told that the PyGTK & Glade is a cross-platform developing combo. And there's a good reason to believe that most folks started it from Linux as I did. Well, now it is time to deliver the combo's platform-crossing feature: let's march on to MS Windows. I tested this on Windows XP and so far everything seemed fine. To set up the developing environment, several packages are needed. I list their names as well as downloading links below. Assuming you haven't installed any of them before, I would suggest that you download them all (don't install yet). Even if you have, the version might not be guaranteed to work with other components. I also need to mention py2exe here. It is not essential for the application development. What it does is "compiling" your script into a executable and collecting other related files required by the script together, so that it will be able to run independently on those windows systems where the packages don't exist. Since you can't expect your users to have installed every packages, it indeed roles significantly in this game. The order of the installation does matter here. First you need to install Gtk+ 2.12.9 Development Environment, this includes GTK+ development environment, libglade and Glade3. However the launcher it creates on your desktop has a little problem: it points to glade-2. You can fix that manually by modify its properties. Since many tutorials about PyGTK & Glade only apply on glade2, you may want to install glade-2. You can decompress the package and copy the files into corresponding path (e.g GTK/bin and GTK/share) if you like. Next, install Python 2.5. And then the rest. This is where the order matters. Because the rest of the packages wouldn't be able to detect the right location to go if python was absent. Now the installation is done. You can open IDLE and try import pygtk, gtk and py2exe to see if everything works. You also should also be able to edit you GUI with Glade now. Some more words on py2exe. You can find a brief tutorial on its usage [here]. And how to combine it with PyGTK [here]. The second link tells you to copy some stuff from GTK to your executable path, that makes the size of you application way more bigger that it was. Try to remove some of the unrelated files in these folders, like Docs, as long as your executable behaves properly.

Thursday, May 29, 2008

Creating a message box dialog with PyGTK and Glade

Being a newbie to Python, GTK and Glade, I found it rather enjoyable developing stuff fast and easily with this language/library/tool combination. All you need to do to get a GUI up and running is to "draw" it with Glade and connect it with pygtk.

What makes it not so perfect is that there's hardly any systematical material online to tell folks how these great things works together. I could only find some separate pieces of information here and there about this, as I wrote before. Fortunately these pieces information are all great. So I highly recommended those absolute beginners to read this essay, to say the least, to get things started.

Here I'm going to show the way to create a message box, which is probably the most (simple?) commonly used dialog in a GUI. It has a OK button and displays a message you passed. Like this:

When you use it, simply create a instance and pass the message (and the title of the dialog if necessary) to it:

1 from msgBox import messageBox
2 messageBox('Guess what? I am a message box!')


How does that look?

First of all, let's draw the message box out. This is really easy:

Open Glade2, find "dialog" in the palette and click it. Choose "Standard Button Layout", "OK". After the dialog shows up, click "label" on the pallette and then click on the blank area in the previous dialog window.

Next, we are going to set up the signals. Thanks to glade, this part becomes so simple in our case that all we need is click the OK button (or, notice that you can always go to View->Show Widget Tree to focus on the widget you are looking for). In the Properties window, choose "Signals" tab, click on the "..." button after "Signal" entry. Find "clicked" and click "OK". Now don't forget to click "Add" afterwards.

Notice that here we are using mostly default names: dialog1,okbutton1,label1,etc. And you can always choose your own.

Now save the project and quit, I name it "msgBox". And you should get a new folder named the same as the project and two files with the name plus suffix .glade and .gladep. If you try to open them with a editor, you will find them just XML files that represent the widget trees and the properties of each widget we created.

Now type in the following script in a editor and save the file as msgBox.py (line number not included, of course), I'll explain it line by line.
msgBox.py

 1 try:
 2         import pygtk
 3         pygtk.require('2.0')
 4         import gtk
 5         import gtk.glade
 6 except:
 7         print 'Install pygtk,libgtk2.0 and libglade2.0'
 8         import os
 9         os.exit(1)
10
11 class messageBox:
12         def __init__(self, lbl_msg = 'Message here',
13                         dlg_title = ''):
14                 self.wTree = gtk.glade.XML('msgbox.glade')
15                 self.dlg = self.wTree.get_widget('dialog1')
16                 self.lbl = self.wTree.get_widget('label1')
17                 self.dlg.set_title(dlg_title)
18                 self.lbl.set_text(lbl_msg)
19                 handlers = { 'on_okbutton1_clicked':self.done }
20                 self.wTree.signal_autoconnect( handlers )
21
22         def done(self,w):
23                 self.dlg.destroy()

Line1-9 imports the modules we need. If you've checked the recommended essays in the beginning, you shouldn't have problems with these lines.

The rest of the code defines a class that calls up our message box dialog.

Line 12-13: We pass two arguments to the __init__ method: the message you are going to display and the title of the the dialog window.

Line 14: this is the highlight of the whole point. The method gtk.glade.XML takes the XML file we created with glade, parses it and creats gtk.glade.XML object( which has the widgets tree and some methods ) accordingly.

Line 15-16 uses the gtk.glade.XML method get_widget to fetch the references of the widgets, whose names are passed as arguments.

Line 17, as you may guess, sets the title of the dialog window to the name we assigned.

Line 18 sets the message.

Line 19-20: remember the signal we set for the OK button? here we bind the signal and its handler in a dictionary and pass it to the method gtk.glade.XML.signal_autoconnect. That means when the GUI releases the signal "on_okbutton1_clicked" (which happens when the OK button is clicked, obviously), the program will call the corresponding method( self.done ).

Line 22-23: defines the method done, which is our handler of the signal "on_onkbutton_clicked". It does one thing: call the destroy method, which will terminates the dialog.

A little summary here: we created the GUI object according to the XML files, set up the message we intended to play and make the "OK" button destroys the dialog.


Now you get msgBox.glade, msgBox.gladep and msgBox.py. Whenever you need a message box in your application, just copy them to your package and take advantage of it as introduced in the beginning. The following is a package of all the files described before as well as a simple example of using the class:

http://code-of-danmarner.googlecode.com/files/msgBox.zip

Monday, May 26, 2008

Resources to learn PyGTK ( Python ) and Glade

I've developed a good interest in the combination of Python and Glade. And it did give me some exciting moments creating GUIs so easily and quickly. I am going to write about it as soon as I feel comfortable playing around this (update: here they are!). Before that, here's a bunch of useful links that I found while digging into it. Great Documentation/Tutorials/Essays on PyGTK's official website: http://www.pygtk.org/ Documentation on Glade's website: http://glade.gnome.org/documentation.html Awesome essays on some awesome blogs: Creating a GUI using PyGTK and Glade Building an Application with PyGTK and Glade A Beginner's Guide to Using pyGTK and Glade A walkthough on how to get glade work on Win32: http://faq.pygtk.org/index.py?req=show&file=faq21.002.htp

Monday, May 19, 2008

A Python illustration of how abstract superclass works

I made this after reading Learning Python 3rd Edtion, Part VI, Page 490.

The picture shows what happens when a instance's method, which is declared in a superclass and implemented later in a subclass, is called.

Wednesday, May 7, 2008

Converting other encodings into Unicode with Python

Recently I need to convert some files encoded with gb2312 into utf-8, here's the way to achieve this in Python:
# Suppose gb2312_line is a string encoded in gb2312,utf8_line is the converting result. utf8_line = gb2312_line.decode('gb2312').encode('utf8')

Thursday, March 13, 2008

Ruby VS Python: The discussion

It turned out that the discussion about ruby & python is quite big nowadays. And here is a deep talk about them, before you click on it, I should warn you that it is VERY LONG : http://lambda-the-ultimate.org/node/1480 And there's even a meeting in Europe called RuPy Have fun programming, everybody!