[Tutor] rstrip in list?

2010-02-09 Thread Ken G.

I printed out some random numbers to a list and use 'print mylist' and
they came out like this:

['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']

I was using 'print mylist.rstrip()' to strip off the '\n'

but kept getting an error of :

AttributeError: 'list' object has no attribute 'rstrip'

My memory must be hazy but I thought I had it working several months ago.

Any idea or suggestion?

TIA, Ken

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


Re: [Tutor] Closing a matplotlib window after show()

2010-02-09 Thread zhengqing gan
Hi,
recently I have the same problem with Matplotlib.
I wrote a wxpython program which can plot a graph when click a button.
When I click the button first time, I got the plot correct, but when I
closed the figure, and clicked the button again, the code crashed. I have to
reopen the program, and the same problem happened.
The solution is that put import matplotlib.pyplot as plt  before you
plot anything. Then everything works fine.
 I don't know why. It seems that when you close the figure, the imported
module or function stop working. It has to be imported every time.



On Tue, Feb 9, 2010 at 5:00 AM, tutor-requ...@python.org wrote:

 Send Tutor mailing list submissions to
tutor@python.org

 To subscribe or unsubscribe via the World Wide Web, visit
http://mail.python.org/mailman/listinfo/tutor
 or, via email, send a message with subject or body 'help' to
tutor-requ...@python.org

 You can reach the person managing the list at
tutor-ow...@python.org

 When replying, please edit your Subject line so it is more specific
 than Re: Contents of Tutor digest...


 Today's Topics:

   1. python (ailx ailx)
   2. Re: python (Luke Paireepinart)
   3. Re: Closing a matplotlib window after show() (Wayne Watson)


 --

 Message: 1
 Date: Tue, 9 Feb 2010 10:54:06 +0800
 From: ailx ailx ailxr...@gmail.com
 To: tutor@python.org
 Subject: [Tutor] python
 Message-ID:
5fc821071002081854k2fa92309ned9389b9f3d91...@mail.gmail.com
 Content-Type: text/plain; charset=iso-8859-1

 python
 -- next part --
 An HTML attachment was scrubbed...
 URL: 
 http://mail.python.org/pipermail/tutor/attachments/20100209/a46c4683/attachment-0001.htm
 

 --

 Message: 2
 Date: Mon, 8 Feb 2010 21:09:59 -0600
 From: Luke Paireepinart rabidpoob...@gmail.com
 To: ailx ailx ailxr...@gmail.com
 Cc: tutor@python.org
 Subject: Re: [Tutor] python
 Message-ID:
dfeb4471002081909t496fc217w355010e085b66...@mail.gmail.com
 Content-Type: text/plain; charset=iso-8859-1

 quite.

 On Mon, Feb 8, 2010 at 8:54 PM, ailx ailx ailxr...@gmail.com wrote:

  python
 
  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
 
 
 -- next part --
 An HTML attachment was scrubbed...
 URL: 
 http://mail.python.org/pipermail/tutor/attachments/20100208/b19399fe/attachment-0001.htm
 

 --

 Message: 3
 Date: Mon, 08 Feb 2010 21:53:39 -0800
 From: Wayne Watson sierra_mtnv...@sbcglobal.net
 To: Eike Welk eike.w...@gmx.net
 Cc: tutor@python.org
 Subject: Re: [Tutor] Closing a matplotlib window after show()
 Message-ID: 4b70f863.1070...@sbcglobal.net
 Content-Type: text/plain; charset=ISO-8859-1; format=flowed

 Hi, I'm not so sure that's true. I have a large 900 line program where
 some original plot code just continues beyond plot() and show(), after
 the user closes the plot window. New code that I put in gets knotted up,
 as far as I can tell. In both cases, I've put print statements after
 show(), but nothing appears in the shell or, if run  by clicking the
 program file, in the DOS-like window that appears.

 Further, I posted this elsewhere, and someone claims to have tried a few
 simple examples with show() at the ended,and they did not get tied up in
 knots when the user closed the window. I'm going to assume he used IDLE,
 or a  straight execute of the file.

 On 2/8/2010 2:23 PM, Eike Welk wrote:
  Hello Wayne!
 
  On Monday February 8 2010 20:54:27 Wayne Watson wrote:
 
  The basic problem is the show(). One person checked out the examples I
  provided and found show() to operate fine. On my XP machine the program
  I'm modifying has plot code someone put in a year or two ago, and it all
  works fine. My code produces the desired plot, but gets hung up on
 show().
 
  The behavior that you describe, is the normal behavior of Matplotlib:
 When you
  call show(), the program gets stuck.
 
  Therefore the call to show is always the last statement in the example
  programs. Show returns when the last plot window is closed, and in
 principle
  the program could then continue.
 
  If you want to look at plots while the program is running, you must use
  Ipython. This is a modified Python interpreter, that contains special
 code to
  change the way how Matplotlib works.
 
  http://ipython.scipy.org/moin/
 
 
  Eike.
  ___
  Tutor maillist  -  Tutor@python.org
  To unsubscribe or change subscription options:
  http://mail.python.org/mailman/listinfo/tutor
 
 

 --
 Crime is way down. War is declining. And that's far from the good
 news. -- Steven Pinker (and other sources) Why is this true, but yet
 the media says otherwise? The media knows very well how to manipulate us

