Affichage des articles dont le libellé est python. Afficher tous les articles
Affichage des articles dont le libellé est python. Afficher tous les articles

mardi 17 août 2010

Py2xe: how to hide your GUI application error box "See the logfile for details"


If for some reasons you need to hide that box while still logging the errors in myapp.exe.log, the following is a quick and dirty solution:


1) Find the file "boot_common.py" in your py2exe module (exemple: c:\python2x\Lib\site-packages\py2exe\boot_common.py) and make a backup copy in case of.

2) find the "def write" method and add a show_dialog parmeter this way:

def write(self, text, alert=sys._MessageBox, fname=sys.executable + '.log', show_dialog=False):


3) modify the following "else" block this way:


if show_dialog==True:
import atexit
atexit.register(alert, 0,"See the logfile '%s' for details" % fname,"Errors occurred")
else:
pass



To come back to the original behavior change to "show_dialog=True" in 2).

More informations on Py2exe error handling here.

dimanche 25 mai 2008

OS X 10.5 Leppard: "express" Python, PIL and WxPython please

Python on OS X 10.5...

It is not recommended to mess-up with the Python 2.5 installed by default, and while wanting to install PIL and WxPython, I discovered and lost myself in the jungle of Python "distros" and their prerequisites (Fink, MacPorts, DarwinPorts, AciveState, MacPython ...)

In a hurry? go to the source: python.org (MacPython)

1) search and install the universal installer for OS X:
http://www.python.org/download/

This new Python will be the default one when you invoke "Python" in a shell.
It's installed in "/Library/Frameworks/Python.framework"
(the original OS X python is in: "/System/Library/Frameworks/Python.framework").
The new libs are in "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/site-packages"

2) download and install PIL, WxPython (or other packages) from:
http://pythonmac.org/packages/py25-fat

More informations about MacPython:
http://wiki.python.org/moin/MacPython

An interesting read about the Python distributions (especially in a scientific context)

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

samedi 3 mars 2007

[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

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

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

lundi 18 décembre 2006

[Python]: Introspection with help()

An article on this subject

Let's say for example I want to integrate a FTP client in my script and I don't want to lose time searching in books or on the web.

Humm, I'm not sure which lib to use ?

>>> help('modules') #gives a list of all the modules

Oh "ftblib" could be useful, lets check

>>> import ftplib
>>> help(ftplib) # gives a description of this module with the classes, etc.

=> the class "FTP" is what I need for the client part

>>> help(ftplib.FTP) # if I just want to see the description of the FTP class

For more informations about 'help' just type help() at the Python prompt.

Another way to find documentation if you are on-line is to use the Python Sidebar in Firefox.

samedi 16 décembre 2006

[Python] How-to: hide the source code when distributing a Python script ?

Create a .pyc file with your script:

>>> import py_compile
>>> py_compile.compile('myscript.py')

This will produce myscript.pyc (c for compiled) that you can distribute instead of the .py (python will run the .pyc).

[Python] How-to: hide the console

Solution 1 :

Rename the .py file in .pyw (w for window)

Solution 2:

Use' pythonw' instead of Python to launch the script.

This second solution is useful if you have a pyc file : create a shortcut and in it open the file with pythonw (the pyc file won't open a console)