[Tutor] need an explanation

Prasad, Ramit ramit.prasad at jpmorgan.com
Thu Oct 11 23:32:51 CEST 2012


Matthew Ngaha wrote: 
[snip]
> @ DAVE.. you said
> sys is a module (presumably you have an import
> somewhere above this line). In the module, there's a list argv.
> 
> the import statements are:
> 
> import sys
> import os
> import shutil
> import zipfile
> 
> so im guessing [sys, os, shutil, zipfile]  these are the arguments being passed? my mind tells me no, as these
> are more likely the arguments in the A_Class init method?
> 
> here is he full code... i changed the names in the mail to make it clearer. so the names in the code will be
> different. A_Class is actually ZipReplace etc..
> 
> i cant test it because on start the program returns this error and i dont know how to fix it:
> 
>     ZipReplace(*sys.argv[1:4]).zip_find_replace()
> TypeError: __init__() takes exactly 4 positional arguments (1 given)
> 

I suspect that you are not giving enough arguments when running your file. It needs to be something like 
`python test.py arg1 arg2 arg3`.  You can find out by doing a `print sys.argv[1:4]` (Python 2) or
`print(sys.argv[1:4])` (Python 3).

> 
> full code:
> 
> import sys
> import os
> import shutil
> import zipfile
> 
> class ZipReplace:
>     def __init__(self, filename, search_string, replace_string):
>         self.filename = filename
>         self.search_string = search_string
>         self.replace_string = replace_string
>         self.temp_directory = "unzipped-{}".format(
>                 filename)
> 
>    def _full_filename(self, filename):
>         return os.path.join(self.temp_directory, filename)
> 
>     def zip_find_replace(self):
>         self.unzip_files()
>         self.find_replace()
>         self.zip_files()
> 
>    def unzip_files(self):
>         os.mkdir(self.temp_directory)
>         zip = zipfile.ZipFile(self.filename)
>         try:
>             zip.extractall(self.temp_directory)
>         finally:
>             zip.close()
> 
>     def find_replace(self):
>         for filename in os.listdir(self.temp_directory):
>             with open(self._full_filename(filename)) as file:
>                 contents = file.read()
>             contents = contents.replace(
>                     self.search_string, self.replace_string)
>             with open(self._full_filename(filename), "w") as file:
>                 file.write(contents)
> 
>    def zip_files(self):
>         file = zipfile.ZipFile(self.filename, 'w')
>         for filename in os.listdir(self.temp_directory):
>             file.write(self._full_filename(filename), filename)
>         shutil.rmtree(self.temp_directory)
> 
> if __name__ == "__main__":
>     ZipReplace(*sys.argv[1:4]).zip_find_replace()
> 
> is a bit too advanced for me but i now see what it does.. although i wish it didnt return an error when run.
> 
> so the arguments being passed are...
> 
> [os, shutil, zipfile] or [filename, search_string, return_string] ?

The arguments to ZipReplace should be [filename, search_string, return_string].

Imports are completely separate and unrelated to arguments. Importing a library means you can 
access that library from your script. If you are importing a library at the module level (i.e. 
not in a function or a class) means that everything in that module can access that library. 
You should not need to pass those modules (or packages) to anything inside the script. Typically 
even if you need it in another script you just import it there rather than passing it. I am sure 
there are valid reasons for passing an import to another module, but I have not needed to ever do 
something like that.

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