Re: [Tutor] rstrip in list?

2010-02-09 Thread Steven D'Aprano
On Wed, 10 Feb 2010 02:28:43 am Ken G. wrote:
 I printed out some random numbers to a list and use 'print mylist'
 and they came out like this:

 ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']

 I was using 'print mylist.rstrip()' to strip off the '\n'

 but kept getting an error of :

 AttributeError: 'list' object has no attribute 'rstrip'

You have to apply rstrip to each item in the list, not the list itself.

Here are two ways to do it:

#1: modify the list in a for-loop 
for i, item in enumerate(mylist):
mylist[i] = item.rstrip()

#2: make a new list with a list comprehension
mylist = [item.rstrip() for item in mylist]


Of the two, I prefer the second.



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


Re: [Tutor] rstrip in list?

2010-02-09 Thread Ken G.


Kent Johnson wrote:

On Tue, Feb 9, 2010 at 10:28 AM, Ken G. beach...@insightbb.com wrote:
  

I printed out some random numbers to a datafile and use 'print mylist' and
they came out like this:

['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']



How are you generating this list? You should be able to create it
without the \n. That would be better than stripping them out.
  

I inputting some random numbers into a database and then created a list
and appended the list as it read the database.
  

I was using 'print mylist.rstrip()' to strip off the '\n'

but kept getting an error of :

AttributeError: 'list' object has no attribute 'rstrip'

My memory must be hazy but I thought I had it working several months ago.

Any idea or suggestion?



Use a list comprehension or map():

In [1]: l = ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']

In [2]: [ i.rstrip() for i in l ]
Out[2]: ['102', '231', '463', '487', '555', '961']

In [3]: map(str.rstrip, l)
Out[3]: ['102', '231', '463', '487', '555', '961']

Kent
  


My database file has numbers of the same exact length that need to be 
sorted.  I am using
'mylist.sort()' as one of the command.  Actually, I will be using 
'mylist.reverse' after

that command.  I am still in a learning mode.  Thanks.

Ken



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


Re: [Tutor] rstrip in list?

2010-02-09 Thread bob gailer

Ken G. wrote:

I printed out some random numbers to a list and use 'print mylist' and
they came out like this:

['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']

I was using 'print mylist.rstrip()' to strip off the '\n'

but kept getting an error of :

AttributeError: 'list' object has no attribute 'rstrip'


rstrip is a string method, not a list method. You must apply it to each 
element in the list. One way is:


print [item.rstrip() for iten in mylist]

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] rstrip in list?

2010-02-09 Thread Kent Johnson
On Tue, Feb 9, 2010 at 11:09 AM, Ken G. beach...@insightbb.com wrote:

 Kent Johnson wrote:

 On Tue, Feb 9, 2010 at 10:28 AM, Ken G. beach...@insightbb.com wrote:


 I printed out some random numbers to a datafile and use 'print mylist' and
 they came out like this:

 ['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']


 How are you generating this list? You should be able to create it
 without the \n. That would be better than stripping them out.


 I inputting some random numbers into a database and then created a list
 and appended the list as it read the database.

