[FAQTS] Python Knowledge Base Update -- May 22nd, 2000

Fiona Czuczman fiona at sitegnome.com
Mon May 22 08:17:52 EDT 2000


Hello Monday!

Below are the entries I've entered today into http://python.faqts.com

regards, Fiona Czuczman


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


-------------------------------------------------------------
How do I invoke the 1.6 debugger with my file loaded?
http://www.faqts.com/knowledge-base/view.phtml/aid/3141
-------------------------------------------------------------
Fiona Czuczman
Guido van Rossum

Assuming this is about IDLE: turn on the debugger in the shell's menu,
then run the module using F5 or Ctrl-F5 in the editor window.


-------------------------------------------------------------
How can I open an existing file and truncate it?
http://www.faqts.com/knowledge-base/view.phtml/aid/3143
-------------------------------------------------------------
Fiona Czuczman
David Goodger

Problem:

When I do the following ->

f = open('file', 'w')
f.truncate(3)
f.close()

the file becomes corrupted. It has the wished size, but it is all null
bytes.

Solution:

I assume that you want to open an existing file and truncate it, leaving 
the existing (3 bytes of) data.

>From the Python Library Reference (1.5.2p2), section 2.8, Built-in
Functions:

    open (filename[, mode[, bufsize]])

    Return a new file object ... The first two arguments are the same
    as for stdio's fopen(): ... mode indicates how the file is to be
    opened: 'r' for reading, 'w' for writing (truncating an existing
    file), and 'a' opens it for appending (which on some Unix systems
    means that all writes append to the end of the file, regardless of
    the current seek position).

    Modes 'r+', 'w+' and 'a+' open the file for updating (note that
    'w+' truncates the file). ...

I think your problem may be in your interpretation of the word 
"truncating" under the 'w' mode above. Since Python's open() modes are 
the same as stdio's fopen() in C, the following is relevant. From The C 
Programming Language, Second Edition (Kernighan & Ritchie), Appendix B, 
section B1.1, (<stdio.h>) File Operations:

    FILE *fopen ...
    
    ... Legal values for mode include:
    
        ...
        "w"        create text file for writing; discard previous
                contents if any
        ...
        "w+"    create text file for update; discard previous
                contents if any
        "a+"    append; open or create text file for update, writing
                at end
    
    Update mode permits reading and writing the same file; ...

So in the Python docs, "truncating" is used, whereas in C, it's "discard
previous contents if any". A bit misleading. Basically, modes "w" and 
"w+" both create a *new* file, clobbering/erasing any existing file with 
the same name. So your f.truncate(3) makes perfect sense: it sets the 
file length of a *new* file to 3 bytes, and since it *is* a new file, 
it's reasonable (downright friendly, in my opinion!) to put nulls in 
there.

I think what you want is to use the mode "a+" for a text file, or "a+b" 
for a binary file:

    >>> f1 = open("tmpfile", "w")
    >>> f1.write("123456789")
    >>> f1.close()
    >>> print open("tmpfile", "r").read()
    123456789
    >>> f2 = open("tmpfile", "a+")
    >>> f2.truncate(3)
    >>> f2.close()
    >>> print open("tmpfile", "r").read()
    123


-------------------------------------------------------------
Do COM servers created with PythonCOM support the dual interface?
http://www.faqts.com/knowledge-base/view.phtml/aid/3144
-------------------------------------------------------------
Fiona Czuczman
Mark Hammond

For Python to support a "native", (vtable base) interface, there must be
some C++ support for the interface.  This can be in the core pythoncom
module, or in pythoncom extensions (eg, all the .pyd files in the
win32comext tree)

PythonCOM has support for quite a large number of these native
interfaces - you can browse them by looking in the pythoncom 
dictionaries (browse the pythoncom module from pythonwin).

Any of these interfaces that we do have C++ support for, you can indeed
create dual interface objects.  All you need do is say you support the
interface - IDispatch is handled automatically.


-------------------------------------------------------------
Do PythonCOM servers expose the vtable to the client for use with early-bound IDispatches?
http://www.faqts.com/knowledge-base/view.phtml/aid/3145
-------------------------------------------------------------
Fiona Czuczman
Mark Hammond

if Python has native support for the interface, it does.

The basic rule is - if you create a COM object, and the user of this COM
object does a QI on you, if the QI succeeds, then you do have a dual
interface.  IDispatch and IUnknown (and IDispatchEx) are the only
interfaces guaranteed to succeed a QI - any others are a bonus, and will
indeed be dual interface.


-------------------------------------------------------------
Are dictionary lookups threadsafe?
Can I lookup and retrieve an item in a dictionary while another thread is adding an item?
http://www.faqts.com/knowledge-base/view.phtml/aid/3146
-------------------------------------------------------------
Fiona Czuczman
Thomas Wouters

Python takes great care to make sure all builtin operations are 
threadsafe, and tries to make it as easy as possible to make your own 
extensions threadsafe. As long as you write Python, you're very 
threadsafe, because only one thread can execute Python code. If you 
start writing extentions, in C, or embedding Python in a multi-threaded 
program, things get a little more complicated, though, and you'll have 
to pay attention to where you allow multiple (Python) threads to 
execute.


-------------------------------------------------------------
How can I tokenize a string using more than 1 separator?
http://www.faqts.com/knowledge-base/view.phtml/aid/3140
-------------------------------------------------------------
Fiona Czuczman
David Goodger

use re.split:

>>> import re
>>> s = "one,two;three:four,five;six:seven"
>>> re.split("[,;:]", s)
['one', 'two', 'three', 'four', 'five', 'six', 'seven']
>>> sep = re.compile("[,;:]")
>>> sep.split(s)
['one', 'two', 'three', 'four', 'five', 'six', 'seven']







More information about the Python-list mailing list