Re: [Tutor] Are there other ways of solving this exercise?

2012-01-13 Thread amt
Wow!!! Thanks so much for your reply. It was so nicely written and I
understood everything you said.





Thanks again, have a nice weekend.



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


[Tutor] Are there other ways of solving this exercise?

2012-01-12 Thread amt
Exercise 16, extra credit 3: There's too much repetition in this file.
Use strings, formats, and escapes to print out line1, line2, and line3
with just one target.write() command instead of 6.
Code from the book:


from sys import argv

script, filename = argv

print We're going to erase %r. % filename
print If you don't want that, hit CTRL-C (^C).
print If you do want that, hit RETURN.

raw_input(?)

print Opening the file...
target = open(filename,'w')

print Truncating the file. Goodbye!
target.truncate()

print Now I'm going to ask you for three lines.

line1 = raw_input(line 1: )
line2 = raw_input(line 2: )
line3 = raw_input(line 3: )

print I'm going to write these to the file.

target.write(line1)
target.write(\n)
target.write(line2)
target.write(\n)
target.write(line3)
target.write(\n)

print And finally, we close it.
target.close()




How I solved it after trial and error:

from sys import argv

script, filename = argv

print We're going to erase %r. % filename
print If you don't want that, hit CTRL-C (^C).
print If you do want that, hit RETURN.

raw_input(?)

print Opening the file...
target = open(filename, 'w')

print Truncating the file. Goodbye!
target.truncate()

print Now I'm going to ask you for three lines.

line1 = raw_input(line 1: )
line2 = raw_input(line 2: )
line3 = raw_input(line 3: )

print I'm going to write these to the file.

target.write(%s\n%s\n%s\n %(line1, line2, line3))

print And finally, we close it.
target.close()

This is the only method I was able to figure out of solving the exercise.

Are there other ways of solving this exercise using strings, formats
and escapes like the author mentioned in the exercise question? If
yes, please write them.




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


Re: [Tutor] Are there other ways of solving this exercise?

2012-01-12 Thread Walter Prins
Hi,

On 12 January 2012 14:24, amt 0101...@gmail.com wrote:

 target.write(%s\n%s\n%s\n %(line1, line2, line3))

 This is the only method I was able to figure out of solving the exercise.

 Are there other ways of solving this exercise using strings, formats
 and escapes like the author mentioned in the exercise question? If
 yes, please write them.


Firstly for those interested, I've tracked this down to Learn Python
The Hard Way, 2nd Edition.  (Amt, please include a reference if
possible (especially when online) when you ask questions.)  The
question is available here:
http://learnpythonthehardway.org/book/ex16.html

As for your question, I suppose using the string.format() method is
another way that involves strings, formats and escapes.  See here:
http://docs.python.org/library/stdtypes.html  (and see the section on
str.format therein. )

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


Re: [Tutor] Are there other ways of solving this exercise?

2012-01-12 Thread amt
On Thu, Jan 12, 2012 at 9:40 AM, Walter Prins wpr...@gmail.com wrote:
 Hi,

 On 12 January 2012 14:24, amt 0101...@gmail.com wrote:

 target.write(%s\n%s\n%s\n %(line1, line2, line3))

 This is the only method I was able to figure out of solving the exercise.

 Are there other ways of solving this exercise using strings, formats
 and escapes like the author mentioned in the exercise question? If
 yes, please write them.


 Firstly for those interested, I've tracked this down to Learn Python
 The Hard Way, 2nd Edition.  (Amt, please include a reference if
 possible (especially when online) when you ask questions.)  The
 question is available here:
 http://learnpythonthehardway.org/book/ex16.html

 As for your question, I suppose using the string.format() method is
 another way that involves strings, formats and escapes.  See here:
 http://docs.python.org/library/stdtypes.html  (and see the section on
 str.format therein. )

 Walter

Ok, I will keep that in mind.



