dimanche 23 décembre 2007

Controlling Google Earth with Python (API COM)

Google Earth is a powerful and beautiful application. Being able to control the globe, with few lines of Python code, makes you feel like you've got some god-like powers ;)

I've drafted here a short guide to show you how to start controlling Google Earth with Python (through its API COM):
http://docs.google.com/View?docid=dgqhgsgm_933rjw93

samedi 22 décembre 2007

Wrap my head around unicode for Python

Most interesting tutorial I've found: http://boodebr.org/main/python/all-about-python-and-unicode

Keypoints:

  • unicode are "symbols" or "objects" (no fixed computer representation, don't think bytes) and codecs transform them into binary strings (so you can print, store in disk, sent across network...).
  • a unicode string example with some greek characters: unicodeString = u"abc_\u03a0\u03a3\u03a9.txt"
  • you shouldn't 'print' a unicode string without encoding it first (by default Python will encode in ascii which can leads to errors if there are non ascii characters)
  • you can print a unicode "representation": print repr(unicodeString)
  • you encode with the .encode method: binary = unicodeString.encode("utf-8")
  • you can see the binary result like this: print "UTF-8", repr(unicodeString.encode('utf-8'))
  • print "ASCII",unicodeString.encode('ascii','replace') #will replace non-codable characters with '?'
  • from binary to unicode: unicode(utf8_string,'utf-8') # you must specify the encoding if not Python assumes it's ascii
  • once you have a Unicode object, it behaves exactly like a regular string object, so there is no new syntax to learn (other than the \u and \U escapes)
Other links:
* http://www.jorendorff.com/articles/unicode/python.html
* http://evanjones.ca/python-utf8.html
* http://vim.sourceforge.net/tips/tip.php?tip_id=246
* http://farmdev.com/thoughts/23/what-i-thought-i-knew-about-unicode-in-python-amounted-to-nothing/
from my wikinote

dimanche 16 décembre 2007

Iron Python Studio (Python, .Net and the power of Visual Studio for free)

Iron Python Studio is a free IDE for Iron Python (Python for .Net).

You can develop command-line applications, Windows forms and WPF/XAML applications (see the project IronPythonScreenCast (wmv)).

Iron Python Studio has a visual designer for your GUI, code completion, debugger, etc (the usual tools of Visual Studio which makes in particular GUI development so simple).

It can use all the classes of .Net and also use the classic CPython (version 2.4 for now since this first version of the Studio is based on Iron Python 1.1)

More informations on my wiki note

dimanche 20 mai 2007

[Win] Quick access to favorite folders

"Folder Guide" is a handy windows freeware to keep the most common folders you use at your fingertip.

Folder Guide is a free handy utility that provides fast access to your frequently used and favorite folders. It can operate as the part of your context menu in your Windows Explorer.

samedi 3 mars 2007

[Python]: optparse -- powerful command line option parser

I find optparse much better than getopt to get command line options.
The beast is explained here:
http://python.org/doc/lib/module-optparse.html

For example:

from optparse import OptionParser

parser=OptionParser()

parser.add_option("-f", "--file",dest="filename",help="a file to process")
parser.add_option("-r", "--repeat",dest="repeat",help="an integer")
parser.add_option("--dp",dest="directory",help="picture directory")
parser.add_option("-v",help="verbosemode",action="store_true",
dest="verbose",default=True)

(options,args)=parser.parse_args()

print options.filename
print options.repeat
print options.verbose
print options.directory

For flags, 'action' can be "store_true" or "store_false"

[Python]: find files of a certain type in a folder.

To search for .jpg files in the folder "myfoder" you can do:


import os,fnmatch

for fileName in os.listdir ( "myfolder" ):
if fnmatch.fnmatch ( fileName, '*.jpg' ):
print "Found fileName ",fileName

lundi 26 février 2007

Write GPS data on your photos with Exiftool

Exiftool is a Windows/Mac command-line app (or a muti-plateform Perl librairy) to read and write EXIF metadatas on certain file types.

In the following I will use .jpg files from my camera and the Windows .exe version of Exiftool that I will control in a DOS shell.

Want to see some EXIF metadata in your picture ?

$ exiftool mypicture.jpg

Want to extract the precise date and time when this picture was taken ?

$ exiftool -CreateDate mypicture.jpg
( Returns something "2007:02:10 21:19:43")

Want to write your picture was on the Eastern part of Greenwich meridian ?

$ exiftool -GPSLongitudeRef="E" mypicture.jpg

Want to write the exact the longitude value ?

$ exiftool -GPSLongitude="7.422809" mypicture.jpg

Want to write your picture is in the northern latitudes ?

$ exiftool -GPSLatitudeRef="N" mypicture.jpg

and give the exact latitude value ?

$ exiftool -GPSLatitude="48.419973" mypicture.jpg

Obviously you can combine the four tags above in just one line.

You can check everything went well with another exiftool mypicture.jpg (warning: if the app. doesn't understand what to do it didn't return me any error message by default ).

More informations about the GPS tags can be found here.

With this Free Software GPL command-line version it's now easy to integrate geolacalisation capabilities in a Python script for example.

I can upload my pictures on Flickr and they will show on the Flick maps but I still need to use the geocoding bookmarklet if I want to add the geotagged tags and the google maps.

PS: -n option gives you the latitude/longitude in the decimal form when reading these tags
PS: Python also have a nice similar library but I didnt' succeed to write GPS data with it,
http://www.emilas.com/jpeg/

mercredi 21 février 2007

[Python] Callback examples with FTP

From the wikipedia article:

In computer programming, a callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a function defined in a higher-level layer.

Usually, the higher-level code starts by calling a function within the lower-level code passing to it a pointer or handle to another function. While the lower-level function executes, it may call the passed-in function any number of times to perform some subtask. In another scenario, the lower-level function registers the passed-in function as a handler that is to be called asynchronously by the lower-level at a later time in reaction to something.

Two examples with the FTP module of Python:

from ftplib import FTP

ftp = FTP("the_host")
ftp.login('ftpuser', 'ftppass')
ftp.cwd("folder")

# To obtain the list of files in a variable (not just the standard output) :

fileList=[]
command=ftp.retrlines("LIST",fileList.append)

# To download the file :

file = open("myfile.exe", 'wb')
ftp.retrbinary("RETR myfile.exe",file.write)
ftp.quit()
file.close()

mardi 20 février 2007

[Python] How to get the output from os.system() into a variable ?

A quick solution using popen :

import os
result=os.popen('command').read()
print result

where command is your command (expl. on windows: dir,ipconfig,...)

If you expect more than a line and want each word in a list:

import os,string
result=string.split(os.popen('dir/w').read())

Source of this

samedi 17 février 2007

Clipboard and all-in-one system information for windows

Two useful free software to complete my list of windows utilities :

Ditto is a very useful Open Source clipboard:

- history of text copies, images, screenshots
- enable hot keys, network sync, etc

SIW is a freeware which stands for "System Information for Windows"

- no installation required (can run on a USB key)
- hardware informations, temperature sensors, license keys, process, etc, etc.

jeudi 11 janvier 2007

[Python] Generating an html doc for your script(s)

Pydoc
Python comes with a "pydoc" utility. On windows it is lacated in C:\Python2X\Lib (you may want to add this directory to your system path)

To produce an html documentation of a script or module type in a DOS shell (at the level of your script) :

pydoc.py -w nameOfYourScript

Warning:
- don't type the .py extension in the command above
- respect the uppercase and small caps even on windows.
- your script must have some docstrings

The above command produce a nameOfYourScript.html doc with the classes, methods, functions, etc.

Epydoc
For a more powerful and complete alternative I use the excellent Epydoc that you can download here:
http://epydoc.sourceforge.net/

On windows after the installation you can find the epydoc script in C:\Python2X\Scripts (you may want to add this directory to your system path)

To use epydoc just type in a shell in the folder containing your script(s):

epydoc.py scriptNameWithoutPyExtension

This will produce an "html" folder : read the index.html in your browser and enjoy :)

jeudi 4 janvier 2007

Firefox extensions

A selection of useful Firefox extensions (updated 19 Fev 2007):

* Google Toolbar 3 (beta)
* Gutil a "Google Start Menu"
* Super DragAndGo for tabs
* Greasemonkey
* Flashblock
* gTranslate
* Key Scrambler
* Python Sidebar