Re: [Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-06 Thread Alan Gauld via Tutor
On 06/07/16 15:04, loh...@tuta.io wrote:

> script, filename = argv
> txt = open (filename)
> 
> print "Here's your file %r: " % filename
> print txt.read()
> 
> print "Type the filename again: "
> file_again = raw_input("> ")
> 
> txt_again = open(file_again)
> print txt_again.read()


> why do I have to create a variable txt_again to assign it to the open 
> function and them print the file?

You don't, and could get away with a single
variable - filename. Like this:

filename = argv[1]
print "Here's your file %r: " % filename
print open(filename).read()

filename = raw_input("Type the filename again: >")
print open(filename).read()

But your book is (I assume) trying to teach you
good practice.

While you could have just printed the result of read directly,
its better not to over complicate code by doing too much in one
line (for a start, its harder to debug) the same variable.
Variables are cheap to create and if given useful names
tell us a lot about the purpose of the code.

In this case it's all a bit trivial, just printing the file
content, but if you were doing more complex processing
storing the file (and data) in variables would be the
best choice.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-06 Thread Michael Selik
On Wed, Jul 6, 2016 at 10:59 AM  wrote:

> why do I have to create a variable txt_again to assign it to the open
> function and them print the file?
> why is it that I can't only write something like open(file_again).read()?
>

Good insight. In fact you don't need to create the variable. The code ``data
= open('filename').read()`` will open the file named "filename" in the
current working directory, read it, and assign the data to a variable.

However, many programmers use variables not because they must, but because
good variable names can make code easier to read. Also, doing less stuff on
a line of code can make that code easier to read.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-06 Thread lohecn
first, sorry everyone for having attached the file instead of just typing it 
here.
second, thanks a lot for the replies; even though I gave you no code it was 
quite helpful!
the code was this:

from sys import argv

script, filename = argv
txt = open (filename)

print "Here's your file %r: " % filename
print txt.read()

print "Type the filename again: "
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

Peter Otten explained it to me line by line [thanks so much :)] however, I do 
have one more question:
why do I have to create a variable txt_again to assign it to the open 
function and them print the file?
why is it that I can't only write something like open(file_again).read()?


6. Jul 2016 05:22 by __pete...@web.de:


> loh...@tuta.io>  wrote:
>
>> hey everyone. this is my first time trying this -- actually, I've been
>> studying python only for some days now, and I'm afraid my questions are
>> going to be rally simple, but I can't seem to understand this piece of
>> code and thus can't move on.
>
> You seem to be talking about
>
> http://learnpythonthehardway.org/book/ex15.html
>
> """
> from sys import argv
>
> script, filename = argv
>
> txt = open(filename)
>
> print "Here's your file %r:" % filename
> print txt.read()
>
> print "Type the filename again:"
> file_again = raw_input("> ")
>
> txt_again = open(file_again)
>
> print txt_again.read()
> """
>
> As others said, always provide the code you are asking about, or if that is
> not possible at least provide a link.
>
>> you probably know the book, so you know that zed always makes us write
>> code so that then we can understand how it works, and it's great, but in
>> this exercise there are just too many new functions and without
>> explanation they are a bit hard to understand... so I'm having trouble
>> with most of the lines here.
>>
>> it's not that I want the full explanation to that code, but since I'm
>> unfamiliar with some of its concepts, I'm just going to tell you all the
>> things that I don't understand (sorry for it being a lot):
>> 1. the need to put script into an estipulation for argv (line 3)
>
> Write a script tmp.py containing
>
> from sys import argv
> print argv
>
> then call it with with one parameter, e. g.
>
> $ python tmp.py somefile.txt
> ['tmp.py', 'somefile.txt']
>
> As you can see argv is a list with two items, the first being "tmp.py", the
> name of the script you are invoking. You are only interested in the second
> one, the filename. The easy way to get that is
>
> filename = argv[1]
>
> the hard way is to use "unpacking"
>
> script, filename = argv
>
> where python will assign one item from the list on the right to every name
> on the left:
>
 items = ["foo", "bar"]
 one, two = items
 one
> 'foo'
 two
> 'bar'
>
> What happens if the number of names on the left doesn't match the number of
> items in the list?
>
 one, two, three = items