After reading from http://docs.python.org/library/stdtypes.html I came
up with this:

from sys import argv

script, filename = argv

print We're going to erase %r. % filename
print If you don't want that, hit CTRL-C (^C).
print If you do want that, hit RETURN.

raw_input(?)

print Opening the file...
target = open(filename, 'w')

print Truncating the file. Goodbye!
target.truncate()

print Now I'm going to ask you for three lines.

line1 = raw_input(line 1: )
line2 = raw_input(line 2: )
line3 = raw_input(line 3: )

print I'm going to write these to the file.

bag = %s\n%s\n%s\n.format(line1,line2,line3)
target.write(bag)

print And finally, we close it.
target.close()


Is this how it is supposed to look like using str.format?




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


Re: [Tutor] Are there other ways of solving this exercise?

2012-01-12 Thread Walter Prins
Hi amt,

On 12 January 2012 15:11, amt 0101...@gmail.com wrote:
 After reading from http://docs.python.org/library/stdtypes.html I came
 up with this:

 bag = %s\n%s\n%s\n.format(line1,line2,line3)
 target.write(bag)

 Is this how it is supposed to look like using str.format?

Not quite.  The documentation states:

str.format(*args, **kwargs): Perform a string formatting operation.
The string on which this method is called can contain literal text or
replacement fields delimited by braces {}. Each replacement field
contains either the numeric index of a positional argument, or the
name of a keyword argument. Returns a copy of the string where each
replacement field is replaced with the string value of the
corresponding argument.

So, this is different from the % operator, where format specifiers are
indicated with %.  Instead you need to use, as per the documentation,
curly braces e.g. {  and }.

You can easily test this in the Python interpreter e.g.:

 print %s\n%s\n%s.format('aaa', 'bbb', 'ccc')
%s
%s
%s

(Hmm, does not work...)

 print '{0}\n{1}\n{2}'.format('aaa','bbb','ccc')
aaa
bbb
ccc

(Hmm, that does work!...)

Final comment, you can get rid of the variable bag by directly
printing the result of the call to format() like you did in your
previous solution.

Cheers,

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


[Tutor] Are there other ways of solving this exercise?

2012-01-12 Thread Mike G
quote
...Exercise 16, extra credit 3...Code from the book...like the author mentioned
/quote

The book and author, do they have a name...? Is this an exercise in refactoring?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Are there other ways of solving this exercise?

2012-01-12 Thread amt
I'll give it another try:

On Thu, Jan 12, 2012 at 10:26 AM, Walter Prins wpr...@gmail.com wrote:
 Hi amt,

 On 12 January 2012 15:11, amt 0101...@gmail.com wrote:
 After reading from http://docs.python.org/library/stdtypes.html I came
 up with this:

 bag = %s\n%s\n%s\n.format(line1,line2,line3)
 target.write(bag)

 Is this how it is supposed to look like using str.format?

 Not quite.  The documentation states:

 str.format(*args, **kwargs): Perform a string formatting operation.
 The string on which this method is called can contain literal text or
 replacement fields delimited by braces {}. Each replacement field
 contains either the numeric index of a positional argument, or the
 name of a keyword argument. Returns a copy of the string where each
 replacement field is replaced with the string value of the
 corresponding argument.

 So, this is different from the % operator, where format specifiers are
 indicated with %.  Instead you need to use, as per the documentation,
 curly braces e.g. {  and }.

 You can easily test this in the Python interpreter e.g.:

 print %s\n%s\n%s.format('aaa', 'bbb', 'ccc')
 %s
 %s
 %s

 (Hmm, does not work...)

 print '{0}\n{1}\n{2}'.format('aaa','bbb','ccc')
 aaa
 bbb
 ccc

 (Hmm, that does work!...)
So the code should look like this:

bag = {0}\n{1}\n{2}.format(line1,line2,line3)
target.write(bag)




 Final comment, you can get rid of the variable bag by directly
 printing the result of the call to format() like you did in your
 previous solution.

 Cheers,

 Walter

