What is a perl hash in python
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Fri Jan 12 17:42:47 EST 2007
Karyn Williams a écrit :
> 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. 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 not a hash (the Python type for hashtables is 'dict'), it's a list
of 2-tuples. FWIW, the dict type can accept such a list as an argument
to it's constructor - but then you loose the ordering.
Also, the data structure is somewhat weird, since the second item of
each tuple is always a one-element list.
> Perhaps someone could translate from perl to python
Do you mean "from Python to Perl" ?
> for me - not in code but just in concept.
>
>
> 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.
>
> 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:
>
> 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")
>
> if fileOut2D[1] in RSMList:
> w = w + 1
> continue
> 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
>
> w = w + 1
>
> #--------------------------Loop 1 -------------------------
>
Yuck. This code stinks. Whoever wrote this ought to be shot down. I
refuse to try&clean this mess unless I get payed (and well payed).
More information about the Python-list
mailing list