Re: [Tutor] Adding a GUI

2007-09-16 Thread Alan Gauld
"wormwood_3" <[EMAIL PROTECTED]> wrote 

> I am just starting to learn GUI programming, with wxPython. 

Good choice. If you will be doing much in wxPython get the book.
It makes the whole thing much easier.

> The script heretofore was just run at the command line. 
> Would it make sense to add an option in the same script 
> to have it run in "graphical mode", 

You can do but personally I prefer to have two separate 
versions, one with a CLI and the other a GUI. They both 
share a common module which is the core functionality
represented as functions/classes so that the UI (either of them)
can call them as needed.

To do that the first step is to identify the core functions and 
refactor them into a separate module (or modules). To do 
that you have to remove all UI operations - print/raw_input etc
from the functions and make them completely parameterised.
(ideally they should be stateless too) The strings and data 
you need to display will be returned by the functions.

Once you have that done its easy to add a CLI a GUI and 
even a web interface.

> Anyone have any tips on adding a GUI to a pre-existing 
> script in a way that minimizes repetition of logic? 

I go through the process in the case study in my tutorial. 
Just scan through to the section Adding a GUI. It uses 
Tkinter but the conepts are identical.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to match regular expression from right to left

2007-09-16 Thread 王超
hi,

I want to match the regular expression from right to left, such as:

TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
TAG_pattern.search(line)

Does the search method support this?

Thanks,
Daniel
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to match regular expression from right to left

2007-09-16 Thread Tom Tucker
Yep, looks like it.

>>> line = 'us::blah blah2 1002 blah3'
>>> import re
>>> TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
>>> if TAG_pattern.search(line):
... print 'we have a match'
...
we have a match
>>>

>>> line2 ='us::blah blah2 1001 blah3'
>>> if TAG_pattern.search(line2):
... print 'we have a match'
... else:
... print 'no match found'
...
no match found



On 9/16/07, 王超 <[EMAIL PROTECTED]> wrote:
> hi,
>
> I want to match the regular expression from right to left, such as:
>
> TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
> TAG_pattern.search(line)
>
> Does the search method support this?
>
> Thanks,
> Daniel
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to match regular expression from right to left

2007-09-16 Thread 王超
yes, but I mean if I have the line like this:

line = """38166 us::Video_Cat::Other; us::Video_Cat::Today Show;
us::VC_Supplier::bc; 1002::ms://bc.wd.net/a275/video/tdy_is.asf;
1003::ms://bc.wd.net/a275/video/tdy_is_.fl;"""

I want to get the part "us::MSNVideo_Cat::Other; us::MSNVideo_Cat::Today
Show; us::VC_Supplier::Msnbc;"

but re.compile(r"(us::.*) .*(1002|1003).*$") will get the
"1002::ms://bc.wd.net/a275/video/tdy_is.asf;" included in an lazy mode.

How can i filter it out in the re?

Thanks,
Daniel


On 9/16/07, Tom Tucker <[EMAIL PROTECTED]> wrote:
>
> Yep, looks like it.
>
> >>> line = 'us::blah blah2 1002 blah3'
> >>> import re
> >>> TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
> >>> if TAG_pattern.search(line):
> ... print 'we have a match'
> ...
> we have a match
> >>>
>
> >>> line2 ='us::blah blah2 1001 blah3'
> >>> if TAG_pattern.search(line2):
> ... print 'we have a match'
> ... else:
> ... print 'no match found'
> ...
> no match found
>
>
>
> On 9/16/07, 王超 <[EMAIL PROTECTED]> wrote:
> > hi,
> >
> > I want to match the regular expression from right to left, such as:
> >
> > TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
> > TAG_pattern.search(line)
> >
> > Does the search method support this?
> >
> > Thanks,
> > Daniel
> >
> > ___
> > Tutor maillist  -  Tutor@python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> >
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to match regular expression from right to left