You mean print {0}\n{1}\n{2}\n.format(line1,line2,line3)?


Ok, but if I drop the variable bag and print directly,how will I write
line1,line2,line3 in the .txt file since I have no parameter to give
to the write method.(target.write() ) ?


Walter, thanks a lot for taking your time to help me out.




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


Re: [Tutor] Are there other ways of solving this exercise?

2012-01-12 Thread Walter Prins
On 12 January 2012 16:57, amt 0101...@gmail.com wrote:
 I'll give it another try:

 So the code should look like this:

 bag = {0}\n{1}\n{2}.format(line1,line2,line3)
 target.write(bag)

Yes.

 Final comment, you can get rid of the variable bag by directly
 printing the result of the call to format() like you did in your
 previous solution.

 You mean print {0}\n{1}\n{2}\n.format(line1,line2,line3)?

No, print as such is actually besides the point, it was just used to
actually output the string with some interpretation given to the
newlines.


 Ok, but if I drop the variable bag and print directly,how will I write
 line1,line2,line3 in the .txt file since I have no parameter to give
 to the write method.(target.write() ) ?

Well in fact you do, in the same way that you have one in your
original solution which had no intermediate bag variable.

OK let's backtrack a bit and try to clarify.  In your original solution you had:

target.write(%s\n%s\n%s\n %(line1, line2, line3))

What's happening here?  Firstly you're calculating a string expression, namely:

%s\n%s\n%s\n %(line1, line2, line3)

Let's try this in the Python interpreter:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit
(AMD64)] on win32
Type copyright, credits or license() for more information.
 %s\n%s\n%s\n % ('aaa','bbb','ccc')
'aaa\nbbb\nccc\n'
 print %s\n%s\n%s\n % ('aaa','bbb','ccc')
aaa
bbb
ccc



Notice, first I type the expression directly at the interpreter
prompt.  Python therefore evaluates the epxression types, and as a
courtesy displays the result, then discards it.  On the next line, I
repeat the excercise, but this time prepend a print statement to it.
This is essentially giving the result of the expression evaluation to
the print statement.  Now the job of the print statement is also to
display the value of variables, but it applies a bit more
interpretation to what it's given, and so it actually interpretets the
newline characters in the result string, displayed as \n in the
string on the previous line, and so the literal aaa, bbb and ccc
ends up on seperate lines.

Now consider your original line again:
target.write(%s\n%s\n%s\n %(line1, line2, line3))

What's actually happening here?  Well, as happens in the interactive
interpreter example above, firstly the string expression using the %
operator is evaluated, which results in a string result as above, but
using the contents of line1, line2 and line3.  This result is then
passed as the parameter to the target.write() method.

Now, the exact same thing happens when you use a string method.  The
string method str.format() also returns a result, namely the string
that results when formatting the format string with the parameter
values specified.  So in this sense it's no different to what you had
before.  Here's an interactive Python demonstration again:

Python 2.7 (r27:82525, Jul  4 2010, 07:43:08) [MSC v.1500 64 bit
(AMD64)] on win32
Type copyright, credits or license() for more information.
 '{0}\n{1}\n{2}\n'.format(aaa,bbb,ccc)
'aaa\nbbb\nccc\n'
 print '{0}\n{1}\n{2}\n'.format(aaa,bbb,ccc)
aaa
bbb
ccc



Notice, the mechanics is exactly the same.  Consequently, you can
write your code without the intermediate bag variable, just like you
didn't need it in your original solution:
target.write( '{0}\n{1}\n{2}\n'.format(line1,line2,line3) )

To belabor the point: here the call to str.format() returns a result
(another string) which is immediately and directly passed to
target.write() for writing to the file.

 Walter, thanks a lot for taking your time to help me out.

You're welcome, hope it helps.

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