What is a perl hash in python
Marc 'BlackJack' Rintsch
bj_666 at gmx.net
Fri Jan 12 13:24:43 EST 2007
In <mailman.2653.1168622715.32031.python-list at python.org>, Karyn Williams
wrote:
> I am new to Pyton. I am trying to modify and understand a script someone
> else wrote. I am trying to make sense of the following code snippet. I know
> line 7 would be best coded with regex.
What is line 7 in the snippet?
> I first would like to understand what was coded originally. thelistOut
> looks like a hash to me (I'm more familiar with perl).
It's a list which contains tuples. Each tuple contains an integer and a
list with one string that looks like a pathname.
> Perhaps someone could translate from perl to python for me - not in code
> but just in concept.
Which Perl? You gave us Python!?
> Here is the code. This script is reading the list thelistOut and then
> removing any items in RSMlist and taking the remainder and putting them
> in graphAddressOut with the formatting.
There's nothing removed from `thelistOut`. Names where the
filename/basename without the extension is in `RSMList` are not processed
and added to `outputOut`.
> This is a SAMPLE of what is in the lists referenced below in the loop:
>
>
> thelistOut = [(632,
> ['/usr/local/www/data-dist/mrtg/main/test/172.16.0.23_9.log']), (145,
> ['/usr/local/www/data-dist/mrtg/main/test/172.16.0.23_13.log']), (0,
> ['/usr/local/www/data-dist/mrtg/main/test/172.16.0.23_5.log'])]
>
> RSMList = ['172.16.0.1_1', '172.16.0.1_2', '172.16.0.1_3',
> '172.16.0.1_4', '172.16.0.1_5']
>
>
>
> #--------------------------Loop 1 -------------------------
>
> w = 0
> while w < 45:
The loop looks odd. Is it really a literal 45 here or are all elements of
`thelistOut` processed? Then a for loop over the list if you don't need
`w` for something other than indexing into the list or an `xrange()`
object are much cleaner than using a while loop and updating the counter
manually. That the second element of the tuple seems to be always a list
with one item looks odd too.
> fileOut = string.split(thelistOut[w][1][0],".log")
> fileOutSplitedCommon = string.split(fileOut[0], "main/")
> fileOut2D = string.split(fileOutSplitedCommon[1], "/")
> fileOut = string.split(fileOut[0],"data-dist")
This might be more readable and understandable if `os.path.splitext()` and
`os.path.split()` where used.
> if fileOut2D[1] in RSMList:
> w = w + 1
> continue
Might be cleaner to negate the test and use the remaining code as body of
that ``if`` statement.
> graphAddressOut = tag1 + logUrl + fileOut[1] + extention1 + tag2
> +
> "<b>SWITCH: " + string.swapcase(fileOut2D[0]) + " & nbsp;PORT
> ID: " + fileOut2D[1] + "</b><br>" + imgTitleTag + imgTag1 + logUrl +
> fileOut[1] + extention2 + imgTag2 + tag3 + tag5
> outputOut.append(graphAddressOut)
> strOut = strOut + graphAddressOut
That's an unreadable mess. Better use string formatting.
And last but not least: a hash is called dictionary in Python.
Ciao,
Marc 'BlackJack' Rintsch
More information about the Python-list
mailing list