Re: [Tutor] need advice on streamlining code...
At a certain time, now past [Jan.17.2005-01:48:34PM -0500], elh@outreachnetworks.com spake thusly: 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() Thanks to all who replied. I got some good ideas from the exchange. ~elh -- Eric L. Howard e l h @ o u t r e a c h n e t w o r k s . c o m www.OutreachNetworks.com313.297.9900 JabberID: [EMAIL PROTECTED] Advocate of the Theocratic Rule ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need advice on streamlining code...
Jacob S. wrote: insideipgrep = insideipgrep.lstrip(ifconfig_fxp0=\inet ) No! The argument to lstrip() is a list of characters, any of which will be stripped! It is *not* a prefix to remove! insideipgrep='ifconfig if 00=_xxx Wow' insideipgrep.lstrip(ifconfig_fxp0=\inet ) 'Wow' You could use insideipgrep = insideipgrep[len(ifconfig_fxp0=\inet ):] One minor suggestion - use embedded underscores or capital letters to make your variable names easier to read - instead of insideipgrep use insideIpGrep or inside_ip_grep. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need advice on streamlining code...
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@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need advice on streamlining code...
I seem to always be the one to suggest this, but -- String methods are better than using the string module because the string module has been ?deprecated? or will be soon. I think that is the word here. So, do this instead. insideipgrepfd = os.popen(grep ifconfig_fxp0 /etc/rc.conf) insideipgrep = insideipgrepfd.readline() ## Says same thing below -- readline() just reads first line insideipfield, insideip = insideipgrep[0].strip().split(=) insideipsplit = insideip.split() insideipquads = insideipsplit[1].split(.) insidemaskquads = insideipsplit[4].split(.) And, heck, I know it wouldn't be that simple, but if the line stays the same but just the numbers change, you can do, insideipgrepfd = os.popen(grep ifconfig_fxp0 /etc/rc.conf) insideipgrep = insideipgrepfd.readlines() ##Wait, if you're just using the first line use insideipgrepfd.readline() insideipgrep = insideipgrep.lstrip(ifconfig_fxp0=\inet ) temp = insideipgrep.split( netmask ) insideipquads = temp[0].split(.) insideipmaskquads = temp[1].split(.) Warning, code just above is not very stable -- if the text of the line changes in anyway it won't work. HTH, Jacob Schmidt 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() insideipfield, insideip = string.split(string.strip(insideipgrep[0]), =) insideipsplit = string.split(insideip, ) insideipquads = string.split(insideipsplit[1], .) insidemaskquads = string.split(insideipsplit[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 -- Eric L. Howard e l h @ o u t r e a c h n e t w o r k s . c o m www.OutreachNetworks.com313.297.9900 JabberID: [EMAIL PROTECTED] Advocate of the Theocratic Rule ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need advice on streamlining code...
yeah, I wasn't sure about that readline/lines thing, cos I'm not sure how popen works. On Mon, 17 Jan 2005 21:38:37 -0500, Jacob S. [EMAIL PROTECTED] wrote: I seem to always be the one to suggest this, but -- String methods are better than using the string module because the string module has been ?deprecated? or will be soon. I think that is the word here. So, do this instead. insideipgrepfd = os.popen(grep ifconfig_fxp0 /etc/rc.conf) insideipgrep = insideipgrepfd.readline() ## Says same thing below -- readline() just reads first line insideipfield, insideip = insideipgrep[0].strip().split(=) insideipsplit = insideip.split() insideipquads = insideipsplit[1].split(.) insidemaskquads = insideipsplit[4].split(.) And, heck, I know it wouldn't be that simple, but if the line stays the same but just the numbers change, you can do, insideipgrepfd = os.popen(grep ifconfig_fxp0 /etc/rc.conf) insideipgrep = insideipgrepfd.readlines() ##Wait, if you're just using the first line use insideipgrepfd.readline() insideipgrep = insideipgrep.lstrip(ifconfig_fxp0=\inet ) temp = insideipgrep.split( netmask ) insideipquads = temp[0].split(.) insideipmaskquads = temp[1].split(.) Warning, code just above is not very stable -- if the text of the line changes in anyway it won't work. HTH, Jacob Schmidt 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() insideipfield, insideip = string.split(string.strip(insideipgrep[0]), =) insideipsplit = string.split(insideip, ) insideipquads = string.split(insideipsplit[1], .) insidemaskquads = string.split(insideipsplit[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 -- Eric L. Howard e l h @ o u t r e a c h n e t w o r k s . c o m www.OutreachNetworks.com313.297.9900 JabberID: [EMAIL PROTECTED] Advocate of the Theocratic Rule ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@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. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor