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

Fiona Czuczman fiona at sitegnome.com
Wed Sep 13 10:30:08 EDT 2000


Hi !

It's been a while ... 

Here are the latest entries into http://python.faqts.com

cheers,

Fiona


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


-------------------------------------------------------------
How can I use Python to read a mailslot in Win32?
http://www.faqts.com/knowledge-base/view.phtml/aid/5841
-------------------------------------------------------------
Jon Nicoll



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


-------------------------------------------------------------
Is there a pretty printer which takes AST objects ( or tuples ) and outputs source code?
http://www.faqts.com/knowledge-base/view.phtml/aid/5831
-------------------------------------------------------------
Joe Hosteny, Steve Holden


Some explanation of what "AST objects" are would be helpful...

The pprint module is your friend here.  It will produce readable
object representations, nicely indented if so requested.  The
printable output will be usable Python code.

You should also note that in general the repr() function will try
to produce a Python representation of an object which is legal
input to the interpreter -- this alone may be enough for simple
objects.

>>> tuple = (1, 'banana', 'orange', 3, 4)
>>> repr(tuple)
"(1, 'banana', 'orange', 3, 4)"
>>> import pprint
>>> pp = pprint.PrettyPrinter()
>>> pp.pformat(tuple)
"(1, 'banana', 'orange', 3, 4)"
>>> pp = pprint.PrettyPrinter(width=2, indent=4)
>>> pp.pformat(tuple)
"(   1,\012    'banana',\012    'orange',\012    3,\012    4)"


-------------------------------------------------------------
How do I get the actual error message from a com_error?
http://www.faqts.com/knowledge-base/view.phtml/aid/5847
-------------------------------------------------------------
Fiona Czuczman
Alex Martelli

Use the .args member of the error object.  Example:

>>> import pywintypes
>>> import win32com.client
>>> try: foo=win32com.client.Dispatch("Not.There")
... except pywintypes.com_error, erob: print 'caught'
...
caught
>>> erob
<pywintypes.com_error instance at 14ca800>
>>> print erob
(-2147221005, 'Invalid class string', None, None)
>>> dir(erob)
['args']
>>> erob.args
(-2147221005, 'Invalid class string', None, None)
>>> erob.args[0]
-2147221005
>>> erob.args[1]
'Invalid class string'
>>>


-------------------------------------------------------------
Could anybody tell me the command to build a 2D array?
http://www.faqts.com/knowledge-base/view.phtml/aid/5848
-------------------------------------------------------------
Fiona Czuczman
Alex Martelli

There is no "declaration" involved.  However, Python lists just
don't work this way -- and it's not an issue of 1-d vs 2-d: you
don't just mention a never-before-bound variable, with a [...]
tagged on to it, and have the variable be magically bound to a
freshly created list (arrays, in Python, are quite a different
thing; see the array module, which is part of the standard
Python distribution -- you must "import array", there's no 2D
ever, etc, etc).

The only normal way to bind a variable is an "assignment" to
that variable.  So, to build a list that's probably what you
would call a '1D array', you basically have 2 choices:

-- initially bind a variable to an empty list, then append to it:
-- bind the variable right off to a list of the proper length,
    then assign to each element

I.e., either use the style:
    ar=[]
    for i in range(5):
        ar.append(i)
or:
    ar=[None]*5
    for i in range(5):
        ar[i]=i

Of course, for this particular case you can also simply
    ar=range(5)
or, in Python 2:
    ar=[i for i in range(5)]
i.e. bind the variable to a pre-constructed list in some way
or other (the second form is called 'list comprehension').


So, when you go '2-D', you have the same choices; and you can use a 
different one on each axis, if you wish, so the
possibilities are many.  From the simplest/verbosest...:

arr=[]
for i in range(5):
    arr.append([])
     for j in range(10):
          arr[i].append(i*j)

to the most concise, a nested list comprehension (only in Python 2...!):

arr=[ [i*j for j in range(10)] for i in range(5)]


-------------------------------------------------------------
How can I get the size of a file in python?
http://www.faqts.com/knowledge-base/view.phtml/aid/5850
-------------------------------------------------------------
Fiona Czuczman
Steve Holden, Robert W. Bill

Problem:

i've done:
        self.logfile = open(filename, 'a')
and i'd like to do this next:
        len_log = self.logfile.size()

...is there something comparable?

Solution:

Indeed there is.  You can do:

        import os
        len_log = os.stat(filename)[6]

This functionality is available on both UNIX and Windows.  To be more
nicely symbolic you could extend the example to:

        import os
        from stat import *
        len_log = os.stat(filename)[ST_SIZE]

which makes it a bit more obvious what's going on.

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

However, because you are opening self.logfile for appending,
you can use:
        
        self.logfile = open(filename, "a")
        len_log = self.logfile.tell()







More information about the Python-list mailing list