[FAQTS] Python Knowledge Base Update -- May 18th, 2000

Fiona Czuczman fiona at sitegnome.com
Thu May 18 09:51:35 EDT 2000


Hello All!

I've added another collection of entries into http://python.faqts.com

You'll also notice there are a couple of questions at the top of the 
summary that have been asked in the knowledge base today, feel free to 
answer them :-)

enjoy...

Fiona


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


-------------------------------------------------------------
How do I write a triple-quoted string that ends with a backslash?
http://www.faqts.com/knowledge-base/view.phtml/aid/3021
-------------------------------------------------------------
Beat Bolli



-------------------------------------------------------------
How do I write a raw string (r'...') that ends with a backslash?
http://www.faqts.com/knowledge-base/view.phtml/aid/3022
-------------------------------------------------------------
Beat Bolli



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


-------------------------------------------------------------
How can I find a good list of web hosting companies that support python?
http://www.faqts.com/knowledge-base/view.phtml/aid/3008
-------------------------------------------------------------
Fiona Czuczman
D'Arcy J.M. Cain, Joseph Block

We support python and use it for all our inhouse code.  
http://www.communiweb.net

http://www.vex.net/ fully supports Python.  It is what we use for our
billing system, the owner (me) is the current maintainer for PyGreSQL
and the Vaults of Parnassus are served from here so you can be sure
that it's an integral part of our system and will continue to be
maintained and updated..


-------------------------------------------------------------
Is there a way to launch an OS specific command argument w/ Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/3007
-------------------------------------------------------------
Fiona Czuczman
Darrell Gallion

import os
os.system("/usr/bin/gedit")


-------------------------------------------------------------
How do I place a newline into the win32file.WriteFile (wHandle, string) method?
http://www.faqts.com/knowledge-base/view.phtml/aid/3024
-------------------------------------------------------------
Fiona Czuczman
Robert

For Windows platforms you need to specify "\r\n" for most programs to
recognize a "newline"


-------------------------------------------------------------
Can I modify JPEG compression with PIL?
http://www.faqts.com/knowledge-base/view.phtml/aid/3009
-------------------------------------------------------------
Fiona Czuczman
Fredrik Lundh

im.save("foo.jpg", quality=50)

http://www.pythonware.com/library/pil/handbook/image.htm
http://www.pythonware.com/library/pil/handbook/formats.htm


-------------------------------------------------------------
How does one display images in Python? When I use the .show method it does not seem to work.
http://www.faqts.com/knowledge-base/view.phtml/aid/3011
-------------------------------------------------------------
Fiona Czuczman
Fredrik Lundh

maybe you don't have a BMP viewer installed?  try saving
a file as BMP, and run the "start" command on it from the
MS-DOS window:

    > python
    ...
    >>> import Image
    >>> im = Image.open("...")
    >>> im.save("myfile.bmp")
    >>> [ctrl-Z]
    > start myfile.bmp

if this doesn't work, check the file associations, or hack
Image.py so it uses another default format.


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


-------------------------------------------------------------
Is there any way to FORCE fields to be of string?
http://www.faqts.com/knowledge-base/view.phtml/aid/2852
-------------------------------------------------------------
Fiona Czuczman
Courageous (identity == Joe Kraska?)

Yes:

        str = "myinteger is: " +`i`

This works for any python object, generally, although
sometimes the results aren't pretty. :)


-------------------------------------------------------------
Is there a way to do a large output of text using print?
http://www.faqts.com/knowledge-base/view.phtml/aid/2684
-------------------------------------------------------------
Fiona Czuczman
Fredrik Lundh

use triple-quoted strings:

print """

Content-type: text/html

<html>
  <head>
    <title>
     .....
</html>
"""


-------------------------------------------------------------
Is there any way to use consecutive print statements?
http://www.faqts.com/knowledge-base/view.phtml/aid/2853
-------------------------------------------------------------
Fiona Czuczman
Courageous, Fredrik Lundh, Cliff Crawford

Here's one way:

        print "This is a test of the emergency broadcast system\n" \
          "This is only a test. Had this been an actual\n" \
          "emergency, you would have been given instructions\n" \
          "on where to go and what to do.