2007-09-16 Thread Kent Johnson
王超 wrote:
> hi,
> 
> I want to match the regular expression from right to left, such as:
> 
> TAG_pattern = re.compile(r"(us::.*) .*(1002|1003).*$")
> TAG_pattern.search(line)
> 
> Does the search method support this?

What do you mean match from right to left? If you mean, match an re that 
is anchored at the end of the string, then sure, $ is supported. If you 
mean, find the right-most match of several (similar to the rfind method 
of a string) then no, it is not directly supported. You could use 
re.findall() and chose the last match but if the matches can overlap 
this may not give the correct result.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding a GUI

2007-09-16 Thread Michael Langford
Wormwood, an easier way to create a crossplatform GUI than raw wxPython is
to use pythoncard, which is a library that is on top of wxPython that is
there to make it easier to use and to make it easier to layout GUI
screens/dialogs.

I've found its a much faster "whiteboard to running software time" then
wxPython by itself. (I've used both, raw wxPython a lot *more* than
pythoncard, as i'd not yet heard of it).

Alan is right about separating the core logic of the program from the
input/output methods, and using that in both a command line and a gui
program.

I doubt you'll need a book to use pythoncard. Its about as easy as VB to
build a form with the WYSIWYG, and very pythonic to use the forms you've
built.

To get up an going, install a compatible version of wxPython use this link:
http://pythoncard.sourceforge.net/walkthrough1.html

 --Michael

References:
Pythoncard:http://pythoncard.sourceforge.net/

On 9/16/07, wormwood_3 <[EMAIL PROTECTED]> wrote:
>
> Hi all,
>
> I am just starting to learn GUI programming, with wxPython. I have a
> script that that I have developed to a useful point, and I want to add a GUI
> to it. I am a little unsure as the the best approach to this. The script
> heretofore was just run at the command line. Would it make sense to add an
> option in the same script to have it run in "graphical mode", which would
> trigger the wxPython code I would add in, and otherwise use the same logic?
>
> More generally: Anyone have any tips on adding a GUI to a pre-existing
> script in a way that minimizes repetition of logic? I would rather not make
> a separate script that only used wxPython, and I do not think I have to, but
> perhaps there are good reasons to, etc.
>
> Thanks,
> Sam
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Michael Langford
Phone: 404-386-0495
Consulting: http://www.TierOneDesign.com/
Entertaining: http://www.ThisIsYourCruiseDirectorSpeaking.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to match regular expression from right to left

2007-09-16 Thread Kent Johnson
王超 wrote:
> The number of iterms - (us::.*?) - varies.
> 
> When I use re.findall with (us::*?), only several 'us::' are extracted.

I don't understand what is going wrong now. Please show the code, the
data, and tell us what you get and what you want to get.

Here is an example:

Without a group you get the whole match:

In [3]: import re
In [4]: line = """38166 us::Video_Cat::Other; us::Video_Cat::Today Show;
us::VC_Supplier::bc; 1002::ms://bc.wd.net/a275/video/tdy_is.asf;
1003::ms://bc.wd.net/a275/video/tdy_is_.fl;"""
In [5]: re.findall('us::.*?;', line)
Out[5]: ['us::Video_Cat::Other;', 'us::Video_Cat::Today Show;',
'us::VC_Supplier::bc;']

With a group you get just the group:

In [6]: re.findall('(us::.*?);', line)
Out[6]: ['us::Video_Cat::Other', 'us::Video_Cat::Today Show',
'us::VC_Supplier::bc']

Kent

> 
> Daniel
> 
> On 9/16/07, * Kent Johnson* <[EMAIL PROTECTED] > 
> wrote:
> 
> 王超 wrote:
>  > yes, but I mean if I have the line like this:
>  >
>  > line = """38166 us::Video_Cat::Other; us::Video_Cat::Today Show;
>  > us::VC_Supplier::bc; 1002::ms://bc.wd.net/a275/video/tdy_is.asf;
>  > 1003::ms://bc.wd.net/a275/video/tdy_is_.fl;"""
>  >
>  > I want to get the part "us::MSNVideo_Cat::Other;
> us::MSNVideo_Cat::Today
>  > Show; us::VC_Supplier::Msnbc;"
>  >
>  > but re.compile(r"(us::.*) .*(1002|1003).*$") will get the
>  > "1002::ms://bc.wd.net/a275/video/tdy_is.asf;" included in an lazy
> mode.
> 
> Of course, you have asked for all the text up to the end of the string.
> 
> Not sure what you mean by lazy mode...
> 
> If there will always be three items you could just repeat the relevant
> sections of the re, something like
> 
> r'(us::.*?); (us::.*?); (us::.*?);'
> 
> or even
> 
> r'(us::Video_Cat::.*?); (us::Video_Cat::.*?); (us::VC_Supplier::.*?);'
> 
> If the number of items varies then use re.findall() with (us::.*?);
> 
> The non-greedy match is not strictly needed in the first case but it is
> in the second.
> 
> Kent
> 
> 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to match regular expression from right to left

2007-09-16 Thread 王超
The number of iterms - (us::.*?) - varies.

When I use re.findall with (us::*?), only several 'us::' are extracted.

Daniel

On 9/16/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> 王超 wrote:
> > yes, but I mean if I have the line like this:
> >
> > line = """38166 us::Video_Cat::Other; us::Video_Cat::Today Show;
> > us::VC_Supplier::bc; 1002::ms://bc.wd.net/a275/video/tdy_is.asf;
> > 1003::ms://bc.wd.net/a275/video/tdy_is_.fl;"""
> >
> > I want to get the part "us::MSNVideo_Cat::Other; us::MSNVideo_Cat::Today
> > Show; us::VC_Supplier::Msnbc;"
> >
> > but re.compile(r"(us::.*) .*(1002|1003).*$") will get the
> > "1002::ms://bc.wd.net/a275/video/tdy_is.asf;" included in an lazy mode.
>
> Of course, you have asked for all the text up to the end of the string.
>
> Not sure what you mean by lazy mode...
>
> If there will always be three items you could just repeat the relevant
> sections of the re, something like
>
> r'(us::.*?); (us::.*?); (us::.*?);'
>
> or even
>
> r'(us::Video_Cat::.*?); (us::Video_Cat::.*?); (us::VC_Supplier::.*?);'
>
> If the number of items varies then use re.findall() with (us::.*?);
>
> The non-greedy match is not strictly needed in the first case but it is
> in the second.
>
> Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to match regular expression from right to left

2007-09-16 Thread 王超
kent:
Thank you.  I got the right results with re.findall('(us::.*?);', line).

I used these codes before:
>>> TAG_pattern = re.compile(r"(us::.*?)")
>>> TAG_pattern.findall(line)
and got the unwanted results 'us::'

Tom:
Thank you. But the number of 'us::' terms varies, and kent's solution works
well.

Daniel

On 9/16/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> 王超 wrote:
> > The number of iterms - (us::.*?) - varies.
> >
> > When I use re.findall with (us::*?), only several 'us::' are extracted.
>
> I don't understand what is going wrong now. Please show the code, the
> data, and tell us what you get and what you want to get.
>
> Here is an example:
>
> Without a group you get the whole match:
>
> In [3]: import re
> In [4]: line = """38166 us::Video_Cat::Other; us::Video_Cat::Today Show;
> us::VC_Supplier::bc; 1002::ms://bc.wd.net/a275/video/tdy_is.asf;
> 1003::ms://bc.wd.net/a275/video/tdy_is_.fl;"""
> In [5]: re.findall('us::.*?;', line)
> Out[5]: ['us::Video_Cat::Other;', 'us::Video_Cat::Today Show;',
> 'us::VC_Supplier::bc;']
>
> With a group you get just the group:
>
> In [6]: re.findall('(us::.*?);', line)
> Out[6]: ['us::Video_Cat::Other', 'us::Video_Cat::Today Show',
> 'us::VC_Supplier::bc']
>
> Kent
>
> >
> > Daniel
> >
> > On 9/16/07, * Kent Johnson* <[EMAIL PROTECTED] >
> wrote:
> >
> > 王超 wrote:
> >  > yes, but I mean if I have the line like this:
> >  >
> >  > line = """38166 us::Video_Cat::Other; us::Video_Cat::Today Show;
> >  > us::VC_Supplier::bc; 1002::ms://bc.wd.net/a275/video/tdy_is.asf;
> >  > 1003::ms://bc.wd.net/a275/video/tdy_is_.fl;"""
> >  >
> >  > I want to get the part "us::MSNVideo_Cat::Other;
> > us::MSNVideo_Cat::Today
> >  > Show; us::VC_Supplier::Msnbc;"
> >  >
> >  > but re.compile(r"(us::.*) .*(1002|1003).*$") will get the
> >  > "1002::ms://bc.wd.net/a275/video/tdy_is.asf;" included in an lazy
> > mode.
> >
> > Of course, you have asked for all the text up to the end of the
> string.
> >
> > Not sure what you mean by lazy mode...
> >
> > If there will always be three items you could just repeat the
> relevant
> > sections of the re, something like
> >
> > r'(us::.*?); (us::.*?); (us::.*?);'
> >
> > or even
> >
> > r'(us::Video_Cat::.*?); (us::Video_Cat::.*?);
> (us::VC_Supplier::.*?);'
> >
> > If the number of items varies then use re.findall() with (us::.*?);
> >
> > The non-greedy match is not strictly needed in the first case but it
> is
> > in the second.
> >
> > Kent
> >
> >
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to match regular expression from right to left

2007-09-16 Thread Kent Johnson
王超 wrote:
> kent:
> Thank you.  I got the right results with re.findall('(us::.*?);', line).
> 
> I used these codes before:
>  >>> TAG_pattern = re.compile(r"(us::.*?)")
>  >>> TAG_pattern.findall(line)
> and got the unwanted results 'us::'

That is because .*? will match the empty string. You have to put the ;
after it to force the match to extend.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Adding a GUI

2007-09-16 Thread wormwood_3
>> First of all Sam, thanks for your help with the fileinput() problem I
>> was having! =)

