Re: [Tutor] need an explanation

2012-10-12 Thread Matthew Ngaha
Thanks for everyone that replied. I really gained a lot from all the
input. Also thanks to Dave and Prasad for explaining why i had errors
trying to run the program. I fully understand the code now and im able to
run it without errors.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] need an explanation

2012-10-11 Thread Matthew Ngaha
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 * .


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

thanks for your time
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need an explanation

2012-10-11 Thread Prasad, Ramit
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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need an explanation

2012-10-11 Thread Dave Angel
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


Re: [Tutor] need an explanation

2012-10-11 Thread Mark Lawrence

On 11/10/2012 20:24, 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?


sys isn't doing anything and the weird indexing is called slicing.



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 isn't calling anything.  The second, third and fourth items from 
sys.argv are being used via slicing to create A_Class and then 
A_Class_Method is called.





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


Obviously a Monty Python fan as I see 3 methods :)



def __init__(self):
 self.zipping_directory = unzipped-{}.format(filename)


Where did filename appear from above?



def _full_filename(self, filename):
 return os.path.join(self.zipping_directory, filename)

def zip_files(self):
 file = zipfile.ZipFile(self.filename, 'w')


Where is self.filename set up?


 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?


I suggest that you show us some real code that will run with some print 
statements in appropriate places to show us what is happening.  That way 
you may well be able to answer your own questions and learn at the same 
time.




thanks for your time



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




--
Cheers.

Mark Lawrence.

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


Re: [Tutor] need an explanation

2012-10-11 Thread Emile van Sebille

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 is doing nothing -- argv in sys holds the command line arguments 
passed into python.  The first sys.argv[0] is the python script being 
executed, and the rest sys.argv[1:] are arguments passed in to that 
script.  specifying sys.argv[1:4] means you're picking just the three 
items.  *sys.argv[1:4] expands those from their list form and are passed 
into A_Class's __init__ constructor (assuming old style classes).  This 
instantiates an instance of that class then invokes the A_Class_Method 
of that instance.



 
is sys able to call methods? if so why does it need indexing if it uses * .
 
 
--
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 first is passed into the instance's _full_filename method and the 
result of that becomes the first argument passed into file.write, and 
the second is passed in the file.write as the second argument.


HTH

Emile


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?
 
thanks for your time





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


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


Re: [Tutor] need an explanation

2012-10-11 Thread Matthew Ngaha


 Obviously a Monty Python fan as I see 3 methods :)

lol i dont know what i was looking at.. yes its 3 methods sorry:(



 def __init__(self):
  self.zipping_directory = unzipped-{}.format(filename)


 Where did filename appear from above?



sorry i didnt write everything. the init method also had...
self.filename = filename  ... i will paste the whole code



 I suggest that you show us some real code that will run with some print
 statements in appropriate places to show us what is happening.  That way
 you may well be able to answer your own questions and learn at the same
 time.



 sadly when i run the file i get an error so i dont know what to doto fix
it and be able to run

@ 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)


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] ?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] need an explanation

2012-10-11 Thread Dave Angel
On 10/11/2012 04:48 PM, Matthew Ngaha wrote:

 Obviously a Monty Python fan as I see 3 methods :)

 lol i dont know what i was looking at.. yes its 3 methods sorry:(


 def __init__(self):
  self.zipping_directory = unzipped-{}.format(filename)

 Where did filename appear from above?



 sorry i didnt write everything. the init method also had...
 self.filename = filename  ... i will paste the whole code


 I suggest that you show us some real code that will run with some print
 statements in appropriate places to show us what is happening.  That way
 you may well be able to answer your own questions and learn at the same
 time.

 sadly when i run the file i get an error so i dont know what to doto fix
 it and be able to run

 @ 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?

They aren't arguments to anything.  But sys.argv would be undefined if
you had not imported sys.


 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)

When you read this, what does it tell you?  Decompose the statement as I
showed you, and see what the args actually are by printing them. 
Clearly, the error message tells you that you have a list of size 0
instead of size 3.  (The self argument is implied, since you're creating
an instance)

I suggest you look up sys.argv to see what these arguments actually
mean.  If you don't supply any of them, then you'll get this error.

Are you learning from a tutorial?  Does it have argv in its index?  Have
you tried googling forpython sys.argv ?

When you run this script, what arguments DO you type in ?

python myscript.py  arg1  arg2   arg3


 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] ?


Those first three are imports, not arguments to anything.  And the
second 3 are 3 of the formal parameters to the __init__() method.  The
arguments come from the slice, which comes from sys.argv, which comes
from the command line.

You never showed us how you run the program, so how do we know what the
cmdline arguments are?


-- 

DaveA

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


Re: [Tutor] need an explanation

2012-10-11 Thread Prasad, Ramit
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.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor