Re: [Tutor] using python to write web page

2011-06-16 Thread ALAN GAULD


From: Michael bridges 
To: ALAN GAULD 
Sent: Thursday, 16 June, 2011 1:07:14
Subject: Re: [Tutor] using python to write web page


> your python code includes html.

OK, Now I understand.

> i write the python only code --> web page 
> [ do not care if it is html, xhtml, java, javascript, or whatever]

But a "web page" is by definition html.
Thats what makes it a web page.

Most browsers can display plain text too but they will usually 
mess up the formatting so that it is hard to read. If you want 
to do that then just create a normal text file.

But if you want the browser4 to display it with any kind of 
formatting you must use html, thats all the browser understands.


> if i wanted to write html in the python i would just write the html
> not sure how else the restate, rewrite

The reasons for writing the html from Python are that you 
can insert dynamic content into the html, such as names 
or values from a database, or from another file:

#
contentStart = 

Here are the current members of my club:
"""
contentEnd = """
"""

data = ["Eric Idle", "Michael Palin", "Graham Chapman"]

with open("mywebpage.htm","w") as webpage:
  webpage.write(contentStart)
  webpage.write("\n")
  for name in data:
 webpage.write("%s" % name)
   webpage.write("\n")
   webpage.write(contentEnd)
###

Now you can regenerate the web file by just adding names to data.
(and you could populate data from a text file or database etc)
 
But if you want that to display sensibly in a web browser you 
need to use html.

HTH,

Alan G.


--- On Wed, 6/15/11, ALAN GAULD  wrote:


>From: ALAN GAULD 
>Subject: Re: [Tutor] using python to write web page
>To: "Michael bridges" 
>Cc: "tutor@python.org"  
>Date: Wednesday, June 15, 2011, 4:54 PM
>
>
>
>
>From: Michael bridges 
>
>To: Alan Gauld 
>Sent: Thursday, 16 June, 2011 0:43:35
>Subject: Re: [Tutor] using python to write web page
>
>> i do not want to write html, not in whole or in part, nor in reference.
>> just 100% python. not from a server.
>
>The code I showed was 100%  Python that created an html file 
>that could be opened in a browser without a  server.
>
>> i want to write the python only code and have python 
>> only code make the web page that can be read on 
>> any computer with a web browser.
>
>Thats exactly what my code does.
>
>> no server use, no server down load, no server at all.
>
>Thats exactly what my code does.
>
>> no python interrupter.
>
>No idea what you mean here?
>If you mean no Python interpreter how do you expect to  
>execute the python code?
>If you mean you want to read the resultant file without 
>using Python then thats exactly what my code does.
>
>So does my sample code meet your needs, or is there 
>some other requirement that you haven't explained yet?
>
>
>--- On Wed, 6/15/11, Alan Gauld  wrote:
>
>> Python can create html  files so if you only want to  create
>> an html file just do it:
>> 
>> content = """
>> 
>> Hello world
>> """
>> 
>> open("myfile.htm","w").write(contents)
>> 
>
>Alan G
> ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] GTALK and PYTHON

2011-06-16 Thread Dipo Elegbede
Hi All,

I want to write an application in python with which I can log into gtalk.

Kindly point me to any documentation with which I can move on.

Advices too would be of great help..

Thanks.

-- 
Elegbede Muhammed Oladipupo
OCA
+2348077682428
+2347042171716
www.dudupay.com
Mobile Banking Solutions | Transaction Processing | Enterprise Application
Development
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using python to write web page

2011-06-16 Thread Walter Prins
Have a look at the Pyjamas project: http://pyjs.org/  It sounds like what
you want.

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


Re: [Tutor] using python to write web page

2011-06-16 Thread Alan Gauld

"Walter Prins"  wrote

Have a look at the Pyjamas project: http://pyjs.org/  It sounds like 
what

you want.


I hadn't considered using pyjamas outside a web server environment
but I guess you could since it just creates an html/javascript file. 
It

depends on what the OP wants to put in his "web page" but Pyjamas
would certainly allow him to create an html page from pure Python
which is what he says he wants.

But for static data, raw html is IMHO easier to use. Pyjamas is more
useful for building web applications than for generating single pages.
But if he prefers to use python for everything pyjamas may well
work for him.

good call,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Tutor Digest, Vol 88, Issue 56

2011-06-16 Thread Steven D'Aprano

WolfRage wrote:


CODE BELOW
#!/usr/bin/python3
"""With this method I can make the class "Screen" become "stdscr" but if
I refernce any of the new methods or properties the applications
promptly fails and notifies me that the attribute does not exist."""


That's because it doesn't exist.

stdscr is a curses window, which does not have an attribute "height". I 
think you want


self.Screen.getmaxyx()

which will return a tuple (height, width) of the window.




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


Re: [Tutor] GTALK and PYTHON

2011-06-16 Thread Timo

On 16-06-11 11:00, Dipo Elegbede wrote:

Hi All,

I want to write an application in python with which I can log into gtalk.

Kindly point me to any documentation with which I can move on.
Not to be rude, but did you use Google before asking here? There are a 
lot of examples and how-to's.
You could also look at the program Emesene, they have a feature to login 
to GTalk.


Cheers,
Timo



Advices too would be of great help..

Thanks.

--
Elegbede Muhammed Oladipupo
OCA
+2348077682428
+2347042171716
www.dudupay.com 
Mobile Banking Solutions | Transaction Processing | Enterprise 
Application Development



___
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


[Tutor] Weird tkFont behavior

2011-06-16 Thread Steve Willoughby
I'm probably just doing something stupid, but I can't spot the error.  I 
hope someone here can see what I'm looking past, or knows of a known 
issue with these widgets.


I'm writing a GUI app using ttk widgets in Tkinter, which includes text 
displayed in a regular text widget.  Some text in the widget will be in 
Italics, some bold, some both, some regular.  I assigned those font 
effects as tags for the text widget as you can see in the code below.


However, when I run it, about 10% of the time, the text widget will come 
up with random fonts (e.g., bold italic instead of plain roman), even 
though I confirmed that it's using 'rm' as the tag when inserting the 
text into the widget.


The initial setup code includes:
f = Tkinter.Text(self, ...)
f.tag_config('rm', font=tkFont.Font(family='Helvetica', size=10, 
weight=tkFont.NORMAL, slant-tkFont.ROMAN))
f.tag_config('bf', font=tkFont.Font(family="Helvetica', size=10, 
weight=tkFont.BOLD, slant=tkFont.ROMAN))
f.tag_config('it', font=tkFont.Font(family="Helvetica', size=10, 
weight=tkFont.NORMAL, slant=tkFont.ITALIC))
f.tag_config('bi', font=tkFont.Font(family="Helvetica', size=10, 
weight=tkFont.BOLD, slant=tkFont.ITALIC))
f.tag_config('ref', font=tkFont.Font(family="Helvetica', size=10, 
weight=tkFont.NORMAL, slant=tkFont.ITALIC), foreground='blue', 
underline=True)


text is inserted simply by:
f.insert(END, text, 'rm')

and yet some of the time, even though the 'rm' tag is there, I get one 
of the other fonts I configured for the other tags.  Do I need to keep 
other references to the tkFont objects somewhere else or something?



--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] File parsing

2011-06-16 Thread Neha P
Hi all,
I know below query may sound silly, but can somebody suggest any better way of 
doing this:

It would be helpful.

I need to read a file line by line and print each line starting from the last 
word first:
 
C:\Python26>type file_reversing_program.txt

import sys
import string

f_obj=open(sys.argv[1],"r")

for eachline in f_obj:
   eachline=eachline[ :-1]# to eliminate the trailing "\n" 
    list_words=eachline.split(" ")
   list_words[0]=list_words[0]+"\n"# to add "\n" so that after line 1 is 
printed, line 2 should start on a new line 
    list_words.reverse()
    for every_word in list_words:
         print every_word,#  'comma' helps in printing words on same line,hence 
for last word we append "\n" 

f_obj.close()

C:\Python26>type input_file.txt

"Hi ther, how are you?"
I are doing fine, thank you.

C:\Python26>file_reversing_program.py input_file.txt

you?" are how ther,"Hi
you. thank fine, doing are I

Is there a better way of doing the above program, mainly the text highlighted 
in yellow,
Also if that is settled can there be a logic for getting the ouput more 
properly formatted (for text in blue) , 
say giving an output like  :
"you? are how ther, Hi"
you. thank fine, doing are I



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


Re: [Tutor] Weird tkFont behavior

2011-06-16 Thread Steve Willoughby

On 16-Jun-11 09:52, Steve Willoughby wrote:

I'm probably just doing something stupid, but I can't spot the error. I
hope someone here can see what I'm looking past, or knows of a known
issue with these widgets.


I think I solved it, actually.. as I was typing this up, I wondered in 
passing about this:



of the other fonts I configured for the other tags. Do I need to keep
other references to the tkFont objects somewhere else or something?


and I think that was it.  I recoded it so that I store the Font objects 
independently of the tag configuration call, in case they were getting 
destroyed accidentally somewhere (I know you have to do that with other 
Tk objects such as images), and now I can't get the app to fail.


If interested, here's the new setup code:

|  f=Tkinter.Text(self, ...)
|
|  self.font_tags = {}
|  for tag, weight, slant, others in (
|('rm', tkFont.NORMAL, tkFont.ROMAN,  {}),
|('bf', tkFont.BOLD,   tkFont.ROMAN,  {}),
|('it', tkFont.NORMAL, tkFont.ITALIC, {}),
|('bi', tkFont.BOLD,   tkFont.ITALIC, {}),
|('ref',tkFont.NORMAL, tkFont.ITALIC, {'foreground':'blue', 
'underline':True}),

|  ):
|  self.font_tags[tag] = tkFont.Font(family='Helvetica', size=10, 
weight=weight, slant=slant)

|  f.tag_config(tag, font=self.font_tags[tag], **others)


(Added "|" in the left column to hopefully get the text to indent right 
with some mail readers.  That's not part of the code, of course.)


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Beginner puzzle with unpacking argv

2011-06-16 Thread Lisi
Copied and pasted:
>From text of Learn Python the Hard Way

1 from sys import argv
2
3 script, user_name = argv

>From my script (typed, not copied and pasted, but copied and pasted from my 
script to here):

1 from sys import argv
2 
3 script, user_name = argv

I have tried every permutation of white space I could think of that might have 
looked like the original, but I always get the same error:

lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex14.py
Traceback (most recent call last):
  File "ex14.py", line 3, in 
script,  user_name=argv
ValueError: need more than 1 value to unpack

I haven't got past line 3 yet!!  I know that you need the right number of 
variables, but my script has got as many as the original - it's the same as 
the original!

Help!

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


Re: [Tutor] Beginner puzzle with unpacking argv

2011-06-16 Thread Steve Willoughby

On 16-Jun-11 10:10, Lisi wrote:


1 from sys import argv
2
3 script, user_name = argv

I have tried every permutation of white space I could think of that might have
looked like the original, but I always get the same error:


That will work ONLY if argv has at least 2 values in it.  Your source 
code is ok as far as it goes.  Try running your script with two command 
line arguments and see what you get.  (The first argument should be the 
name of your script, incidentally).


If your script were named foo.py, then running the command:
  > foo.py

would give you the error you see because argv only has 1 thing in it and 
you're trying to retrieve two.  If you ran it as:

  > foo.py bar

that should work, and script would be "foo.py" and user_name would be "bar".

You could check len(argv) first to see how many items you have before 
you try to get two values from it.


For more sophisticated argument handling, you could look at the optparse 
or argparse modules (but that's beyond the beginner level--something to 
keep in mind for later).


--
Steve Willoughby / st...@alchemy.com
"A ship in harbor is safe, but that is not what ships are built for."
PGP Fingerprint 4615 3CCE 0F29 AE6C 8FF4 CA01 73FE 997A 765D 696C
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner puzzle with unpacking argv

2011-06-16 Thread Joel Goldstick
On Thu, Jun 16, 2011 at 1:10 PM, Lisi  wrote:

> Copied and pasted:
> >From text of Learn Python the Hard Way
>
> 1 from sys import argv
> 2
> 3 script, user_name = argv
>
> >From my script (typed, not copied and pasted, but copied and pasted from
> my
> script to here):
>
> 1 from sys import argv
> 2
> 3 script, user_name = argv
>
> I have tried every permutation of white space I could think of that might
> have
> looked like the original, but I always get the same error:
>
> lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex14.py
> Traceback (most recent call last):
>  File "ex14.py", line 3, in 
>script,  user_name=argv
> ValueError: need more than 1 value to unpack
>
> I haven't got past line 3 yet!!  I know that you need the right number of
> variables, but my script has got as many as the original - it's the same as
> the original!
>
> Help!
>
> Lisi
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

when you run the program you are not giving it any arguments to read.
argv[0] is the name of your script.  But you are not providing a value for
user_name to consume

Try this:

lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex14.py Lisi





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


Re: [Tutor] File parsing

2011-06-16 Thread James Reynolds
use split on the list to split it up. search each element for something
like:

if '"' == element[:-1]:

if that evaluation is True, I would remove the quote mark from the word on
the right side, and place a new one on the left side using something like
'"' + element.

I would do the same thing for the other side in the same for loop, instead
the evaluation would be:

if '"' == element[:1]:




On Thu, Jun 16, 2011 at 1:03 PM, Neha P  wrote:

> Hi all,
> I know below query may sound silly, but can somebody suggest any better way
> of doing this:
> It would be helpful.
>
> I need to read a file line by line and print each line starting from the
> last word first:
>
> C:\Python26>type file_reversing_program.txt
> import sys
> import string
>
> f_obj=open(sys.argv[1],"r")
>
> for eachline in f_obj:
> eachline=eachline[ :-1] # to eliminate the trailing "\n"
> list_words=eachline.split(" ")
> list_words[0]=list_words[0]+"\n" # to add "\n" so that after line 1 is
> printed, line 2 should start on a new line
> list_words.reverse()
> for every_word in list_words:
>  print every_word, #  'comma' helps in printing words on same
> line,hence for last word we append "\n"
>
> f_obj.close()
>
> C:\Python26>type input_file.txt
> "Hi ther, how are you?"
> I are doing fine, thank you.
>
> C:\Python26>file_reversing_program.py input_file.txt
> you?" are how ther, "Hi
> you. thank fine, doing are I
>
> Is there a better way of doing the above program, mainly the text
> highlighted in yellow,
> Also if that is settled can there be a logic for getting the ouput more
> properly formatted (for text in blue) ,
> say giving an output like  :
> "you? are how ther, Hi"
> you. thank fine, doing are I
>
>
> Thanks,
> Neha
>
>
> ___
> 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] Beginner puzzle with unpacking argv

2011-06-16 Thread Peter Lavelle
If you need to process command line arguments then the argparse module 
may also be useful to you. More info can be found here: 
http://docs.python.org/library/argparse.html


Regards

Peter Lavelle

On 16/06/11 19:03, Steve Willoughby wrote:

On 16-Jun-11 10:10, Lisi wrote:


1 from sys import argv
2
3 script, user_name = argv

I have tried every permutation of white space I could think of that 
might have

looked like the original, but I always get the same error:


That will work ONLY if argv has at least 2 values in it.  Your source 
code is ok as far as it goes.  Try running your script with two 
command line arguments and see what you get.  (The first argument 
should be the name of your script, incidentally).


If your script were named foo.py, then running the command:
> foo.py

would give you the error you see because argv only has 1 thing in it 
and you're trying to retrieve two.  If you ran it as:

> foo.py bar

that should work, and script would be "foo.py" and user_name would be 
"bar".


You could check len(argv) first to see how many items you have before 
you try to get two values from it.


For more sophisticated argument handling, you could look at the 
optparse or argparse modules (but that's beyond the beginner 
level--something to keep in mind for later).





--
LinkedIn Profile: http://linkedin.com/in/pmjlavelle
Twitter: http://twitter.com/pmjlavelle

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


Re: [Tutor] File parsing

2011-06-16 Thread Neha P
Thanks James

I guess i have to use the same code for text in yellow... seems like ther's no 
other way... 

Regards,
Neha


From: James Reynolds 
To: Neha P 
Cc: "tutor@python.org" 
Sent: Thursday, June 16, 2011 2:43 PM
Subject: Re: [Tutor] File parsing


use split on the list to split it up. search each element for something like:

if '"' == element[:-1]:

if that evaluation is True, I would remove the quote mark from the word on the 
right side, and place a new one on the left side using something like '"' + 
element.

I would do the same thing for the other side in the same for loop, instead the 
evaluation would be:

if '"' == element[:1]:





On Thu, Jun 16, 2011 at 1:03 PM, Neha P  wrote:

Hi all,
>I know below query may sound silly, but can somebody suggest any better way of 
>doing this:
>
>It would be helpful.
>
>
>I need to read a file line by line and print each line starting from the last 
>word first:
> 
>C:\Python26>type file_reversing_program.txt
>
>import sys
>import string
>
>
>f_obj=open(sys.argv[1],"r")
>
>
>for eachline in f_obj:
>   eachline=eachline[ :-1]# to eliminate the trailing "\n" 
>    list_words=eachline.split(" ")
>   list_words[0]=list_words[0]+"\n"# to add "\n" so that after line 1 is 
>printed, line 2 should start on a new line 
>    list_words.reverse()
>    for every_word in list_words:
>         print every_word,#  'comma' helps in printing words on same 
>line,hence for last word we append "\n" 
>
>
>f_obj.close()
>
>
>C:\Python26>type input_file.txt
>
>"Hi ther, how are you?"
>I are doing fine, thank you.
>
>
>C:\Python26>file_reversing_program.py input_file.txt
>
>you?" are how ther,"Hi
>you. thank fine, doing are I
>
>
>Is there a better way of doing the above program, mainly the text highlighted 
>in yellow,
>Also if that is settled can there be a logic for getting the ouput more 
>properly formatted (for text in blue) , 
>say giving an output like  :
>"you? are how ther, Hi"
>you. thank fine, doing are I
>
>
>
>
>
>Thanks,
>Neha
>
>___
>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] File parsing

2011-06-16 Thread Lisi
Thanks, Neha.  I am sending this back on list so that those who are more 
knowledgeable than I am can help you.  

On Thursday 16 June 2011 18:47:39 Neha P wrote:
> Hi Lisi,
> Below is the code and I have prefixed(-->) to the line code which i'm
> mainly concerned about and have commented using #
>
>
>
> for eachline in f_obj:
>
> # to eliminate the trailing "\n"
> -->eachline=eachline[ : -1]  
>  list_words=eachline.split(" ")
> # to add "\n" so that after line 1 is printed, line 2 should start on a new
> line""" -->list_words[0]=list_words[0]+"\n"
> list_words.reverse()
> # 'comma' helps in printing words on same
> # line, hence for last word have to append "\n" as done in above stmt
>  for every_word in list_words:
>   print every_word,
>
>
> Thanks,
> Neha
>
>
>
>
> 
> From: Lisi 
> To: Neha P 
> Sent: Thursday, June 16, 2011 1:19 PM
> Subject: Re: [Tutor] File parsing
>
> On Thursday 16 June 2011 18:03:55 Neha P wrote:
> > Hi all,
> > I know below query may sound silly, but can somebody suggest any better
> > way of doing this:
> >
> > It would be helpful.
> >
> > I need to read a file line by line and print each line starting from the
> > last word first:
> > C:\Python26>type file_reversing_program.txt
> >
> > import sys
> > import string
> >
> > f_obj=open(sys.argv[1],"r")
> >
> > for eachline in f_obj:
> >eachline=eachline[ :-1]# to eliminate the trailing "\n" 
> > list_words=eachline.split(" ")
> >list_words[0]=list_words[0]+"\n"# to add "\n" so that after line 1 is
> > printed, line 2 should start on a new line  list_words.reverse()
> > for every_word in list_words:
> >  print every_word,#  'comma' helps in printing words on same
> > line,hence for last word we append "\n" 
> >
> > f_obj.close()
> >
> > C:\Python26>type input_file.txt
> >
> > "Hi ther, how are you?"
> > I are doing fine, thank you.
> >
> > C:\Python26>file_reversing_program.py input_file.txt
> >
> > you?" are how ther,"Hi
> > you. thank fine, doing are I
> >
> > Is there a better way of doing the above program, mainly the text
> > highlighted in yellow, Also if that is settled can there be a logic for
> > getting the ouput more properly formatted (for text in blue) , say giving
> > an output like  :
> > "you? are how ther, Hi"
> > you. thank fine, doing are I
> >
> >
> >
> > Thanks,
> > Neha
>
> Colours haven't transmitted (I use plain text).
>
> Could you identify the blocks of text by some other means?
>
> Thanks,
> Lisi
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File parsing