Sure thing:-) Sorry I could not actually solve it! I am still have a hard time 
getting my mind around the line.strip(), then printing based on a condition 
loop. Not sure why...


The excerpt from Lutz' book was very helpful, thanks. One issue I still have: I 
do not really want to lose the ability to run the script from the CLI. This 
seems relatively common too, having the same script with a command line and a 
graphical version. Lutz seems to present the case of pure conversion, where a 
script was first CLI only, and will end up being GUI only, which I was hoping 
to avoid...

I didn't mean to speak badly of Tkinter, I know it is the most ubiquitous, and 
can work quite well. I have used it before, and it is relatively easy to use. 
The main issue I have with it is just that it, well, looks bad! The windows and 
frames do not resemble native windows per OS, which is an advantage of wxPython.

I will check out his book more, though, 300 pages on GUI programming would be 
helpful!

Thanks,
Sam






___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Adding a GUI

2007-09-16 Thread wormwood_3
Hi,

>> Wormwood, an easier way to create a crossplatform GUI than raw wxPython is 
>> to use pythoncard, which is a library that is on top of wxPython that is 
>> there to make it easier to use and to make it easier to 
>> layout GUI screens/dialogs.


I have heard some good things about pythoncard. I will be sure to give it a try!

>> I've found its a much faster "whiteboard to running software time" then 
>> wxPython by itself. (I've used both, raw wxPython a lot *more* than 
>> pythoncard, as i'd not yet heard of it).

Awesome.

>> Alan is right about separating the core logic of the program from the 
>> input/output methods, and using that in both a command line and a gui 
>> program. 


I hope I am not missing messages from the tutor list again. I did not see 
anything from Alan on my question, which you seemed to be referring to... But 
that aside, I definitely agree this is an important principle, which I always 
try to implement.

>> I doubt you'll need a book to use pythoncard. Its about as easy as VB to 
>> build a form with the WYSIWYG, and very pythonic to use the forms you've 
>> built.
>> To get up an going, install a compatible version of wxPython use this link:
http://pythoncard.sourceforge.net/walkthrough1.html

Thanks!



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] referencing vars()

