[Tutor] need an explanation

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Oct 11 22:05:03 CEST 2012


Matthew Ngaha wrote: 
> i need help on 2 topics.
> 
> 1) can someone please tell me what sys is doing, and why its using weird indexing?
> 
> if __name__ == "__main__":
>     A_Class(*sys.argv[1:4]).A_Class_Method()
> 
> is sys able to call methods? if so why does it need indexing if it uses * .

Sys is a module. What do you mean by calling methods? Sys does nothing
unless you ask it to do something. In the above example, there are several 
things going on. I will try and help what is going on.

1. sys.argv[1:4] - sys.argv is the list of arguments provided to Python 
(E.g. For the command `python test.py argument1, argument2` 
sys.argv would be ['test.py', 'argument1', 'argument2']. So going
back to your code. "[1:4]" is a technique called slicing. It creates 
a new (sliced) list with elements from sys.argv. To understand what is 
being sliced, puzzle through this short sample.

>>>[1,2,3,4,5][1:4]
[2, 3, 4]

2. *sys.argv[1:4] - The asterisk tells Python to use the list (in this
case the new sliced list) as a list of arguments for some function.

3. A_Class() - Create an instance of class A_Class

4. A_Class().A_Class_Method() - Call the A_Class_Method function
on the newly created instance object of class A_Class.

> 
> 
> ------------------------------------------------------------------------------------------------------
> 2) also i need help with zipfiles. these 2 functions are related in the same class.
> 
> def __init__(self):
>     self.zipping_directory = "unzipped-{}".format(filename)
> 
> def _full_filename(self, filename):
>         return os.path.join(self.zipping_directory, filename)
> 
> def zip_files(self):
>         file = zipfile.ZipFile(self.filename, 'w')
>         for filename in os.listdir(self.zipping_directory):
>             file.write(self._full_filename(filename), filename)
> 
> the main thing i need help with is the last line. the zip file is writing to a file but why does it use the same
> argument twice? the for loop above that line returns the file from the zipping directory, which is the 2nd
> argument on file.write? But the 1st argument is using that same file because that is the file returned from the
> def _full_filename(self, filename): method. so please can someone tell me why it uses the same file argument
> twice in its write method?

You can actually use Python to find out a great deal about questions
like this. This is one of the reasons I like Python's interactive 
prompt. 

>>> help(zipfile.ZipFile.write )
Help on method write in module zipfile:

write(self, filename, arcname=None, compress_type=None) unbound zipfile.ZipFile method
    Put the bytes from filename into the archive under the name
    arcname.

So in this case, the first filename is being zipped while the second
filename is the name that will be shown *inside* the zip.

> 
> thanks for your time


You are welcome.

Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list