2011-06-16 Thread spawgi
Hello Neha,

I think this script will do what you want and will also fix some of the
issues in your original code -

  1 #! /usr/bin/python
  2
  3 import curses.ascii
  4 import string
  5 import sys
  6
  7 with open(sys.argv[1],'r') as input:
  8   for eachline in input:
  9 eachline = eachline.strip('\n')
 10 if curses.ascii.isalpha(eachline[0]):
 11   first_char = ''
 12 else:
 13   first_char = eachline[0]
 14 last_char = eachline[len(eachline)-1]
 15 eachline = eachline.lstrip(first_char)
 16 eachline = eachline.rstrip(last_char)
 17 words = eachline.split(' ')
 18 outline = first_char
 19 for word in reversed(words):
 20   outline = outline + str(word) + ' '
 21 outline = outline.rstrip() + last_char + '\n'
 22 print outline
---

It is much more explicit. It considers some special cases and gives the
output in the format desired by you.

Regards,
SWP

On Fri, Jun 17, 2011 at 12:21 AM, Neha P  wrote:

> Thanks James
>
> I guess i have to use the same code for text in yellow... seems like ther's
> no other way...
>
> Regards,
> Neha
> --
> *From:* James Reynolds 
> *To:* Neha P 
> *Cc:* "tutor@python.org" 
> *Sent:* Thursday, June 16, 2011 2:43 PM
> *Subject:* Re: [Tutor] File parsing
>
> use split on the list to split it up. search each element for something
> like:
>
> if '"' == element[:-1]:
>
> if that evaluation is True, I would remove the quote mark from the word on
> the right side, and place a new one on the left side using something like
> '"' + element.
>
> I would do the same thing for the other side in the same for loop, instead
> the evaluation would be:
>
> if '"' == element[:1]:
>
>
>
>
> On Thu, Jun 16, 2011 at 1:03 PM, Neha P  wrote:
>
> Hi all,
> I know below query may sound silly, but can somebody suggest any better way
> of doing this:
> It would be helpful.
>
> I need to read a file line by line and print each line starting from the
> last word first:
>
> C:\Python26>type file_reversing_program.txt
> import sys
> import string
>
> f_obj=open(sys.argv[1],"r")
>
> for eachline in f_obj:
> eachline=eachline[ :-1] # to eliminate the trailing "\n"
> list_words=eachline.split(" ")
> list_words[0]=list_words[0]+"\n" # to add "\n" so that after line 1 is
> printed, line 2 should start on a new line
> list_words.reverse()
> for every_word in list_words:
>  print every_word, #  'comma' helps in printing words on same
> line,hence for last word we append "\n"
>
> f_obj.close()
>
> C:\Python26>type input_file.txt
> "Hi ther, how are you?"
> I are doing fine, thank you.
>
> C:\Python26>file_reversing_program.py input_file.txt
> you?" are how ther, "Hi
> you. thank fine, doing are I
>
> Is there a better way of doing the above program, mainly the text
> highlighted in yellow,
> Also if that is settled can there be a logic for getting the ouput more
> properly formatted (for text in blue) ,
> say giving an output like  :
> "you? are how ther, Hi"
> you. thank fine, doing are I
>
>
> Thanks,
> Neha
>
>
> ___
> 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
>
>