2007-09-16 Thread Terry Carroll
On Sat, 15 Sep 2007, John wrote:

> I'm trying to do the above, but of course get an error because vardict is
> only referencing vars(), thus changes size... also, I tried
> vardict=[vars()], but this fails as well??

How about 

 import copy
 vardict=copy.copy(vars())

That way, you've made a copy of vars, and any changes that happen in the 
meantime don't matter.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] remove instances from a list

2007-09-16 Thread Ara Kooser
Hello all,

   On my continuing quest to grapple with OO programming Kent showed
me that I could call instances and store them in a list like:
yeasts = 
[Yeast("Red_1","Red","ade","lys"),Yeast("Yellow_1","Yellow","lys","ade"),
  
Yeast("Red_2","Red","ade","lys"),Yeast("Yellow_2","Yellow","lys","ade")]

Give certain conditions I want the yeast cell to die. Kent suggested I
use something this:
 yeasts = [yeast for yeast in yeasts if yeast.isAlive()] to clear out
dead yeast.

Is the translation for the above line of code into pseudocode?
yeast for every yeast in the list yeasts if the yeast method returned isAlive()

Or is this meant to be a true or false return? I have included the
code below. Thank you again.

Ara


###
CODE BELOW
###

#
# Yeast Model
# By Ara Kooser
# Version beta
#
#Thanks to Kent, Michael, and Alan for suggestions and help
##
import random
chemicals = []