> Traceback (most recent call last):
>   File "", line 1, in 
> ValueError: need more than 2 values to unpack
>
> You get an exception. That is why you have to provide the name "script" in
> Zed's example even though you are not actually interested in the script
> name.
>
>> 2. the what is txt and why it has to be used there (line 4)
>
> txt is a file object and
>
>> 3. txt.read() -- which are all new functions(? I dont even know what they
>> are)  (line 7)
>
> read() is a method that here reads the whole file into a string. You use 
> the
> open() function to open a file and usually assign the file object that is
> returned by open to a name. You can freely choose that name. The structure
> is the same for every object, be it a number:
>
> x = 42  # assign a number to x
> y = x + x  # do some arithmetic with x and assign the result to y
> print y  # print the result
>
> a list:
>
> mynumbers = list()  # create a list
> mynumbers.append(42)  # append a number to the list
> print mynumbers  # print the list
>
> or a file:
>
> myfile = open("example.txt")  # open the file example.txt in the current
>   # working directory. If the file doesn't 
> exist
>   # you get an error
>
> print "first line:", myfile.readline()  # read the first line and print it
> print "rest of the file:"
> print myfile.read()  # read the rest of the file and print it
>
> myfile.close()  # close the file
>
>> 4. file_again (line 10)
>> 5. txt_again (line 12)
>> and line 14.
>
> 4. and 5. are just a repetition of the first part, with the variation that
> the filename, assigned to file_again is read interactively with raw_input()
> instead of passing it as a commandline argument to the script.
>
> The names used can be freely chosen by the programmer, a script
>
> from sys import argv
>
> red_apple, my_hat = argv
>
> blue_suede_shoes = open(my_hat)
> print blue_suede_shoes.read()
> blue_suede_shoes.close()
>
> would work exactly like the first part of the hard-way example. However,
> picking descrip

Re: [Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-06 Thread Peter Otten
loh...@tuta.io wrote:

> hey everyone. this is my first time trying this -- actually, I've been
> studying python only for some days now, and I'm afraid my questions are
> going to be rally simple, but I can't seem to understand this piece of
> code and thus can't move on.

You seem to be talking about

http://learnpythonthehardway.org/book/ex15.html

"""
from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()
"""

As others said, always provide the code you are asking about, or if that is 
not possible at least provide a link.
 
> you probably know the book, so you know that zed always makes us write
> code so that then we can understand how it works, and it's great, but in
> this exercise there are just too many new functions and without
> explanation they are a bit hard to understand... so I'm having trouble
> with most of the lines here.
> 
> it's not that I want the full explanation to that code, but since I'm
> unfamiliar with some of its concepts, I'm just going to tell you all the
> things that I don't understand (sorry for it being a lot):
> 1. the need to put script into an estipulation for argv (line 3)

Write a script tmp.py containing

from sys import argv
print argv

then call it with with one parameter, e. g.

$ python tmp.py somefile.txt
['tmp.py', 'somefile.txt']

As you can see argv is a list with two items, the first being "tmp.py", the 
name of the script you are invoking. You are only interested in the second 
one, the filename. The easy way to get that is

filename = argv[1]

the hard way is to use "unpacking"

script, filename = argv

where python will assign one item from the list on the right to every name 
on the left:

>>> items = ["foo", "bar"]
>>> one, two = items
>>> one
'foo'
>>> two
'bar'

What happens if the number of names on the left doesn't match the number of 
items in the list?

>>> one, two, three = items
Traceback (most recent call last):
  File "", line 1, in 
ValueError: need more than 2 values to unpack

You get an exception. That is why you have to provide the name "script" in 
Zed's example even though you are not actually interested in the script 
name.

> 2. the what is txt and why it has to be used there (line 4)

txt is a file object and

> 3. txt.read() -- which are all new functions(? I dont even know what they
> are)  (line 7)

read() is a method that here reads the whole file into a string. You use the 
open() function to open a file and usually assign the file object that is 
returned by open to a name. You can freely choose that name. The structure 
is the same for every object, be it a number:

x = 42  # assign a number to x
y = x + x  # do some arithmetic with x and assign the result to y
print y  # print the result

a list:

mynumbers = list()  # create a list
mynumbers.append(42)  # append a number to the list
print mynumbers  # print the list

or a file:

myfile = open("example.txt")  # open the file example.txt in the current
  # working directory. If the file doesn't exist
  # you get an error

print "first line:", myfile.readline()  # read the first line and print it
print "rest of the file:"
print myfile.read()  # read the rest of the file and print it

myfile.close()  # close the file

> 4. file_again (line 10)
> 5. txt_again (line 12)
> and line 14.

4. and 5. are just a repetition of the first part, with the variation that 
the filename, assigned to file_again is read interactively with raw_input() 
instead of passing it as a commandline argument to the script.

The names used can be freely chosen by the programmer, a script

from sys import argv

red_apple, my_hat = argv

blue_suede_shoes = open(my_hat)
print blue_suede_shoes.read()
blue_suede_shoes.close()

would work exactly like the first part of the hard-way example. However, 
picking descriptive names and using them consistently makes it much easier 
for a human reader to understand what's going on.

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


Re: [Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-06 Thread Alan Gauld via Tutor
On 06/07/16 00:56, loh...@tuta.io wrote:
> hey everyone. this is my first time trying this 

Welcome, but...

> you probably know the book,

Sorry, I don't and I suspect I'm not alone.
It's probably a fine book, but we don't all know it.

>  so you know that zed always makes us write code 
> so that then we can understand how it works, 

Good man Zed :-)

> exercise there are just too many new functions and without explanation they 
> are a bit hard to understand... so I'm having trouble with most of the lines 
> here.

And here's the next problem.
This is a text based mailing list. As such the server often strips out
attachments as potential security risks. So we can't see the code (I'm
assuming you attached it?)