-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] File parsing

2011-06-16 Thread spawgi
Output on your sample input -

$./file_reversing_program.py input.txt
"you? are how there, Hi"

you thank fine, doing am I.


On Fri, Jun 17, 2011 at 1:26 AM,  wrote:

> Hello Neha,
>
> I think this script will do what you want and will also fix some of the
> issues in your original code -
> 
>   1 #! /usr/bin/python
>   2
>   3 import curses.ascii
>   4 import string
>   5 import sys
>   6
>   7 with open(sys.argv[1],'r') as input:
>   8   for eachline in input:
>   9 eachline = eachline.strip('\n')
>  10 if curses.ascii.isalpha(eachline[0]):
>  11   first_char = ''
>  12 else:
>  13   first_char = eachline[0]
>  14 last_char = eachline[len(eachline)-1]
>  15 eachline = eachline.lstrip(first_char)
>  16 eachline = eachline.rstrip(last_char)
>  17 words = eachline.split(' ')
>  18 outline = first_char
>  19 for word in reversed(words):
>  20   outline = outline + str(word) + ' '
>  21 outline = outline.rstrip() + last_char + '\n'
>  22 print outline
>
> ---
>
> It is much more explicit. It considers some special cases and gives the
> output in the format desired by you.
>
> Regards,
> SWP
>
> On Fri, Jun 17, 2011 at 12:21 AM, Neha P  wrote:
>
>> Thanks James
>>
>> I guess i have to use the same code for text in yellow... seems like
>> ther's no other way...
>>
>> Regards,
>> Neha
>> --
>> *From:* James Reynolds 
>> *To:* Neha P 
>> *Cc:* "tutor@python.org" 
>> *Sent:* Thursday, June 16, 2011 2:43 PM
>> *Subject:* Re: [Tutor] File parsing
>>
>> use split on the list to split it up. search each element for something
>> like:
>>
>> if '"' == element[:-1]:
>>
>> if that evaluation is True, I would remove the quote mark from the word on
>> the right side, and place a new one on the left side using something like
>> '"' + element.
>>
>> I would do the same thing for the other side in the same for loop, instead
>> the evaluation would be:
>>
>> if '"' == element[:1]:
>>
>>
>>
>>
>> On Thu, Jun 16, 2011 at 1:03 PM, Neha P  wrote:
>>
>> Hi all,
>> I know below query may sound silly, but can somebody suggest any better
>> way of doing this:
>> It would be helpful.
>>
>> I need to read a file line by line and print each line starting from the
>> last word first:
>>
>> C:\Python26>type file_reversing_program.txt
>> import sys
>> import string
>>
>> f_obj=open(sys.argv[1],"r")
>>
>> for eachline in f_obj:
>> eachline=eachline[ :-1] # to eliminate the trailing "\n"
>> list_words=eachline.split(" ")
>> list_words[0]=list_words[0]+"\n" # to add "\n" so that after line 1
>> is printed, line 2 should start on a new line
>> list_words.reverse()
>> for every_word in list_words:
>>  print every_word, #  'comma' helps in printing words on same
>> line,hence for last word we append "\n"
>>
>> f_obj.close()
>>
>> C:\Python26>type input_file.txt
>> "Hi ther, how are you?"
>> I are doing fine, thank you.
>>
>> C:\Python26>file_reversing_program.py input_file.txt
>> you?" are how ther, "Hi
>> you. thank fine, doing are I
>>
>> Is there a better way of doing the above program, mainly the text
>> highlighted in yellow,
>> Also if that is settled can there be a logic for getting the ouput more
>> properly formatted (for text in blue) ,
>> say giving an output like  :
>> "you? are how ther, Hi"
>> you. thank fine, doing are I
>>
>>
>> Thanks,
>> Neha
>>
>>
>> ___
>> 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
>>
>>
>
>
> --
> http://spawgi.wordpress.com
> We can do it and do it better.
>



