[FAQTS] Python Knowledge Base Update -- June 27th, 2000

Fiona Czuczman fiona at sitegnome.com
Tue Jun 27 09:22:08 EDT 2000


Hi Guys!

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

Cheers, Fiona


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


-------------------------------------------------------------
Client side cookie
http://www.faqts.com/knowledge-base/view.phtml/aid/4012
-------------------------------------------------------------
Feng Tian



-------------------------------------------------------------
Where can I find informations about the Gnuplot module?
http://www.faqts.com/knowledge-base/view.phtml/aid/4025
-------------------------------------------------------------
Thomas Leclercq



-------------------------------------------------------------
can i make something like jtable with tkinter?
http://www.faqts.com/knowledge-base/view.phtml/aid/4019
-------------------------------------------------------------
udo apolloner



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


-------------------------------------------------------------
Is there a way to differentiate between WinNT and Win9x using Python?
http://www.faqts.com/knowledge-base/view.phtml/aid/4016
-------------------------------------------------------------
Fiona Czuczman
Alex Martelli

import win32api
win32api.GetVersionEx()

will return, e.g., the tuple:

(4, 0, 1381, 2, 'Service Pack 5')


The 4th element in the tuple ("2" here) has the specific info you 
require; it's 1 for Win/95 and Win/98, 2 for Win/NT.


-------------------------------------------------------------
Is there any way to control the function that is used during an 'in' search?
http://www.faqts.com/knowledge-base/view.phtml/aid/4020
-------------------------------------------------------------
Fiona Czuczman
Stephen Hansen, Thomas Wouters

I believe its the __contains__ method.

In 1.6, yes. In 1.5.2, overloading 'in' is not possible, except by
overloading __getitem__ -- the 'in' implementation for user classes and 
most builtin types simply __getsitems__ until it finds a match or 
reaches IndexError. In 1.6, it's possible to override it in both and 
provide a more efficient implementation. (See 'Objects/rangeobject.c' 
for an example ;-)


-------------------------------------------------------------
Is there any way to make a wrapper for a string object, like UserDict is a wrapper for the dict type?
http://www.faqts.com/knowledge-base/view.phtml/aid/4021
-------------------------------------------------------------
Fiona Czuczman
Thomas Wouters

There is the UserString module in the 1.6 alphas, and it seems to be 
fairly portable. (It defines all methods that strings have in 1.6, 
whereas they have none in 1.5.2, but that shouldn't bother portability. 
You can't use them unless you use 1.6 strings as well, though, because 
those methods are defined in terms of string methods ;-)

You can get the UserString module by downloading a 1.6 alpha, or by 
grabbing a recent CVS tree, or by browsing the CVS three through 
sourceforge:

http://www.sourceforge.net/cvs/?group_id=5470

The module should at least show that it's *possible* to write a string
wrapper ;-) though without the 1.6 additions to strings & the 
__contains__ overloader it might not be that efficient.


-------------------------------------------------------------
Is there a built in Python function that eliminates duplicates in a list?
http://www.faqts.com/knowledge-base/view.phtml/aid/4028
-------------------------------------------------------------
Fiona Czuczman
Bjorn Pettersen, Aahz Maruch, jerry_spicklemire

def unique(lst):
    d = {}
    for item in lst:
        d[item] = None
    return d.keys()

--------

The Timbot recommends

        d[item] = 1

because there's no lookup involved on 1, so it's fractionally faster.
(Any other constant should probably work the same.)

--------

Just a minor "heads up" for a possible "gotcha".

It looks to me as if this method, though speedy, will return the
cleaned list in a much different order than the original, since the

  dict.keys()

method outputs in the (seemingly random) order of the dicts internal
hash table.

The moral is, if the order of the list members matters, be sure to wait
until after the cleaning step to sort, or at least remember to re-sort!


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


-------------------------------------------------------------
I update a class with 'reload (module)'. How can I update the instances too?
How to change class instances behavior via reload?
http://www.faqts.com/knowledge-base/view.phtml/aid/3347
-------------------------------------------------------------
Fiona Czuczman
Martin von Loewis,Laurent Szyster, Jürgen Hermann

You'd have to find the instance one-by-one. If you manage to get hold
of them, you can set their __class__ attribute to the new class:

>>> class A:
...   def doit(self):print "old"
... 
>>> a=A()
>>> class A:
...   def doit(self):print "new"
... 
>>> a.doit()
old
>>> a.__class__=A
>>> a.doit()
new

--- Alternate ---

You should implement static and dynamic features that you need in 
different modules and use proxy methods.

Like this

-- dtest.py --
def method(self):
    ...

-- test.py --
class test:
    def method(self):
        return dtest.method(self)

And then,

>>> import test, dtest
>>> t = test.test()
>>> reload(dtest)
>>> t.method()

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

Laurent showed you how to do this automatically. Another technique I 
used to DETECT whether the implementation of a class has changed is 
this:

class spam:
    def __init__(self):
        pass

    def checkReload(self):
        return self.__class__ != spam


-------------------------------------------------------------
What is the best and fastest way to scan a whole list of foldersto see if something has been added or not?
http://www.faqts.com/knowledge-base/view.phtml/aid/1326
-------------------------------------------------------------
Nathan Wallace, Aaron Digulla
Darrell

If your talking about NT then give this a try.

    http://www.dorb.com/darrell/win32WorkSvr/makeThumbsDG.py

def main():
    """
    Use events to avoid polling.
    A sleep is used to give most files a chance to finish
    being writen. Sort of a hack and might not be needed.
    The notification event LAST_WRITE seems to trigger
    at the start and end of writing. So a single file
    will trigger this twice.
    """
    hnd=win32file.FindFirstChangeNotification\
        (os.path.abspath('.')+os.sep+'incoming'\
        ,0, winnt.FILE_NOTIFY_CHANGE_LAST_WRITE )

    while 1:
        int = win32event.WaitForSingleObject( hnd, -1)
            # Try to give the file time to finish writing
        time.sleep(2)
        print 'run'
        try:
            passed, failed, skipped = makeThumbNails()
        except:
            if '-d' in sys.argv:
                traceback.print_exc()
            raise ThumbNailException
        win32file.FindNextChangeNotification(hnd)

On Unix, things are a bit more complicated (there is no way to get
notified when a directory is changed). Instead, you have to check
for the modification time (mtime). The mtime of a directory will
change if an item in that directory is added, deleted or renamed.
It will *not* change when an item is modified (for example, if a file
is overwritten or an item is added to a subdirectory).

You will have to use os.path.walk() to recusively scan the directory
tree if you don't have a list of folders to check. Since that can
take a long time, you should use a cache which contains the
names of all directories plus their mtimes (get them with
os.stat (path)[stat.ST_MTIME]). Next time, you should load
that chache and just check and re-read directories which have
changed.


-------------------------------------------------------------
How do I get a file list via an ftp connection?
http://www.faqts.com/knowledge-base/view.phtml/aid/3887
-------------------------------------------------------------
Fiona Czuczman, Kenroy Harrison
Thomas Weholt

Here's a sample from the FTPLIB Library Reference page:

http://www.python.org/doc/current/lib/module-ftplib.html

from ftplib import FTP
ftp = FTP('ftp.python.org')   # connect to host, default port
ftp.login()               # user anonymous, passwd user at hostname
ftp.retrlines('LIST')     # list directory contents
ftp.quit()


The file Tools/scripts/ftpmirror.py in the Python source distribution is
a script that can mirror FTP sites, or portions thereof, using the
ftplib module. It can be used as an extended example that applies this
module.







More information about the Python-list mailing list