On 27/11/12 16:49, Dave Wilder wrote:
Hello,
I believe there is a simple answer to this, but I am confused on what
direction I should go that would perform what I wish to do most
efficiently.
** My Environment ***
[root@f5ite ~/tests]$ python
Python 2.7 (r27:82500, Jul 6 2010, 02:54:50)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
[root@f5ite ~/tests]$ uname –a
Linux VM-QA-ITE-03 2.6.32-220.17.1.el6.i686 #1 SMP Tue May 15 22:09:39
BST 2012 i686 i686 i386 GNU/Linux
[root@f5ite ~/tests]$ more /etc/redhat-release
CentOS release 5.8 (Final)
[root@f5ite ~/tests]$
I need to strip down a string to make a list of “auto” and anything
starting w/ “10” and nothing else.
well, if you know the pattern, you could use regex for that.
Or, if it's really simple, a single split and lots of .startswith or
fnmatch calls could also work.
Below is the string I am currently working with.
results
'tmsh list net interface 1.1 media-capa \rbilities\nnet interface 1.1
{\n media-capabilities {\n none\n auto\n 10T-FD\n
10T-HD\n 100TX-FD\n 100TX-HD\n 1000T-FD\n
1000T-HD\n }\n}\n'
I want to chop it up, so I have a list of all media speeds (auto or
“10X” that I can then loop through, so would like to end up with
something like below:
results
‘none’, ‘auto’, ‘10T-FD’…’1000T-HD’
I could do this using a series of “split” commands. However, is there a
better way? For example, can I just eliminate “media-capabilities”, “{“
and “}” right off the bat
and then do a split on “\n”? Should I use “re” matching for this?
I'd say so.
Assuming that is essentially 'auto' and 'starts with 10..T'
import re
media_re = re.compile(r'(auto|10+T-[HF]D)')
instr = """tmsh list net interface 1.1 media-capa \rbilities\nnet
interface 1.1
{\n media-capabilities {\n none\n auto\n 10T-FD\n
10T-HD\n 100TX-FD\n 100TX-HD\n 1000T-FD\n
1000T-HD\n }\n}\n"""
print [ x.strip() for x in instr.split() if media_re.match(x) ]
gives:
['auto', '10T-FD', '10T-HD', '1000T-FD', '1000T-HD']
if you want to include 'none' as well, you'd add it to the regex
I could also just a single split(‘\n’) and then inside the loop ignore
everything that is not “auto” or starts with “10”, but that seems
inefficient.
Any advice on the best way to do this?
I'd use regex for it, but there are numerous other ways to achieve this.
--
Stuart Sears RHCA etc.
"It's today!" said Piglet.
"My favourite day," said Pooh.
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor