Help needed with translating perl to python

Discussion in 'Python' started by vj, Jun 26, 2007.

  1. vj

    vj Guest

    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) ;
    }
    vj, Jun 26, 2007
    #1
    1. Advertising

  2. vj

    vj Guest

    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
    vj, Jun 26, 2007
    #2
    1. Advertising

  3. vj

    Jay Loden Guest

    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?


    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.

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

    >
    > is md5_hex the same as md5.new(key).hexdigest() in python?


    http://www.xav.com/perl/site/lib/Digest/MD5.html#functions

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


    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
    Jay Loden, Jun 26, 2007
    #3
  4. vj

    Greg Armer Guest

    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

    http://www.codelounge.org

    If it would be cheaper to repair the old one, the
    company will insist on the latest model.
    Greg Armer, Jun 26, 2007
    #4
  5. vj

    Guest

    On Jun 26, 8:04 am, vj <> wrote:
    > 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, 2007
    #5
  6. vj

    Guest

    On Jun 26, 8:59 am, wrote:

    (snipped)

    >
    > >>> 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'



    Let me add that instead of an an-close-as-possible translation
    from the original Perl code, one can rewrite this as:

    >>> def bcdlen(length):

    .... strlen = "%04s" % length
    .... return chr(int(strlen[2:4], 16)) + chr(int(strlen[0:2], 16))


    which is more "Pythonic" to me.

    --
    Hope this helps,
    Steven
    , Jun 26, 2007
    #6
  7. vj

    Duncan Booth Guest

    wrote:

    > Let me add that instead of an an-close-as-possible translation
    > from the original Perl code, one can rewrite this as:
    >
    >>>> def bcdlen(length):

    > ... 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)
    Duncan Booth, Jun 28, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eirik Eldorsen

    need help translating VB.NET -> C#

    Eirik Eldorsen, Nov 25, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    525
    =?Utf-8?B?d2VpY2h1bmcgW01DU0QsIE1DREJBXQ==?=
    Nov 26, 2004
  2. Davis Marques
    Replies:
    6
    Views:
    299
    Paul Rubin
    Jan 12, 2004
  3. lombardm

    Translating c code help required

    lombardm, Sep 26, 2007, in forum: C Programming
    Replies:
    9
    Views:
    336
    lombardm
    Sep 27, 2007
  4. hofer
    Replies:
    12
    Views:
    1,462
  5. joe
    Replies:
    1
    Views:
    112
Loading...

Share This Page