-- 
http://spawgi.wordpress.com
We can do it and do it better.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Beginner puzzle with unpacking argv

2011-06-16 Thread Lisi
On Thursday 16 June 2011 19:03:27 Joel Goldstick wrote:
> On Thu, Jun 16, 2011 at 1:10 PM, Lisi  wrote:
> > Copied and pasted:
> > >From text of Learn Python the Hard Way
> >
> > 1 from sys import argv
> > 2
> > 3 script, user_name = argv
> >
> > >From my script (typed, not copied and pasted, but copied and pasted from
> >
> > my
> > script to here):
> >
> > 1 from sys import argv
> > 2
> > 3 script, user_name = argv
> >
> > I have tried every permutation of white space I could think of that might
> > have
> > looked like the original, but I always get the same error:
> >
> > lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex14.py
> > Traceback (most recent call last):
> >  File "ex14.py", line 3, in 
> >script,  user_name=argv
> > ValueError: need more than 1 value to unpack
> >
> > I haven't got past line 3 yet!!  I know that you need the right number of
> > variables, but my script has got as many as the original - it's the same
> > as the original!

> when you run the program you are not giving it any arguments to read.
> argv[0] is the name of your script.  But you are not providing a value for
> user_name to consume
>
> Try this:
>
> lisi@Tux:~/Python/LearnPythonTheHardWay$ python ex14.py Lisi

