[FAQTS] Python Knowledge Base Update -- August 16th, 2000

Fiona Czuczman fiona at sitegnome.com
Wed Aug 16 07:01:15 EDT 2000


Hi Guys,

The latest entries into http://python.faqts.com

Cheers,

Fiona Czuczman

Reminder/invitation to all python folk living in Melbourne, Australia:

Tomorrow night, Gin Palace, 7:30 


## Unanswered Questions ########################################


-------------------------------------------------------------
How should a sortable sequence be implemented?
http://www.faqts.com/knowledge-base/view.phtml/aid/5367
-------------------------------------------------------------
Charles Hixson



## New Entries #################################################


-------------------------------------------------------------
How can I decompile a python PYC file to see the source?
http://www.faqts.com/knowledge-base/view.phtml/aid/5375
-------------------------------------------------------------
Fiona Czuczman
Thomas Wouters, Erno Kuusela

You cannot. .pyc files are bytecode, and you can only disassemble them 
into a representation of bytecode. That's not as low-level as 
machine-code, but it's not Python code either ! See the 'dis' module on 
how to disassemble.

------------

try decompyle
(<URL:http://www.csr.uvic.ca/~aycock/python/content.html#decompyle>).

note the words of warning on the page:
| It's rather fragile, and dependent on what the Python bytecode
| compiler emits. I'm releasing it for demonstration and amusement
| purposes only.


-------------------------------------------------------------
What is the best (fastest) way to strip new line characters from a string in Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/5376
-------------------------------------------------------------
Fiona Czuczman
Richard Chamberlain, Alex Martelli

Presuming you don't want any space at the end you can use string.strip
so...

import string
p="I line with a newline.\n"
q=string.rstrip(p)

or you could use string.replace

q=string.replace(p,'\n','')

so that would replace the newline character(s) with nothing.

------------------------

If you have to remove more than one kind of character, and particularly 
if you don't know beforehand how many of each kind to remove (so that a 
loop might be needed), I think that, for speed, you can't beat the 
little-known string.translate method.


It's super if you also have to do some char-to-char translation, but,
even if you don't, this works:

# once only...:
import string
identity=string.maketrans('','')

# then, every time you need to eliminate characters:
str=string.translate(str,identity,'\n\r')


-------------------------------------------------------------
"event.widget" and "event.type" return numerical values.  How can I relate these numerical values to the name of the widget?
http://www.faqts.com/knowledge-base/view.phtml/aid/5378
-------------------------------------------------------------
Fiona Czuczman
Matthew Dixon Cowles

event.widget isn't really a numerical value, it just prints that
way. Fredrik Lundh's excellent Introduction to Tkinter explains that
in the event attributes table at (wrapped for line length):

http://www.pythonware.com/library/tkinter/
  introduction/events-and-bindings.htm

So you can do things like:

if event.widget.widgetName=="text":

or:

if event.widget==self.textArea:

or:

event.widget.selection_clear()

As for event.type, I'm sure that you could find a list or figure them
out for yourself, but on the same page Fredrik says:

For portability reasons, you should stick to char, height, width, x,
y, x_root, y_root, and widget unless you know exactly what you're
doing...

And since I never know exactly what I'm doing <0.1 wink>, I follow his
advice carefully. But really, I've never needed the event type in
practice since I've always known what the event was since I specified
it when I bound the callback to that event.


-------------------------------------------------------------
What's the tip to highlight the syntax with python-mode in emacs?
http://www.faqts.com/knowledge-base/view.phtml/aid/5379
-------------------------------------------------------------
Fiona Czuczman
Erik Max Francis

Put something like this in your .emacs file:

