[FAQTS] Python Knowledge Base Update -- September 19th, 2000

Fiona Czuczman fiona at sitegnome.com
Tue Sep 19 01:59:12 EDT 2000


Hi Guys,

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

cheers,

Fiona


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


-------------------------------------------------------------
How do I get the full pathname of a module?  Let's say I use 'import' to load a module.  How can I find the location of its *.py or *.pyc file?
http://www.faqts.com/knowledge-base/view.phtml/aid/5918
-------------------------------------------------------------
Rolf Freimuth



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


-------------------------------------------------------------
How do I set the default font for a window and all its widgets?
http://www.faqts.com/knowledge-base/view.phtml/aid/5922
-------------------------------------------------------------
Fiona Czuczman
Paul Magwene

You can set application wide defaults very easily in Tkinter.  The
following is from John Grayson's excellent book, Python and Tkinter
programming (you NEED this book if you want to do any really portable
Python GUI work):

-- Create a file called optionDB (or any other name you'd like) in the
same directory as your source code.  This file is a simple text file
specifying defaults (like the .Xdefaults file on X windows). E.g. (taken
directly from Grayson):

*font:          Verdana 10
*Label*font:    Verdana 10 bold
*background:    Gray80
*Entry*background: white


This will set the application wide default to Verdana, 10 pt, except for
lables which will use a bold version of the font.  The other options
should be self explanatory.

Then in your sourcecode do something like:

root = Tk()
root.option_readfile('optionDB')


-------------------------------------------------------------
Python routine to remove blank lines from a text file
http://www.faqts.com/knowledge-base/view.phtml/aid/5924
-------------------------------------------------------------
Fiona Czuczman
Alex Martelli, Tyler Eaves, Jeremy Hylton

out = open(r'C:\bar.txt', 'w')
for line in open(r'c:\foo.txt').readlines():
    if len(line)>1:
        out.write(line)
out.close()

Notes:
    we can use the r'string' notation to let backslashes (\) be
    used in a platform-natural way on Windows, without doubling
    them up (we could also write 'c:/foo.txt', etc);

    we don't need an explicitly named object for the input-file,
    as we just get its list-of-lines in the for statement itself;
    thus, we don't need to close it, either (it is automatically
    closed by the Python runtime as soon as possible & convenient).

----- Alternatively ------

import fileinput
f = open("c:\\bar.txt", "w")
for line in fileinput.input("c:\\foo.txt"):
    if not line.startswith("\n"):
        f.write(line)
f.close()

If I could modify the behavior a bit, I would: (1) make it skip any
non-blank line that only has whitespace and (2) have it edit the file
in place. 

import fileinput
for line in fileinput.input("c:\\foo.txt", inplace=1):
    if line.strip():
        f.write(line)
f.close()


-------------------------------------------------------------
Is there any way to set the initial seed for the Gaussian random number generator function "gauss"?
http://www.faqts.com/knowledge-base/view.phtml/aid/5925
-------------------------------------------------------------
Fiona Czuczman
Frédéric van der Plancke

The seed function is not in module random (too bad) but in
module whrandom:

So you can do:
    import whrandom
    whrandom.seed()
    #or, with up to three integer arguments:
    whrandom.seed(1,2,3)

(whrandom is actually meant to be "hidden implementation
details", but we obviously still need it explicitly.)






More information about the Python-list mailing list