473,405 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Help needed with translating perl to python

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

Jun 26 '07 #1
2 1921
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

Jun 26 '07 #2
On Jun 26, 8:04 am, vj <vinjv...@gmail.comwrote:
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:
>>def bcdlen(*args):
.... 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'
--
Hope this helps,
Steven

Jun 26 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Davis Marques | last post by:
hi; I'm translating some PHP scripts to Python and have hit a roadblock with a for statement. If someone could explain to me how one should translate the multiple increment, evaluations, etc....
4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
31
by: Mark Dufour | last post by:
Hi all, I have recently released version 0.0.20 and 0.0.21 of Shed Skin, an optimizing Python-to-C++ compiler. Shed Skin allows for translation of pure (unmodified), implicitly statically typed...
15
by: hofer | last post by:
Hi, Let's take following perl code snippet: %myhash=( one =1 , two =2 , three =3 ); ($v1,$v2,$v3) = @myhash{qw(one two two)}; # <-- line of interest print "$v1\n$v2\n$v2\n"; How...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.