[Tutor] need advice on streamlining code...

Liam Clarke cyresse at gmail.com
Mon Jan 17 22:09:02 CET 2005


Well, if you're looking to extract the IP & mask you could use a
regEx. They're not tooooo bad....

If it's only that line that you're extracting, and it's format doesn't change 

import re
pattern='ifconfig_fxp0="inet (?P<ip>*.?) netmask (?P<mask>*.?)"
reObj=re.compile(pattern, IGNORECASE)

jay = os.popen("grep ifconfig_fxp0 /etc/rc.conf").readlines()

matches=reObj.search(jay[0])

ip = matches.group('ip')
netmask =matches.group('mask') #You can then do your string splits,
whatever now.

HTH

Liam Clarke

regExs, can be your friends. If you KISS.

On Mon, 17 Jan 2005 11:05:59 -0800 (PST), Chad Crabtree
<flaxeater at yahoo.com> wrote:
> I can't really think of a more elegant solution than what you have,
> maybe regex's but I hate those.  You *can* reduce the number of lines
> by
> two, and there was a variable you never used.
> HTH
> Eric L. Howard wrote:
> 
> >The following block of code works, and provides the necessary output
> I'm
> >looking for...but I have a feeling that it's working through sheer
> brute
> >force and could be better:
> >
> >    insideipgrepfd = os.popen("grep ifconfig_fxp0 /etc/rc.conf")
> >    insideipgrep = insideipgrepfd.readlines()
> >
> >
> insideipgrep=os.popen("grep ifconfig_fxp0 /etc/rc.conf").readlines()
> 
> >    insideipfield, insideip =
> string.split(string.strip(insideipgrep[0]), "=")
> >
> >
> insideip = string.split(string.strip(insideipgrep[0]), "=")[1]
> 
> >    insideipsplit = string.split(insideip, " ")
> >    insideipquads = string.split(insideipsplit[1], ".")
> >    insidemaskquads = string.split(insideipsplit[4], ".")
> >
> >
> insideipquads=string.split(string.split(insideip, " ")[1],".")
> insidemaskquads = string.split(string.split(insideip, " ")[4], ".")
> 
> >the line in /etc/rc.conf looks like:
> >
> >ifconfig_fxp0="inet 172.31.2.100 netmask 255.255.255.0"
> >
> >Any and all thoughts/pointers are appreciated.
> >
> >    ~elh
> >
> >
> >
> 
>                 
> __________________________________
> Do you Yahoo!?
> Meet the all-new My Yahoo! - Try it today!
> http://my.yahoo.com 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list