473,756 Members | 3,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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%02 s%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 1936
vj
I posted too soon:
Statement 1:
my $today = sprintf("%4s%02 s%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).he xdigest() 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%02 s%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%0 2d" % 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)).hexd igest()

# 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
1560
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. in the first line I would appreciate it deeply ... for ($y = 0, $x = $cx-$cy-1; $y <= $cy; ++$y,++$x) { $prd = $q * $y_ar + $car; $prd -= ($car = intval($prd / 1E7)) * 1E7;
4
2212
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 = " s^\?AAA\?01^BBB^g; #Comment "
31
2798
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 Python programs into optimized C++, and hence, highly optimized machine language. Besides many bug fixes and optimizations, these releases add the following changes: -support for 'bisect', 'collections.deque' and 'string.maketrans'
15
5884
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 do I translate the second line in a similiar compact way to python?
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9275
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.