class Yeast:

def __init__(self,name,group,need,release):
#name can be any, group is either red or yellow, need/release lys or ade
self.name = name
self.group = group
self.need = need
self.release = release
self.growth = 0
self.life = 0
self.starve = 0
self.alive = True

#METHODS

def setup(self):
#Sets up the random variable for each yeast before the game logic loop
if self.group == "Red":
a = random.randrange(40,160)
self.life = a
self.growth = 5
aa = random.randrange(20,50)
self.starve = aa
elif self.group == "Yellow":
b = random.randrange(20,80)
self.life = b
self.growth = 3
bb = random.randrange(10,30)
elif self.group == "Defect":
pass
else:
pass

def chem_need(self):
if self.need == "ade":
if self.need in chemicals:
print self.name,"is taking ade"
self.life = self.life+1
chemicals.remove(self.need)
print chemicals
print "Life", self.life
print
else:
print self.name, "found no ade present"
self.life = self.life-1

elif self.need == "lys":
if self.need in chemicals:
print self.name," is taking lys"
self.life = self.life+1
chemicals.remove(self.need)
print chemicals
print "Life",self.life
print
else:
print self.name, "found no lys present"
self.life = self.life-1

else:
pass

def near_death(self):
#release of aa in the environment near cell death
if self.life <= self.starve:
c = random.randrange(1,3)
if c == 1:
print self.name, "-- Releasing", self.release
chemicals.append(self.release)

if c == 2:
print self.name, "-- Releasing", self.release
chemicals.append(self.release)
chemicals.append(self.release)

if c == 3:
print self.name, "-- Releasing", self.release
chemicals.append(self.release)
chemicals.append(self.release)
chemicals.append(self.release)

def isAlive(self):
if self.life > 0:
self.alive = True
else:
self.alive = False

def growth(self):
pass

###
#Program Starts Here
###

#Stores the instances in a list for easier use. Later versions will
allow the user to add
#instances

#Instance are called: Yeast, name, group, aa need, aa release

yeasts = 
[Yeast("Red_1","Red","ade","lys"),Yeast("Yellow_1","Yellow","lys","ade"),
  
Yeast("Red_2","Red","ade","lys"),Yeast("Yellow_2","Yellow","lys","ade")]


