[FAQTS] Python Knowledge Base Update -- July 5th, 2000

Fiona Czuczman fiona at sitegnome.com
Wed Jul 5 09:14:23 EDT 2000


Hi !

Another instalment of entries into http://python.faqts.com

cheers,

Fiona

Australian - instalment
American - installment

:-) .. in case you were wondering that I couldn't spell?


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


-------------------------------------------------------------
How do I format my request to get an url/socket when there is a proxy in the middle and Proxy Authentication is required ?
http://www.faqts.com/knowledge-base/view.phtml/aid/4182
-------------------------------------------------------------
Guest



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


-------------------------------------------------------------
How do I return a user's login name who is logged on at a PC?
http://www.faqts.com/knowledge-base/view.phtml/aid/4189
-------------------------------------------------------------
Fiona Czuczman
richard_chamberlain, j vickroy, Ralf Claus

You'll need the win32 extensions - 
http://starship.python.net/crew/mhammond/

import win32api
login=win32api.GetUserName()

--- or ---

On Windows NT:

import os
os.environ['USERNAME']

... provides the information.

I do not believe that either Win 95 or 98 has the 'USERNAME' attribute 
set.

--- or ---

import getpass
username = getpass.getuser()

works on both systems (NT,UNIX), if you need.


-------------------------------------------------------------
Can I use Python to run Internet Explorer and control it via COM?
http://www.faqts.com/knowledge-base/view.phtml/aid/4191
-------------------------------------------------------------
Fiona Czuczman
Roger Upole, Mark Hammond

Here's a small sample of how to navigate to a URL.
>>> import win32com.client
>>> x=win32com.client.Dispatch('InternetExplorer.Application.1')
>>> x.Visible=1
>>> x.Navigate('http://www.python.org')
If you run makepy on Microsoft Internet Controls, the file generated in
Python\win32com\genpy will show all the properties and methods available 
for this object.

This will work fine to control IE.

However, look in "win32com\client\__init__.py", and look for the
"DispatchWithEvents" docstring - this has an example that uses IE to 
both control it, and respond to events (such as URL load completed, etc)


-------------------------------------------------------------
Is there a Python MySQL module for use under Windows?
http://www.faqts.com/knowledge-base/view.phtml/aid/4188
-------------------------------------------------------------
Fiona Czuczman
Stefan Franke

Check out

  ZMySQLDA-1.2.0.tar.gz

on the Zope site (www.zope.org). It contains MySQLdb-0.1.2 with some 
Windows patches already applied. I run it sucessfully with the lately 
GPLed MySQL-2.23.19 on Win98.

All you need to do is to edit Setup.in, adjust the paths to your  local 
MySQL installation, and run compile.py.


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


-------------------------------------------------------------
What is the best way to make multiple replacements of text strings in a file?
http://www.faqts.com/knowledge-base/view.phtml/aid/4049
-------------------------------------------------------------
Fiona Czuczman, Thiébaut Champenier, Steve Holden
Steve Holden, Steve Nordby,David Goodger, Fredrik Lundh,Thiébaut Champenier

You could clean the source up a bit with:

     foobar = ( ('foo1', 'bar1'),
              ('foo2', 'bar2'),
              ('fooN', 'barN') )
     source = open(source_file,'r')
     contents = source.read()
     source.close()

     for foo, bar in foobar:
         contents = replace(contents, foo, bar)
           
     dest = open(dest_file, 'w')
     dest.write(contents10)
     dest.close()

For speedup, you could write the whole loop as a single statement,
but it will get horrible quickly:

    contents = replace(
               replace(
               replace(contents,
               'fooN', 'barN'),
               'foo2', 'bar2'),
               'foo1', 'bar1)

and, of course, ths code is much less easy to maintain.

How about using a dictionary and a loop:

    replaceme = {'foo1': 'bar1', 'foo2': 'bar2', 'foo3': 'bar3'}
    for key in replaceme.keys():
        srch = key
        rplc = replaceme[key]
        contents = string.replace(contents, srch, rplc)

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

Perhaps a bit complex for a newbie, but the most definitive answer I've 
seen so far has been Fredrik Lundh's 2000-04-12 reply to the "Python 
idiom: Multiple search-and-replace" thread:

> Is there a Python feature or standard library API that will get me 
> less Python code spinning inside this loop?   re.multisub or 
> equivalent? 

haven't benchmarked it, but I suspect that this approach is more 
efficient:

...

# based on re-example-5.py

import re
import string

symbol_map = { "foo": "FOO", "bar": "BAR" }

def symbol_replace(match, get=symbol_map.get):
    return get(match.group(1), "")

symbol_pattern = re.compile(
    "(" + string.join(map(re.escape, symbol_map.keys()), "|") + ")"
              )

print symbol_pattern.sub(symbol_replace, "foobarfiebarfoo")

-------------------
Thiébaut Champenier has wriiten a string replacement utility,
which is available for inspection at:  http://tibi.free.fr/repl.py







More information about the Python-list mailing list