(setq auto-mode-alist
      (cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
      (cons '("python" . python-mode)
            interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python hacking mode." t)
(defun my-python-mode-hook ()
  (setq-default indent-tabs-mode nil)
  )
(add-hook 'python-mode-hook 'my-python-mode-hook)

(global-font-lock-mode t)
(setq font-lock-maximum-decoration t)


## Edited Entries ##############################################


-------------------------------------------------------------
I'm new to Python, where should I start?
Can you give me an overview of the Python Documentation?
Do I need books to learn Python?
Is there Python documentation available in languages other than english?
http://www.faqts.com/knowledge-base/view.phtml/aid/1356
-------------------------------------------------------------
Nathan Wallace, Fiona Czuczman
Tom Funk,Simon Brunning,Dparsavand, Phil Austin,Peter Schneider-Kamp,Sami Hangaslammi

The handbook is part of the on-line documentation available at the
Python web site and with the Python installation -- and it's free.

You may not need to go to a bookstore if you peruse the following page:

  http://www.python.org/doc/

The tutorial is quite complete:

  http://www.python.org/doc/current/tut/tut.html

If you work through the tutorial, it should carry you pretty far along
in your quest.  I found it to be *quite* useful.

The Library Reference discusses the modules that ship with Python. 

  http://www.python.org/doc/current/lib/lib.html

The Language Reference is a bit more abstract, and *much* more dry.  
However, it does completely describe the core Python language
constructs, grammar and syntax.   It's often referred to as material for
"Language Lawyers."

  http://www.python.org/doc/current/ref/ref.html

I like the Module Index:

  http://www.python.org/doc/current/modindex.html

It allows you to jump straight to the module of your choice.

If you're using Win32 (as I do), then you may find the MS HTML Help 
version to be useful (it's my favorite).  If you use Win32, you might 
want to check out:

  http://www.orgmf.com.ar/condor/pytstuff.html

Best of all, these very complete works of non-fiction are FREE.... gotta 
love that!  

I own five Python books, but I still find myself referring back to the 
Python documentation regularly.

---

As one newbie to another, I can recommend
<http://www.idi.ntnu.no/~mlh/python/programming.html> for an
introduction to programming, and
<http://www.idi.ntnu.no/~mlh/python/instant.html> for a bit more on
Python. After that, you might want to look at
<http://starship.python.net/crew/amk/grimoire/html/> for a
'cookbook' of useful techniques. If none of these take your fancy,
there are other links at the python site -
<http://www.python.org/doc/Intros.html>.

If you have any questions look at the FAQs -
<http://www.python.org/doc/FAQ.html>

If you follow http://www.pythonlabs.com/ to
http://www.vex.net/parnassus/ and then choose the  Info/Books/Tutorials 
link then Tutorials, you will get quite a few alternatives.

Also, have a look at:

http://www.networkcomputing.com/unixworld/tutorial/005/005.html

http://yhslug.tux.org/obp/thinkCS/thinkCSpy

The current numpy tutorial (supercedes the one that's cited on 
Parassus):

http://numpy.sourceforge.net/

Richard P. Muller's python short course:

http://www.wag.caltech.edu/home/rpm/python_course/

Konrad Hinsen's Python for Science tutorial

http://starship.python.net/crew/hinsen/

------------------

If you're searching documentation in a language other than english, 
check out ->

Non-English Python Resources
http://www.python.org/doc/NonEnglish.html

"Here are links to Python documents in languages other than English, 
including things like Python documentation translations, articles, and 
mailing lists."

Languages listed:

Bulgarian, Esperanto, French, German, Hungarian, Italian, Japanese, 
Korean, Norwegian, Portuguese, Russian, Spanish

---------------

A couple more tut's:

Try these:

http://dmoz.org/Computers/Programming/Languages/Python/Documentation_and
_Tutorials/

 http://www.honors.montana.edu/~jjc/easytut/easytut/


-------------------------------------------------------------
What is the name of the file that contains the color definitions for Tkinter (like 'rosy brown', etc.)?
http://www.faqts.com/knowledge-base/view.phtml/aid/5363
-------------------------------------------------------------
Fiona Czuczman
Richard Chamberlain, John Grayson,Dan Kuchler

I don't think you'll find them anyway in the code, I believe they are an
internal tcl/tk thing, ( although I think the colour names are X
related). In your tools folder you'll find a little application called
pynche which allows you to select colours and it returns the nearest Tk
colour by name. There is a file in there called namedcolors.txt which
lists the colours.

------------------

You'll find the colors defined in tk8.0.n/xlib/X11/xcolors.c.

You can also find a little Python program in the examples for my book 
which displays all the colors. You can find it in the utils subdirectory 
as colors.py You don't have to buy the book to get the examples...

   www.manning.com/Grayson

-----------------

Just for completeness, I think the best place to get the color 
definitions is from the Tk man pages.  The newer releases (Tk 8.4a1 and 
Tk 8.3.2) have a 'colors' manual page which lists all of the color names 
and the associated RGB values.

The manual pages for all versions of tk are online at:

http://dev.scriptics.com/man/

and for this particular question, the web page that lists the colors is:

http://dev.scriptics.com/man/tcl8.3.2/TkCmd/colors.htm







More information about the Python-list mailing list