> 1. the need to put script into an estipulation for argv (line 3)
> 2. the what is txt and why it has to be used there (line 4)
> 3. txt.read() -- which are all new functions(? I dont even know what they 
> are)  (line 7)
> 4. file_again (line 10)
> 5. txt_again (line 12)
> and line 14.

Without sight of the code its hard to know what's going on.
But I suspect some of these "functions" are actually variables
(or objects) and hopefully zed has already discussed those?

Can you repost but include your code inside the mail message.
Also try to post in plain text since HTML tends to get garbled
in transit.

HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-06 Thread Michael Selik
On Tue, Jul 5, 2016 at 8:24 PM  wrote:

> I'm having trouble with most of the lines here.
>

It looks like you tried to attach a file. This mailing list does not allow
attachments. Instead, could you paste the code into your email?


> things that I don't understand:
> 1. the need to put script into an estipulation for argv (line 3)
> 2. the what is txt and why it has to be used there (line 4)
> 3. txt.read() -- which are all new functions(? I dont even know what they
> are)  (line 7)
>

I'm guessing txt is a file object or a file-like object that supports the
.read method to read the entire contents of the file into a single string
object.


> 4. file_again (line 10)
> 5. txt_again (line 12)
> and line 14.
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-05 Thread Steven D'Aprano
Hi Heloisa, and welcome.

Do you have a link to the code? Or better still, if it is short (say 
under fifty lines) can you copy it into an email and send it?

On Wed, Jul 06, 2016 at 12:56:19AM +0100, loh...@tuta.io wrote:

[...]
> 1. the need to put script into an estipulation for argv (line 3)

I'm sorry, I don't know what that word "estipulation" means. Do you mean 
"stipulation", as in:

That which is stipulated, or agreed upon; that which is
definitely arranged or contracted; an agreement


> 2. the what is txt and why it has to be used there (line 4)
> 3. txt.read() -- which are all new functions(? I dont even know what they 
> are)  (line 7)
> 4. file_again (line 10)
> 5. txt_again (line 12)
> and line 14.

The only thing I can guess without seeing the code is that MAYBE txt is 
an open file, and txt.read() reads the content of the file.



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


Re: [Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-05 Thread boB Stepp

Welcome!

On 07/05/2016 06:56 PM, loh...@tuta.io wrote:

hey everyone. this is my first time trying this -- actually, I've been
studying python only for some days now, and I'm afraid my questions are going
to be rally simple, but I can't seem to understand this piece of code and
thus can't move on.

you probably know the book, so you know that zed always makes us write code
so that then we can understand how it works, and it's great, but in this
exercise there are just too many new functions and without explanation they
are a bit hard to understand... so I'm having trouble with most of the lines
here.


While this book is fairly well known, not everyone has a copy at hand. 
I would suggest that you resend your email with the full exercise typed 
into its body (This list does not accept attachments BTW.), and then 
pose your questions.  This way you provide the necessary context *in* 
your email body, so that everyone can follow along.


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


[Tutor] help with exercise 15 of zed shaw's LPTHW

2016-07-05 Thread lohecn
hey everyone. this is my first time trying this -- actually, I've been 
studying python only for some days now, and I'm afraid my questions are going 
to be rally simple, but I can't seem to understand this piece of code and 
thus can't move on.

you probably know the book, so you know that zed always makes us write code 
so that then we can understand how it works, and it's great, but in this 
exercise there are just too many new functions and without explanation they 
are a bit hard to understand... so I'm having trouble with most of the lines 
here.

it's not that I want the full explanation to that code, but since I'm 
unfamiliar with some of its concepts, I'm just going to tell you all the 
things that I don't understand (sorry for it being a lot):
1. the need to put script into an estipulation for argv (line 3)
2. the what is txt and why it has to be used there (line 4)
3. txt.read() -- which are all new functions(? I dont even know what they 
are)  (line 7)
4. file_again (line 10)
5. txt_again (line 12)
and line 14.

as you can see, it's pretty much everything. sorry about that.
I'd really appreciate any help at all!
thanks a lot,

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