Help needed with translating perl to python

V

vj

I have a perl script which connect to network stream using sockets.
The scripts first logins in to the server and then parses the data
comming from the socket.

Statement 1:
my $today = sprintf("%4s%02s%02s", [localtime()]->[5]+1900,
[localtime()]->[4]+1, [localtime()]->[3]) ;


Statement 2:
my $password = md5_hex("$today$username") ;

Statement group 3:

$msglen = bcdlen(length($msg)) ;

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) ;
}
 
V

vj

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?
2. What does -> ? do in perl?
3. What is 'my'
Statement 2:
my $password = md5_hex("$today$username") ;

is md5_hex the same as md5.new(key).hexdigest() in python?
$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?
3. is the '.' operator just + in python?

Thanks,

Vineet
 
J

Jay Loden

vj said:
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?

http://perldoc.perl.org/functions/localtime.html

It's more like time.localtime()

One key thing you'll notice here is the adding of 1900 - the year returned by Perl's localtime is 'number of years since 1900' so in order to convert it to the actual year you have to add 1900.
2. What does -> ? do in perl?

In this case, it's accessing localtime similar to something like localtime[5]. -> can basically be considered similar to dotted notation in Python, used to access items in a container object.
3. What is 'my'

http://perldoc.perl.org/functions/my.html

It's a way of declaring a local variable in Perl.
is md5_hex the same as md5.new(key).hexdigest() in python?
http://www.xav.com/perl/site/lib/Digest/MD5.html#functions


1. here the funciton is being called with the length of variable msg.
However the function def below does not have any args

Perl subroutines (functions) can be declared without any arguments if desired, and then you can use 'shift' to access any arguments. So:

sub printMe {
my $arg = shift;
print $arg;
}

would print the first argument passed to it, e.g. printMe("foo"); would output foo.
2. What does shift do above?

http://perldoc.perl.org/functions/shift.html

See above, it's used for accessing the first value of an array, in this case an arry of arguments to a subroutine.
3. is the '.' operator just + in python?

'.' operator is used for string concatenation in Perl, so + would be the equivalent in Python, yes.

-Jay
 
G

Greg Armer

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.
is md5_hex the same as md5.new(key).hexdigest() in python?
Yes it is.
1. here the funciton is being called with the length of variable msg.
However the function def below does not have any args


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
(e-mail address removed)
http://www.codelounge.org

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

attn.steven.kuo

I have a perl script which connect to network stream using sockets.
The scripts first logins in to the server and then parses the data
comming from the socket.

Statement 1:
my $today = sprintf("%4s%02s%02s", [localtime()]->[5]+1900,
[localtime()]->[4]+1, [localtime()]->[3]) ;


Perl has "Do What I Mean" features that allow you to
treat strings and number interchangeably. Python's
time.localtime returns a tuple of integers so you'll
have to use the proper format conversion characters:

import time
today = "%04d%02d%02d" % time.localtime()[0:3]

# No need to add offsets of 1900 and 1 because Python
# does this for you


Statement 2:
my $password = md5_hex("$today$username") ;


You should have added that md5_hex is comes from
Digest::MD5, not a core Perl module. Regardless:


import md5
password = md5.new("%s%s" % (today, username)).hexdigest()

# seems to be what you wanted


Statement group 3:

$msglen = bcdlen(length($msg)) ;

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

}




You can have a variadic function in Python but the parameters
are passed via a tuple. Because a tuple is immutable, one cannot
"shift" elements out of a tuple. Here I've used the first parameter
via selection by index. Perl's substr is replaced by slice notation;
chr is, well, chr. Concatenation (Perl's '.' operator) is replaced
by string formatting:
.... strlen = "%04s" % str(args[0])
.... firstval = int(strlen[2:3]) * 16 + int(strlen[3:4])
.... lastval = int(strlen[0:1]) * 16 + int(strlen[1:2])
.... return "%s%s" % (chr(firstval), chr(lastval))
....'FE'
 
A

attn.steven.kuo

On Jun 26, 8:59 am, (e-mail address removed) wrote:

(snipped)
... strlen = "%04s" % str(args[0])
... firstval = int(strlen[2:3]) * 16 + int(strlen[3:4])
... lastval = int(strlen[0:1]) * 16 + int(strlen[1:2])
... return "%s%s" % (chr(firstval), chr(lastval))
...>>> bcdlen(4546)

'FE'


Let me add that instead of an an-close-as-possible translation
from the original Perl code, one can rewrite this as:
.... strlen = "%04s" % length
.... return chr(int(strlen[2:4], 16)) + chr(int(strlen[0:2], 16))


which is more "Pythonic" to me.
 
D

Duncan Booth

Let me add that instead of an an-close-as-possible translation
from the original Perl code, one can rewrite this as:
... strlen = "%04s" % length
... return chr(int(strlen[2:4], 16)) + chr(int(strlen[0:2], 16))


which is more "Pythonic" to me.

Throwing an exception for values of length less than 100 doesn't seem
terribly good. Did you mean "%04d"?

Personally I think I'd split the number up before round-tripping through
the string. Maybe:

def bcdbyte(b):
return chr(int(str(b), 16))

def bcdlen(length):
return bcdbyte(length%100) + bcdbyte(length//100)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top