Re: Help needed with translating perl to python

2007-06-26 Thread Greg Armer
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
[EMAIL PROTECTED]
http://www.codelounge.org

If it would be cheaper to repair the old one, the
company will insist on the latest model.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does python support working with password protected zip files?

2008-03-06 Thread Greg Armer
On 3/6/08, Malcolm Greene <[EMAIL PROTECTED]> wrote:
> I'm new to Python and trying to figure out how to read and write to
>  password protected zip files.
>
>  I can work with plain zip files no problem.
>
>  I've googled this topic and am coming up empty except for a post on
>  Nabbler.com that seemed to imply that password protected zip files
>  may(???) be supported in Python 2.6 and ads for a commercial Windows
>  (only) zip component from chilkat.com.
>
>  Any suggestions?

Hi Malcolm,

I just had to do some with with this myself and eventually resorted to
simply using a popen() call to the system unzip utility with the
-Ppass command line option.



def _unzipdata(self):
print "[~] Processing zip file %s..." % self.zipfile
# Create temporary directory
os.mkdir('_temp')

# Unzip password protected zipfile to _temp
os.chdir('_temp')
os.popen("unzip -P%s ../%s" % (self.password, self.zipfile), "r")
return True


While this is not ideal, i could not find any other alternatives.

Hope that helps.

--
Greg Armer

Entropy has us outnumbered.
-- 
http://mail.python.org/mailman/listinfo/python-list