[FAQTS] Python Knowledge Base Update -- September 2nd, 2000

Fiona Czuczman fiona at sitegnome.com
Sat Sep 2 06:47:11 EDT 2000


Hi Guys,

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

cheers,

Fiona Czuczman


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


-------------------------------------------------------------
How to limit the max number of simultaneous conections in a server implemented with asyncore.py
http://www.faqts.com/knowledge-base/view.phtml/aid/5681
-------------------------------------------------------------
Eduardo Roldan



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


-------------------------------------------------------------
Is it possible to determine the name of the currently executing function?
http://www.faqts.com/knowledge-base/view.phtml/aid/5702
-------------------------------------------------------------
Fiona Czuczman
Alex Martelli

Sure -- piece o' cake...:


def caller(n=1):
    from traceback import extract_stack
    stack = extract_stack()
    return stack[-n-2][2]

# Example usage:

def a():
    print "this is", caller(0)
    b()

def b():
    print "and this is",caller(0),
    print "called by",caller()


Expected output by calling a():

this is a
and this is b called by a


-------------------------------------------------------------
What is File-like Object?
http://www.faqts.com/knowledge-base/view.phtml/aid/5703
-------------------------------------------------------------
Fiona Czuczman
Pete Shinners, Will Ware

a file-like object is any object (including the regular file object)
that has the same methods as the file object. this would typically
be the .read() and/or .write() methods.

The reason many modules mention they take a file-like object is
to give the developer more flexibility. Instead of only taking
a file object, they can take any python object with the same set
of methods.

Most of the time you will probably just use a regular file object.
but you can also use many other objects if you want. usual suspects
would be stuff like sys.stdout, stringio, and similar things
(actually, sys.stdout really is a file object)

hopefully that's not too confusing. if you're still not sure,
just use a regular file object and don't sweat it.

(not sure as to your newbie-ness, a file object is returned
from the open() command) :]

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

It's an object that behaves like a file. That is to say,
it implements the methods a file would normally have, such as
read(), readline(), readlines(), write(), flush(), and so forth.
You can find out more about files and their methods in the Python
Tutorial at http://www.python.org/doc/current/tut/tut.html
The part about files is section 7.2.


-------------------------------------------------------------
Can I implement access control in my classes (equivalent to C++'s private, public, protected, etc...)?
http://www.faqts.com/knowledge-base/view.phtml/aid/5705
-------------------------------------------------------------
Fiona Czuczman
Alex Martelli

Naming an item with two leading underscores (and without two
_trailing_ underscores as well) is as close to "private" as Python
comes (the compiler decorates the attribute name with the
classname, so no accidental clash is possible any more).  There
is, AFAIK, no parallel to "protected" (nor any particular need for
it; it is, after all, a feature which Stroustrup deeply regrets
having let himself be talked intro introducing in C++ -- see his
excellent book "Design & Evolution of the C++ Programming
Language").


-------------------------------------------------------------
Is there a python script similar to shelve or pickle that will take an array and convert it to XML code?
http://www.faqts.com/knowledge-base/view.phtml/aid/5706
-------------------------------------------------------------
Fiona Czuczman
Michal Wallace

Problem:

We have several arrays, some are multidimensional arrays, created with
NumPy.  We'd like store these arrays in an XML data structure.  Is there
a python script similar to shelve or pickle that will take an array and
convert it to XML code?  Thanks.

Solution:

The xmlrpc module has routines to convert structures to XML (it's on
www.pythonware.com)

.. But if you want more control over what the XML looks like, you
could always start with something like:


def xmlify(somearray):
    res = "<cell>"
    for item in somearray:
        if type(item)==type([]):
            res = res + xmlify(item)
        else:
            res = res + item
    res = res + "</cell>"
    return res


print '<?xml version="1.0"?>'
print xmlify(yourMultiDimensionalArray)


-------------------------------------------------------------
How can I get a file's permissions?
http://www.faqts.com/knowledge-base/view.phtml/aid/5707
-------------------------------------------------------------
Fiona Czuczman
Fredrik Lundh

Problem:

I have been able to get a file's owner and group IDs using the os.stat 
function but it doesn't seem to give me the file's permissions.

Solution:

the first member of the stat tuple (ST_MODE) contains the permissions, 
as a bitmask.

you can use the functions and macros in the "stat" module to decipher 
them.

    st = os.stat(myfile)
    mode = st[stat.ST_MODE]
    if mode & stat.ST_IREAD:
        print "readable"
    if mode & stat.ST_IWRITE:
        print "writable"
    if mode & stat.ST_IEXEC:
        print "executable"

Problem cont.:

Thanks for your help but I only seem to be able to check whether or not 
the user running the script has "rwx" permissions.  I am looking to find 
the permissions for owner/group/other, preferably in numeric mode.

Solution:

did you try printing the "mode" value?  I'm pretty sure it does contain 
the "numeric mode" you're looking for.
try this:

    st = os.stat(myfile)
    mode = st[stat.ST_MODE]
    print "mode is", octal(mode & 0777)

(looking in the stat module source file may also help; it's in Python's 
Lib directory)


-------------------------------------------------------------
Is there a simple way of displaying an error message dialog box in Tkinter?
http://www.faqts.com/knowledge-base/view.phtml/aid/5708
-------------------------------------------------------------
Fiona Czuczman
Yaniv, Fredrik Lundh

Of-course there is:

from tkMessageBox import *
showerror(<title>, <message>)

-----------

check out the tkMessageBox module, documented here:
http://w1.132.telia.com/~u13208596/tkintrobook/standard-dialogs.htm


-------------------------------------------------------------
How do I do do simple interpolation using NumPy?
http://www.faqts.com/knowledge-base/view.phtml/aid/5709
-------------------------------------------------------------
Fiona Czuczman
Nick Bower

import arrayfns
new_y = arrayfns.interp(y,x,new_x)


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


-------------------------------------------------------------
How to "expire" a web page with Python ?
http://www.faqts.com/knowledge-base/view.phtml/aid/5664
-------------------------------------------------------------
Quynh Nguyen Anh, Fiona Czuczman
Calvelo Daniel

Not sure whether I understand the question, but it looks as if you want 
to

     print """<meta http-equiv="expires" content="some_date">"""

at the beginning (within the <head>er) of the script generating a page.

Still not sure I understand the question and/or/if it is a Python 
question...

HTH anyway.







More information about the Python-list mailing list