print "Welcome to the red/yellow yeast simulation environment."
print"Red yeast cells need adenine to grow and they release lysine."
print"Yellow yeast cells ned lysine to grow and release adenine."
print
raw_input("Please press return or enter to start the game.")

rounds = raw_input("How many rounds to you wish to play through?")
print "Starting environment", chemicals
rounds =int(rounds)
count = 0

###Game logic

for yeast in yeasts:
yeast.setup()


while count < rounds:



print "##"
print "Round number",count
print

for yeast in yeasts:
yeast.chem_need()


print
print "Chemicals in the environment",chemicals
print

#Calls the growth code that in not yet fu

Re: [Tutor] remove instances from a list

2007-09-16 Thread John Fouhy
On 17/09/2007, Ara Kooser <[EMAIL PROTECTED]> wrote:
> Give certain conditions I want the yeast cell to die. Kent suggested I
> use something this:
>  yeasts = [yeast for yeast in yeasts if yeast.isAlive()] to clear out
> dead yeast.
[...]
> class Yeast:
[...]
> def isAlive(self):
> if self.life > 0:
> self.alive = True
> else:
> self.alive = False

Most programmers would see an 'isAlive' method as a method that would
return True if the thing is alive, and False otherwise.

So you would define it as:

def isAlive(self):
if self.life > 0:
return True
else:
return False

Then you could write code like this:

# suppose 'y' is a yeast object

# If y is alive, make it grow.
if y.isAlive():
y.grow()

(actually, you can write this method much more concisely:

def isAlive(self):
return self.life > 0
)

HTH!

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] remove instances from a list

2007-09-16 Thread Kent Johnson
Ara Kooser wrote:
> Hello all,
> 
>On my continuing quest to grapple with OO programming Kent showed
> me that I could call instances and store them in a list like:
> yeasts = 
> [Yeast("Red_1","Red","ade","lys"),Yeast("Yellow_1","Yellow","lys","ade"),
>   
> Yeast("Red_2","Red","ade","lys"),Yeast("Yellow_2","Yellow","lys","ade")]
> 
> Give certain conditions I want the yeast cell to die. Kent suggested I
> use something this:
>  yeasts = [yeast for yeast in yeasts if yeast.isAlive()] to clear out
> dead yeast.
> 
> Is the translation for the above line of code into pseudocode?
> yeast for every yeast in the list yeasts if the yeast method returned 
> isAlive()

Almost.

yeast for every yeast in the list yeasts if the yeast method isAlive() 
returned True

This is called a list comprehension, it is a very handy way to do things 
with lists. Some examples here:
http://docs.python.org/tut/node7.html#SECTION00714

> def chem_need(self):
> if self.need == "ade":
> if self.need in chemicals:
> print self.name,"is taking ade"
> self.life = self.life+1
> chemicals.remove(self.need)
> print chemicals
> print "Life", self.life
> print
> else:
> print self.name, "found no ade present"
> self.life = self.life-1
> 
> elif self.need == "lys":
> if self.need in chemicals:
> print self.name," is taking lys"
> self.life = self.life+1
> chemicals.remove(self.need)
> print chemicals
> print "Life",self.life
> print
> else:
> print self.name, "found no lys present"
> self.life = self.life-1

Note that the above two blocks are almost identical. The only difference 
is the print statements. Can you figure out a way to combine them into a 
single block? Then you could say
if self.need in ('ade', 'lys'):
   # handle both ade and lys

> def isAlive(self):
> if self.life > 0:
> self.alive = True
> else:
> self.alive = False

This could be just
   self.alive = (self.life > 0)

but I think what you want here is actually to *return* a flag indicating 
whether the yeast is alive:
   def isAlive(self):
 return (self.life > 0)

> 
> #The line below will eventually clean out dead yeast
> #yeast for every yeast in the list yeasts if the yeast method returned isAlive
> yeasts = [yeast for yeast in yeasts if yeast.isAlive()]

This will work correctly if you change isAlive() as above.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] remove instances from a list

