Hi, everybody!
I'm teaching myself Python, and I have no experience in programming apart from some years of shell scripting. So, please bear with me.
These two funktions are part of an administrative script I've set myself as a first lesson, and, as you will see, they're practically the same, except for one variable. So I'd like to weld them together -- but I can't find out how to.
def writeIP(ip): """ IP schreiben """ regex = re.compile('(.*)address(.*)')
This is the only difference between the functions, isn't it?
So, instead of hardwiring 'address' or 'netmask' into the regexp template, you should insert it based on an argument passed to the function. String interpolation works well here: e.g.,
>>> '(.*)%s(.*)' % 'netmask'
'(.*)netmask(.*)'
>>>
confFile = open(networkConf, 'r') conf = confFile.readlines() confFile.close
Note, here you presumably mean confFile.close() i.e., you must supply the parens to call the function.
[snip]
I feel it should be possible to use something like
def writeFile(ip,keyword): ...
Indeed. Use keyword as the argument to the string interpolation >>> regex = re.compile('(.*)%s(.*)' % keyword)
but how would I construct expressions like
netmaskLineNum = conf.index(netmaskLine)
I think these should work unchanged. But it would be easier to read if you changed these names to be neutral to the application e.g., instead of netmaskLine, foundLine
HTH
Michael
-- http://mail.python.org/mailman/listinfo/python-list