If you show the code for this perhaps we can figure out where the
newlines are coming from.

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


Re: [Tutor] rstrip in list? (SOLVED)

2010-02-09 Thread Ken G.

There was so many different solutions presented here to me.

Thanks to all.  By adding '.strip('\n') to the last two lines below, it 
came out:


Sorted List

['102', '231', '463', '487', '555', '961']

for line in file.readlines():
   print line.strip('\n'),
   mylist.append(line.strip('\n'))

Further work and studying needed here.   LOL.

Ken

Steven D'Aprano wrote:

On Wed, 10 Feb 2010 02:28:43 am Ken G. wrote:
  

I printed out some random numbers to a list and use 'print mylist'
and they came out like this:

['102\n', '231\n', '463\n', '487\n', '555\n', '961\n']

I was using 'print mylist.rstrip()' to strip off the '\n'

but kept getting an error of :

AttributeError: 'list' object has no attribute 'rstrip'



You have to apply rstrip to each item in the list, not the list itself.

Here are two ways to do it:

#1: modify the list in a for-loop 
for i, item in enumerate(mylist):

mylist[i] = item.rstrip()

#2: make a new list with a list comprehension
mylist = [item.rstrip() for item in mylist]


Of the two, I prefer the second.



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


[Tutor] Embarrassed...

2010-02-09 Thread Ken G.
I am a little embarrassed.  I just happen to found a program I wrote in 
December that create random numbers into a file, copy the numbers into a 
list, print the numbers unsorted and sorted from the list without 
printing '\n'.  Nevertheless, I do thanks you all for trying to help me out.


Ken


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


Re: [Tutor] Closing a matplotlib window after show()

2010-02-09 Thread Wayne Watson
Form me the solution is getting into interactive mode, which I had never 
heard of until this morning.


On 2/9/2010 9:04 AM, Wayne Watson wrote:
Well, you are correct. Finally, my latest post to the MPL list caught 
the eye of John Hunter. I think he wrote MPL. The way out is 
interactive use. One problem I've  had  with  Python packages they 
seem to based on some other

...

--
Crime is way down. War is declining. And that's far from the good 
news. -- Steven Pinker (and other sources) Why is this true, but yet 
the media says otherwise? The media knows very well how to manipulate us 
(see limbic, emotion, $$). -- WTW

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


[Tutor] apache2.2 - django deployment

2010-02-09 Thread Joson
Hi all,
I wanna deploy a django program on apache2.2. So I downloaded 
mod_wsgi-win32-ap22py26-3.0.so and copied it into apache2.2/modules/,
renamed mod_wsgi.so.

in httpd.conf I appended sentence:
LoadModule python_module modules/mod_wsgi.so

Then It failed to start the server:

httpd.exe: Syntax error on line 128 of C:/Program Files/Apache Software
Foundati
on/Apache2.2/conf/httpd.conf: Can't locate API module structure
`python_module'
in file C:/Program Files/Apache Software
Foundation/Apache2.2/modules/mod_wsgi.s
o: No error
Note the errors or messages above, and press the ESC key to exit.  21...

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


Re: [Tutor] apache2.2 - django deployment

2010-02-09 Thread Glen Zangirolami
Joson,

Everything looks good but I think the *module name is incorrect.*

Change: LoadModule *python_module *modules/mod_wsgi.so
To: LoadModule *wsgi_module *modules/mod_wsgi.so

Glen


On Tue, Feb 9, 2010 at 2:27 PM, Joson zhuchu...@gmail.com wrote:

 Hi all,
 I wanna deploy a django program on apache2.2. So I downloaded 
 mod_wsgi-win32-ap22py26-3.0.so and copied it into apache2.2/modules/,
 renamed mod_wsgi.so.

 in httpd.conf I appended sentence:
 LoadModule python_module modules/mod_wsgi.so

 Then It failed to start the server:

 httpd.exe: Syntax error on line 128 of C:/Program Files/Apache Software
 Foundati
 on/Apache2.2/conf/httpd.conf: Can't locate API module structure
 `python_module'
 in file C:/Program Files/Apache Software
 Foundation/Apache2.2/modules/mod_wsgi.s
 o: No error
 Note the errors or messages above, and press the ESC key to exit.  21...

 Pls help me. thanks:)


 ___
 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] apache2.2 - django deployment