2007-09-16 Thread Ian Witham
On 9/17/07, Ara Kooser <[EMAIL PROTECTED]> wrote:
>
> Hello all,
>
>On my continuing quest to grapple with OO programming Kent showed
> me that I could call instances and store them in a list like:
> yeasts =
> [Yeast("Red_1","Red","ade","lys"),Yeast("Yellow_1","Yellow","lys","ade"),
>
>   
> Yeast("Red_2","Red","ade","lys"),Yeast("Yellow_2","Yellow","lys","ade")]
>
> Give certain conditions I want the yeast cell to die. Kent suggested I
> use something this:
> yeasts = [yeast for yeast in yeasts if yeast.isAlive()] to clear out
> dead yeast.
>
> Is the translation for the above line of code into pseudocode?
> yeast for every yeast in the list yeasts if the yeast method returned
> isAlive()


...yeast for yeast in yeasts if the yeast.isAlive() method returns True OR a
value that evaluates as True.

for instance 0 is false, any other integer is True,
0.0 is False, any other float is True
"" is False, and any non empty string is True.
Each data type has a True or False condition, which I believe is why Python
didn't even have a boolean True/False data type until very recently.

Or is this meant to be a true or false return? I have included the
> code below. Thank you again.


It does not need to return True or False, as whatever it returns can be
evaluated as True or False, as I mentioned above.
You should also realise that you can test for a specific return value by
doing something like this:

yeasts = [yeast for yeast in yeasts if yeast.isAlive() == "I'm not dead
yet!"]

or

yeasts = [yeast for yeast in yeasts if yeast.health > -10]
(this example directly tests the instance's local variable life_force rather
than calling a method and testing the returned value)

Hope this helps,

Ian.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] performance

2007-09-16 Thread Jeff Peery
Hello, 
  I've got a quick question regarding performance of lists. I am taking 
measurements and building up a list of objects for each measurement. the class 
I created for the objects has attributes of time, numerical value, person's 
name who collected the sample etc. I also have functions within my class (I 
think they are properly named 'accessors'?) that get a piece of data within the 
object, for example 'self.GetSampleTime()'. I'm wondering what happens to my 
performance as I add more accesors to my class. How are the accesors managed? 
will each object in my list of objects contain the data for each accesor or do 
all the objects look to the sample module for the accesor? will my list of 
objects become huge and slow as I add more accessors? thanks.
   
  Jeff

   
-
Don't let your dream ride pass you by.Make it a reality with Yahoo! Autos. ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] performance

2007-09-16 Thread Carlos Daniel Ruvalcaba Valenzuela
Don't worry too much for the accessors, I'm pretty sure it won't
degrade your performance in a noticeable way, you objects will only
grow a tiny bit by adding a function to the class, all objects share
the same in memory code and each one has it's own data, the function
for the object is just a reference for the class function, not the
memory of the function itself (I think, it would be a waste of memory
otherwise).

However take it with a grain of salt, do your own benchmarks, you
could do a simple measure with time.time() function, or use one of the
several profiling modules for python (profile, hotshot, etc).

Forwarded to Tutor list, I forgot it sorry!

Regards,
Carlos Daniel Ruvalcaba Valenzuela

On 9/16/07, Jeff Peery <[EMAIL PROTECTED]> wrote:
> Hello,
> I've got a quick question regarding performance of lists. I am taking
> measurements and building up a list of objects for each measurement. the
> class I created for the objects has attributes of time, numerical value,
> person's name who collected the sample etc. I also have functions within my
> class (I think they are properly named 'accessors'?) that get a piece of
> data within the object, for example 'self.GetSampleTime()'. I'm wondering
> what happens to my performance as I add more accesors to my class. How are
> the accesors managed? will each object in my list of objects contain the
> data for each accesor or do all the objects look to the sample module for
> the accesor? will my list of objects become huge and slow as I add more
> accessors? thanks.
>
> Jeff
>
>  
> Don't let your dream ride pass you by. Make it a reality with Yahoo! Autos.
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor