[FAQTS] Python Knowledge Base Update -- June 28th, 2000
Fiona Czuczman
fiona at sitegnome.com
Wed Jun 28 06:27:12 EDT 2000
More information about the Python-list mailing list
Wed Jun 28 06:27:12 EDT 2000
- Previous message (by thread): Complex datastructures in Python
- Next message (by thread): SystemError: bad argument to internal function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi All, Only a couple of entries entered into http://python.faqts.com today. cheers, Fiona ## New Entries ################################################# ------------------------------------------------------------- How can I access a method with variable amount of parameters? http://www.faqts.com/knowledge-base/view.phtml/aid/4048 ------------------------------------------------------------- Fiona Czuczman Remco Gerlich You use apply(). apply(f, args) calls the function f with the arguments given in the tuple 'args'. To make a function that takes multiple arguments, use def my_apply(func, *args): return apply(func, args) (args is a tuple containing the values passed in). To handle keyword arguments as well, use def my_apply(func, *args, **kw_args): return apply(func, args, kw_args) In this case, kw_args is a dictionary with keyword: value pairs. Just play around in the interpreter a bit, it's quite simple but looks complicated the first time you see it... ------------------------------------------------------------- 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 Steve Holden, Steve Nordby,David Goodger, Fredrik Lundh 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") ...
- Previous message (by thread): Complex datastructures in Python
- Next message (by thread): SystemError: bad argument to internal function
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list