On 10/11/2012 03:24 PM, 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()

sys isn't being indexed.  sys is a module (presumably you have an import
somewhere above this line).  In the module, there's a list argv.  That
list is being indexed in the common ways.

When the index contains one or more colons, it's called a slice.  A
slice is another list of zero or more items from this list.  If you
don't understand slices, look it up in your book, or on python.org

argv itself represents the commandline arguments passed when the script
was started.  argv[0] is the name of the script (more or less), and
argv[1], argv[2], argv[3], and argv[4] (etc.) are parameters.  You can
use len(sys.argv) to see how big the list is.

If A_Class_Method is really a class method, then it's a waste of time
creating an instance.  You might as well use
    A_Class.A_Class_Method()

But my guess is that it's NOT a class method, just a confusing name.

> is sys able to call methods? if so why does it need indexing if it uses * .

Where do you see any method of sys being used? I suspect you're getting
confused because there are many things on one line, and you don't know
how to decompose it.  That line is roughly equivalent to:

args = sys.argv[1:4]  # build a list of up to 3 items

obj = A_Class(*args) # instantiate A_Class with up to 3 arguments

obj.A_Class_Method()  # call the method on that instance

del args
del obj

>
> ------------------------------------------------------------------------------------------------------
> 2) also i need help with zipfiles. these 2 functions are related in the

These 3 methods, not 2 functions

> 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?
The two arguments to the write are different, they are not both
"filename"  The first argument is the return value of the call to
full_filename()  In other words, it's a full path to an actual file,
that will be stored in the zip.  The second argument is the name that
will be stored in the zipfile.

See http://docs.python.org/library/zipfile.html

Once again, to see what's going on, try decomposing the line you're not
comfortable with:

   fullname = self.full_filename(filename)
   #perhaps here you should print both strings, to see how they differ
   file.write(fullname, filename)
   del fullname

If you respond, please remember NOT to top-post.

-- 

DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to