Here's another way:

        print "Guten tag! Ich heisse \"%s\", and ich bin von\n" \
          "Dresden. Ich habe %d jaren, und Ich grosse %3.1f\n" \
          "kilogramme. Anschildegung. Meine Deustch ist nicht\n" \
          "so gut." % ( "Josef", 33, 85.3 )

And yet another way:

        print "This is"+" a test"+" of string concatenation, "+ \
              "which can be very expensive."

Note that the % operator isn't just for print. For example, you
do something like:

        str = "Hi, my name is \"%s\"" % "Fred"

And then, how about:

import sys
write = sys.stdout.write

write("sp")
write("am")


-------------------------------------------------------------
What is the use of a heap data structure?
http://www.faqts.com/knowledge-base/view.phtml/aid/2821
-------------------------------------------------------------
Fiona Czuczman
Courageous

A heap priority queue has the property such that the elements
can be inserted and pulled out in a user-defined order in O(log N)
time. So, for example:

heap.push(7)
heap.push(3)
heap.push(9)
heap.push(2)

would be popped()

as 2,3,7,9

Obviously such a data structure is only needed when appending
onto the end all the time isn't possible (otherwise you'd
just use something like a fifo, both pushing and popping in
O(1)).


-------------------------------------------------------------
Is there a heap anywhere in the python modules?
http://www.faqts.com/knowledge-base/view.phtml/aid/2820
-------------------------------------------------------------
Fiona Czuczman
François Pinard

Here is a draft for a simple Heap module.  This one has been tested.  
See the `test()' function near the end to see an example of usage.


"""\
Handle priority heaps.

Heaps are arrays for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for all 
k, counting elements from 0.  For the sake of comparison, unexisting 
elements are considered to be infinite. The interesting property of a 
heap is that a[0] is always its smallest element.
"""

class Heap:

    def __init__(self, compare=cmp):
        """\
Set a new heap.  If COMPARE is given, use it instead of built-in 
comparison.

COMPARE, given two items, should return negative, zero or positive 
depending on the fact the first item compares smaller, equal or greater 
than the second item.
"""
        self.compare = compare
        self.array = []

    def __call__(self):
        """\
A heap instance, when called as a function, return its smallest item.
"""
        return self.array[0]

    def __len__(self):
        """\
Return the number of items in the current heap instance.
"""
        return len(self.array)

    def push(self, item):
        """\
Add ITEM to the current heap instance.
"""
        array = self.array
        compare = self.compare
        array.append(item)
        high = len(array) - 1
        while high > 0:
            low = (high-1)/2
            if compare(array[low], array[high]) <= 0:
                break
            array[low], array[high] = array[high], array[low]
            high = low

    def pop(self):
        """\
Remove and return the smallest item from the current heap instance.
"""
        array = self.array
        compare = self.compare
        item = array[0]
        if len(array) == 1:
            del array[0]
        else:
            array[0] = array.pop()
            low, high = 0, 1
            while high < len(array):
                if ((high+1 < len(array)
                     and compare(array[high], array[high+1]) > 0)):
                    high = high+1
                if compare(array[low], array[high]) <= 0:
                    break
                array[low], array[high] = array[high], array[low]
                low, high = high, 2*high+1
        return item

def test(n=2000):
    heap = Heap()
    for k in range(n-1, -1, -1):
        heap.push(k)
    for k in range(n):
        assert k+len(heap) == n
        assert k == heap.pop()


-------------------------------------------------------------
Is there an example of a TreeWalker implementation, preferably in Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/2851
-------------------------------------------------------------
Fiona Czuczman
Kevin Russell

There's a DOM-walker implementation in the XML-SIG's distribution, which 
you should already have if you've gotten 4DOM to work. Look for 
walker.py in the dom subdirectory.

A year ago I posted some toy examples of how to use it on the XML-SIG
mailing list:

    http://www.python.org/pipermail/xml-sig/1999-February/002546.html

Read the whole thread, since the follow-ups on suggest important
improvements.







More information about the Python-list mailing list