2010-02-09 Thread Glen Zangirolami
Joson,

Everything looks good but I think the *module name is incorrect.*

Change: LoadModule *python_module *modules/mod_wsgi.so
To: LoadModule *wsgi_module *modules/mod_wsgi.so

Glen


On Tue, Feb 9, 2010 at 2:27 PM, Joson zhuchu...@gmail.com wrote:

 Hi all,
 I wanna deploy a django program on apache2.2. So I downloaded 
 mod_wsgi-win32-ap22py26-3.0.so and copied it into apache2.2/modules/,
 renamed mod_wsgi.so.

 in httpd.conf I appended sentence:
 LoadModule python_module modules/mod_wsgi.so

 Then It failed to start the server:

 httpd.exe: Syntax error on line 128 of C:/Program Files/Apache Software
 Foundati
 on/Apache2.2/conf/httpd.conf: Can't locate API module structure
 `python_module'
 in file C:/Program Files/Apache Software
 Foundation/Apache2.2/modules/mod_wsgi.s
 o: No error
 Note the errors or messages above, and press the ESC key to exit.  21...

 Pls help me. thanks:)


 ___
 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] Exiting a Tkinter Program-- An Anomaly or two

2010-02-09 Thread Wayne Watson
I'm looking a 1800+ line someone else wrote. It uses one large dialog 
for menus, and has a large area for images. A few menus open small 
dialogs, for example, to enter a file name. The File menu has an exit 
choice. The only other exit is the x in the upper right corner of the 
large dialog. I'm pretty sure that menu is coded to quit via a shoft def 
in the program.


def Quite(self)
   self.running = False
   self.master.quit()

I see no other code to quit. If I use Exit, the program does not quite. 
If I then use the x, it quits and the shell script is left open for a 
command. Any ideas why Quit doesn't work? It's accessible  via a

self.mainMenu.add_command(.. command=self.Quit)
I  had not turned the program loose by using a menu or touching any 
controls.


If I cause the program to print to the shell, and then use x to exit 
that it hangs the shell. Why? When I x the shell, it tells me  the prog 
is running. Do I want to kill it. Yes,kills the shell  window.


The above seem abnormal to me. Comments?

--
Crime is way down. War is declining. And that's far from the good 
news. -- Steven Pinker (and other sources) Why is this true, but yet 
the media says otherwise? The media knows very well how to manipulate us 
(see limbic, emotion, $$). -- WTW

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


[Tutor] NameError: global name 'celsius' is not defined (actually, solved)

2010-02-09 Thread David

Hi guys,

I just wrote this message, but after restarting ipython all worked fine.
How is it to be explained that I first had a namespace error which, 
after a restart (and not merely a new run Sande_celsius-main.py), went 
away? I mean, surely the namespace should not be impacted by ipython at 
all!?


Many thanks,

David


Dear List,

this should be so basic that I feel bad asking this question here, but I 
don't get it.


I am having a look at Sande's book Hello World. The topic is 
'Modules', and the code comes directly from the book.


I have two files: Sande_celsius-main.py and Sande_my_module.py.

I import the latter from within the former.

# file: Sande_celsius-main.py
from Sande_my_module import c_to_f
celsius = float(raw_input(Enter a temperature in Celsius: ))
fahrenheit = c_to_f(celsius)
print That's , fahrenheit,  degrees Fahrenheit


# this is the file Sande_my_module.py
# we're going to use it in another program
def c_to_f(celsius):
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit

When I run Sande_celsius-main.py, I get the following error:

NameError: global name 'celsius' is not defined
WARNING: Failure executing file: Sande_celsius-main.py

First of all, this error message doesn't exactly tell me _where_ the 
problem is, does it? It could be a problem with(in) the imported 
function c_to_f... I wish he would tell me: Problem in file x, line y.


Secondly, the name celsius in the global namespace of ~-main.py is 
merely a variable, which later is then used as a parameter to c_to_f. I 
do not see a problem here.


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


Re: [Tutor] NameError: global name 'celsius' is not defined (actually, solved)

2010-02-09 Thread wesley chun
 I just wrote this message, but after restarting ipython all worked fine.
 How is it to be explained that I first had a namespace error which, after a
 restart (and not merely a new run Sande_celsius-main.py), went away? I
 mean, surely the namespace should not be impacted by ipython at all!?
 :
 # file: Sande_celsius-main.py
 from Sande_my_module import c_to_f
 celsius = float(raw_input(Enter a temperature in Celsius: ))
 fahrenheit = c_to_f(celsius)
 print That's , fahrenheit,  degrees Fahrenheit

 # this is the file Sande_my_module.py
 # we're going to use it in another program
 def c_to_f(celsius):
    fahrenheit = celsius * 9.0 / 5 + 32
    return fahrenheit

 When I run Sande_celsius-main.py, I get the following error:

 NameError: global name 'celsius' is not defined
 WARNING: Failure executing file: Sande_celsius-main.py


Python interpreters including the standard one or IPython should tell
you a lot more than that. how are you executing this code? would it be
possible to do so from the command-line? you should get a more verbose
error message that you can post here.

best regards,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
Python Fundamentals, Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] NameError: global name 'celsius' is not defined (actually, solved)

2010-02-09 Thread David

Hello Wesley,

thanks for your reply. I was surprised about the limited information 
too. Sadly (?), I can't reproduce the error any more...


David



On 10/02/10 11:13, wesley chun wrote:

I just wrote this message, but after restarting ipython all worked fine.
How is it to be explained that I first had a namespace error which, after a
restart (and not merely a new run Sande_celsius-main.py), went away? I
mean, surely the namespace should not be impacted by ipython at all!?
 :
# file: Sande_celsius-main.py
from Sande_my_module import c_to_f
celsius = float(raw_input(Enter a temperature in Celsius: ))
fahrenheit = c_to_f(celsius)
print That's , fahrenheit,  degrees Fahrenheit

# this is the file Sande_my_module.py
# we're going to use it in another program
def c_to_f(celsius):
fahrenheit = celsius * 9.0 / 5 + 32
return fahrenheit

When I run Sande_celsius-main.py, I get the following error:

NameError: global name 'celsius' is not defined
WARNING: Failure executing file:Sande_celsius-main.py



Python interpreters including the standard one or IPython should tell
you a lot more than that. how are you executing this code? would it be
possible to do so from the command-line? you should get a more verbose
error message that you can post here.

best regards,
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Core Python Programming, Prentice Hall, (c)2007,2001
Python Fundamentals, Prentice Hall, (c)2009
 http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com



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


[Tutor] how to clear contents of a file

2010-02-09 Thread sudhir prasad
hi,
how to clear contents of a file with out actually deleting it,
basically wat im trying to do is copy a source file into  a common file ,run
the common file,after that  i need to copy another source file  into the
common file,i ant to clear the  contents of the common file before copying
contents into it.how does f.truncate() helps here


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


Re: [Tutor] how to clear contents of a file

2010-02-09 Thread Christian Witts

sudhir prasad wrote:

hi,
how to clear contents of a file with out actually deleting it,
basically wat im trying to do is copy a source file into  a common 
file ,run the common file,after that  i need to copy another source 
file  into the common file,i ant to clear the  contents of the common 
file before copying contents into it.how does f.truncate() helps here



thanks,
sudheer


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
  
You can just open the file again in write mode which will truncate it 
for you.
So, f = open(filename, 'w') will clear the contents of your filename 
(don't forget to close the file when you're done if you do this though).


--
Kind Regards,
Christian Witts


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