Thanks to all of you, guys. :-))  So it wasn't the script that was wrong, it 
was the command I was giving. Doh!

It now runs fine as far as I have gone,  so I can now finish typing the rest 
of the script in.  Hopefully without too many stupid mistakes. :-(

Lisi


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


Re: [Tutor] File parsing

2011-06-16 Thread G . nius . ck

instead of
14 last_char = eachline[len(eachline)-1]
do
14 last_char = eachline[-1] #Which is shorter but the same thing

On , spa...@gmail.com wrote:

Hello Neha,


I think this script will do what you want and will also fix some of the  
issues in your original code -


1 #! /usr/bin/python




2
3 import curses.ascii
4 import string
5 import sys
6
7 with open(sys.argv[1],'r') as input:
8 for eachline in input:



9 eachline = eachline.strip('\n')



10 if curses.ascii.isalpha(eachline[0]):
11 first_char = ''
12 else:
13 first_char = eachline[0]
14 last_char = eachline[len(eachline)-1]




15 eachline = eachline.lstrip(first_char)
16 eachline = eachline.rstrip(last_char)
17 words = eachline.split(' ')
18 outline = first_char
19 for word in reversed(words):




20 outline = outline + str(word) + ' '
21 outline = outline.rstrip() + last_char + '\n'
22 print outline
---





It is much more explicit. It considers some special cases and gives the  
output in the format desired by you.




Regards,
SWP



On Fri, Jun 17, 2011 at 12:21 AM, Neha P mywr...@yahoo.com> wrote:




Thanks James





I guess i have to use the same code for text in yellow... seems like  
ther's no other way...






Regards,
Neha






From: James Reynolds eire1...@gmail.com>
To: Neha P mywr...@yahoo.com>




Cc: "tutor@python.org" tutor@python.org>
Sent: Thursday, June 16, 2011 2:43 PM




Subject: Re: [Tutor] File parsing





use split on the list to split it up. search each element for something  
like:



if '"' == element[:-1]:



if that evaluation is True, I would remove the quote mark from the word  
on the right side, and place a new one on the left side using something  
like '"' + element.






I would do the same thing for the other side in the same for loop,  
instead the evaluation would be:




if '"' == element[:1]:












On Thu, Jun 16, 2011 at 1:03 PM, Neha P mywr...@yahoo.com> wrote:





Hi all,


I know below query may sound silly, but can somebody suggest any better  
way of doing this:




It would be helpful.




I need to read a file line by line and print each line starting from the  
last word first:



C:\Python26>type file_reversing_program.txt






import sys
import string







f_obj=open(sys.argv[1],"r")




for eachline in f_obj:





eachline=eachline[ :-1] # to eliminate the trailing "\n"





list_words=eachline.split(" ")
list_words[0]=list_words[0]+"\n" # to add "\n" so that after line 1 is  
printed, line 2 should start on a new line





list_words.reverse()
sans-serif"> for every_word in list_words:
print every_word, # 'comma' helps in printing words on same line,hence  
for last word we append "\n"







f_obj.close()





C:\Python26>type input_file.txt






"Hi ther, how are you?"
I are doing fine, thank you.







C:\Python26>file_reversing_program.py input_file.txt



you?" are how ther, "Hi





you. thank fine, doing are I




Is there a better way of doing the above program, mainly the text  
highlighted in yellow,




serif">Also if that is settled can there be a logic for getting the ouput  
more properly formatted (for text in blue) ,



say giving an output like :
"you? are how ther, Hi"
you. thank fine, doing are I











Thanks,



Neha








___




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







--
http://spawgi.wordpress.com
We can do it and do it better.





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


[Tutor] Step Value

2011-06-16 Thread Vincent Balmori
"From Steven D'Aprono:
* change the ask_number function
to accept a fourth argument;
* make it optional rather than
required;
* give it a sensible default
value;
* within the function, that value
must then be passed to range
"

Okay, I think I understand it better for the quesiton:
"Improve the function ask_number() so that the function can be called with a 
step value. Make the default value of step 1." Here is the improved function 
and the human_move function that calls it later on. The thing I am unaware of 
doing is making the step value 'optional rather than required' though.

def ask_number(question, low, high, step):
    """Ask for a number within a range."""
    step = 1
    response = None
    while response not in range(low, high, step):
        response = int(input(question))
    return response

def human_move(board, human):
    """Get human move."""  
    legal = legal_moves(board)
    move = None
    while move not in legal:
        move = ask_number("Where will you move? (0 - 8):", 0, NUM_SQUARES, 1)
        if move not in legal:
            print("\nThat square is already occupied, foolish human.  Choose 
another.\n")
    print("Fine...")
    return move    ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Step Value

2011-06-16 Thread Steven D'Aprano

Vincent Balmori wrote:


Okay, I think I understand it better for the quesiton:
"Improve the function ask_number() so that the function can be called with a step 
value. Make the default value of step 1." Here is the improved function and the 
human_move function that calls it later on. The thing I am unaware of doing is making the 
step value 'optional rather than required' though.


You're almost there.


def ask_number(question, low, high, step):
"""Ask for a number within a range."""
step = 1


The line above is inappropriate, because it over-writes the value of 
step that the caller provides. If you call


ask_number(question, 1, 100, 5)

the line step=1 throws away the value 5 and replaces it with 1. Get rid 
of that line altogether, you don't want it.


The last change that needs to be made is to give step a default value. 
The way to do that is to put it in the function parameters. He's an example:


def spam(n=3):
"""Return n slices of yummy spam."""
return "spam "*n

And here it is in use:

>>> spam(4)
'spam spam spam spam '
>>> spam()  # just use the default
'spam spam spam '

Can you see what I did to set the default value for n? Read the function 
definition line "def spam... " carefully.




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


Re: [Tutor] File parsing

2011-06-16 Thread Steven D'Aprano

Neha P wrote:

Thanks James

I guess i have to use the same code for text in yellow... seems like ther's no other way... 


What code in yellow? I see no code in yellow.

This is email, don't assume people can see colours. They may have HTML 
blocked or turned off (very important for security, avoiding some 
viruses and some types of web-bugs, or just because they hate getting 
ugly emails with horrible clashing colours). Or they may be colour-blind 
(about one in twelve men, and one in 200 women), or completely blind and 
reading email with a screen reader.


I will leave you to guess which category I fall into.



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


[Tutor] Step Value

2011-06-16 Thread Vincent Balmori
"def spam(n=3):
    """Return n slices of yummy spam."""
    return "spam "*n

And here it is in use:

>>> spam(4)
'spam spam spam spam '
>>> spam()  # just use the default
'spam spam spam '

Can you see what I did to set the default value for n? Read the function 
definition line "def spam... " carefully.

-- Steven"

This is my new code for a default step value based on your feedback Steve:

def ask_number(question, low, high, step):
    """Ask for a number within a range."""
    response = None
    if step == None:
        step = 1
    while response not in range(low, high, step):
        response = int(input(question))
    return response
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Step Value

2011-06-16 Thread Válas Péter
Please try to reply to the previous letters instead of starting a new thread
each time. Thank you!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor