Help needed with translating perl to python

Greg Armer wiqd at codelounge.org
Tue Jun 26 11:55:30 EDT 2007


On Tue, Jun 26, 2007 at 08:17:06AM -0700, vj wrote:
>I posted too soon:
>
>> Statement 1:
>>   my $today = sprintf("%4s%02s%02s", [localtime()]->[5]+1900,
>> [localtime()]->[4]+1, [localtime()]->[3]) ;
>
>1. is localtime the same as time in python?
You could use this instead

-
from time import localtime
today = localtime()
-

'today' would then contain a tuple:

(2007, 6, 26, 17, 41, 27, 327829)

which you could access in a similar way as above (eg: today[0] == 2007)
obviously the order of the values is different from the perl
counterpart.

>2. What does -> ? do in perl?
'->' references a hash (or dict in python) key. In python it would be localtime()[4]

>3. What is 'my'
'my' declares local data structures (scalars, arrays or hashes) when 'use strict;' 
is defined in the perl script.

>
>> Statement 2:
>>   my $password = md5_hex("$today$username") ;
>
>is md5_hex the same as md5.new(key).hexdigest() in python?
Yes it is.

>
>> $msglen = bcdlen(length($msg)) ;
>
>1. here the funciton is being called with the length of variable msg.
>However the function def below does not have any args
>
>> sub bcdlen {
>>   my $strlen = sprintf("%04s", shift) ;
>>   my $firstval = substr($strlen, 2, 1)*16 + substr($strlen, 3, 1) ;
>>   my $lastval  = substr($strlen, 0, 1)*16 + substr($strlen, 1, 1) ;
>>   return chr($firstval) . chr($lastval) ;
>>
>> }
>
>2. What does shift do above?
'shift' accesses the first argument passed to the function, in this case
the value of length($msg)

>3. is the '.' operator just + in python?
In principle yes.

-- 
Greg Armer
wiqd at codelounge.org
http://www.codelounge.org

If it would be cheaper to repair the old one, the
company will insist on the latest model.



More information about the Python-list mailing list