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

Fiona Czuczman fiona at sitegnome.com
Mon May 15 09:58:42 EDT 2000


Hi!

Another day, another 5 entries into http://python.faqts.com ...

Thanks to all who have written to me with feedback on how my summaries 
are going and corrections or additional information to content.  Updates 
have been carried out and are displayed at the bottom of this mail.

Of course, don't be afraid to get into faqts and update or add content 
if you feel inclined.

Cheers, Fiona


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


-------------------------------------------------------------
I've installed python 1.6a2 in solaris 5 succefully, but cannot find _tkinter.  import Tkinter complains it cannot find _tkinter.
http://www.faqts.com/knowledge-base/view.phtml/aid/2850
-------------------------------------------------------------
Fiona Czuczman
Fredrik Lundh

See the troubleshooting guide at:
http://www.python.org/topics/tkinter

(and if necessary, check the downloading/installing instructions
again; you might have missed some important detail)


-------------------------------------------------------------
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.


-------------------------------------------------------------
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 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")


-------------------------------------------------------------
Is it possible to create a win32 python distr with COM support which runs "out-of-the-box" with some sort of path-settings and NO registry-settings?
http://www.faqts.com/knowledge-base/view.phtml/aid/2854
-------------------------------------------------------------
Fiona Czuczman
Mark Hammond

If you search dejanews, you will find code from that imports these 
modules at runtime without registry settings.

Use the "keywords" field and enter "pywintypes pythoncom import"
Use the forum field, and enter "comp.lang.python"
Use the author field, and enter mhammond at skippinet.com.au

One of the 2 results should be 
http://x31.deja.com/getdoc.xp?AN=536077963


If you are only calling other COM objects (ie, not running them), then 
it should work fine with pythoncom15.dll in the same directory as the 
app itself...


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


-------------------------------------------------------------
Is there a pre-written script that will provide either a tar+gz or PkZip like functionality on top of ZLIB?
http://www.faqts.com/knowledge-base/view.phtml/aid/2794
-------------------------------------------------------------
Fiona Czuczman
Alexander Gavrilov,Oleg Broytmann, Mike Steed

There should be modules for both formats. Search through Deja.com.

AND

If you can build zlib into the application, can you not also hack in
info-zip as well?  Python it ain't, but it should do what you want.

http://www.info-zip.org/pub/infozip/

AND

A URL for a PkZip-like tool. It is module 'zipfile.py' in 
ftp://ftp.interet.com/pub/pylib.html


-------------------------------------------------------------
What is the Python code that will print primes below 1000?
http://www.faqts.com/knowledge-base/view.phtml/aid/2823
-------------------------------------------------------------
Fiona Czuczman
François Pinard,Emile van Sebille

initial answer:

import re
for n in range(2, 1000):
    if not re.match('(11+)\\1+$', '1'*n):
        print n

and then again:

While the above submission is an interesting
application of regular expressions, it's not really
the way to get a list of primes.  Below is primes.py,
taken from the standard distribution that is faster
by a factor of 10-20:

primesuspects = range(3,1000,2)
ptr = 0
while ptr < len(primesuspects):
  val = primesuspects[ptr]
  for i in primesuspects[ptr+1:]:
    if not i % val:
      primesuspects.remove(i)
  ptr = ptr + 1
print primesuspects


-------------------------------------------------------------
Is there a function in Python for converting Latin-1 to UTF-8?
http://www.faqts.com/knowledge-base/view.phtml/aid/2781
-------------------------------------------------------------
Fiona Czuczman
Fredrik Lundh

Here's one way to do it (well, two ways, actually):

import string, sys

if sys.version[:3] >= "1.6":
    def utf8(str):
        return unicode(str, "iso-8859-1").encode("utf-8")
else:
    # brute force translation from latin 1 to utf 8
    def utf8(str):
        out = []
        append = out.append
        for ch in str:
            if ch < "\200":
                append(ch)
            else:
                ch = ord(ch)
                append(chr(0xc0 | (ch >> 6)))
                append(chr(0x80 | (ch & 0x3f)))
        return string.join(out, "")







More information about the Python-list mailing list