[Tutor] manipulating a string in python

Dave Angel d at davea.name
Tue Nov 27 18:28:42 CET 2012


On 11/27/2012 11:49 AM, Dave Wilder wrote:

Could you please use text mode in your messages?  Your email editor
produces double-spacing when it converts the html stuff you wrote into
the text this forum supports.

>
>
> 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 at 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 at 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 at f5ite ~/tests]$ more /etc/redhat-release
>
> CentOS release 5.8 (Final)
>
> [root at f5ite ~/tests]$
>
>
Thanks for being so thorough about your environment.
>
>
>
>
> I need to strip down a string to make a list of "auto" and anything starting w/ "10" and nothing else.
>
>
>
> 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'

What is this string supposed to represent?  Is it equivalent to a text
file, so it should be interpreted as lines?  That would at least give a
meaning to "starting w/ "10".  Assuming that, are leading blanks
significant?  If so, i don't see any lines beginning with "10".

> 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'
>

Why is 'none' in the expected results?  It is not 'auto' and it doesn't
begin with '10'.

>
> 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

What are special about "media-capabilities", "{", and "}" ??  Why aren't
you "eliminating" "tmsh" for example?

>
> and then do a split on "\n"?   Should I use "re" matching for this?
>
>
>
> 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?
>
>

Once you have an unambiguous spec, the code should be pretty
straightforward (depends on that spec, of course).

Only then should you worry about inefficient.  Chances are that the code
which generates this string or reads it from a file, or whatever, is
taking more time than the logic you'll need here.  So, for example, it
might be quicker to substitute readlines() for read(), if you're just
getting it from a file.

Another point:  your 'results' display isn't a valid repr() output for
any Python built-in type.  Do i presume you want a list of strings, and
just omitted the square brackets?  And do i presume you don't want any
placeholders in that list for lines not matching the spec?



-- 

DaveA



More information about the Tutor mailing list