Re: [Tutor] Run script automatically in IDLE shell

2005-12-08 Thread Kent Johnson
Yann Le Du wrote:

> P.S. I do all this to have accentuated characters (French) correctly 
> displayed, and this is done in the IDLE Shell, but fails inside the DOS 
> window. So this is why I want to have my scripts run directly inside the 
> IDLE shell...

The DOS shell uses code page 437 while IDLE uses Cp1252. Most likely you 
have data in Cp1252 (or latin-1, which is almost the same). When you 
write it to the shell in IDLE it is fine because IDLE expects it. When 
you write it to the DOS shell it is misinterpreted as Cp437 and the 
wrong characters print.

Try setting the DOS shell to Cp1252 with the command
  > chcp 1252
before you run your Python program.

For more details see
http://www.pycs.net/users/323/stories/14.html

Kent

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


Re: [Tutor] my text adventure

2005-12-08 Thread Kent Johnson
david wrote:
> thanks. i had actually coded this almost exactly the same. i'll try
> to make my questions more specific. i am able to pickle and restore
> world. which is a dictionary of coordinates : room objects. when i
> look at the savefile that pickle generates i can see all my
> descriptions and exits. however when i reload my world gets back all 
> the rooms that were created with dig. but the rooms don't have their 
> exits or descriptions. is pickle the right tool for this? can i
> pickle.dump more than one thing to the same savefile?

Yes, as long as you pickle and unpickle in the same order you can do this.

> or how could i
> go about creating a structure that holds room, coords, descriptions 
> and exits? 

You could put everything you want to pickle into a list or dictionary 
and pickle that, then extract them out again when you unpickle.

Kent

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


Re: [Tutor] My First Program

2005-12-08 Thread Greg Hasseler
You should create a counting variable. Whenever the user answers incorrectly, 
increment that variable by one. Starting with the fourth question and any 
question after, you should check the value of the variable to see if it is 
three or greater. If it is, then exit, if not, then continue. Check only 
beginning with the fourth question, because at the start of the first three 
questions, it would not be possible to already have three incorrect answers.


On Wednesday 07 December 2005 23:09, Trent Rigsbee wrote:
> Hi! My first "ever" program that I've created is a simple game called
> "State Capitals". It's straight forward; 5 simple questions about state
> capitals in a non-GUI format. Can someone look over my code and offer tips,
> suggestions, criticism? Also, I'd like to be able to end the program if the
> user misses 3 questions (i.e., kick them out after the 3rd wrong answer).
> How can I do this? Thanks!
>
>
> print "\nState Capitals! Answer a or b to the following questions.\n"
> question = raw_input("Question 1 - What is the capital of NC, a: Raleigh or
> b: Atlanta? ")
> if question == "a":
>   print "Yes\n"
> else:
>   print "WRONG\n"
>
> question = raw_input("Question 2 - What is the capital of SC, a: Greenville
> or b: Columbia? ")
> if question == "b":
>   print "Yes\n"
> else:
>   print "WRONG\n"
>
> question = raw_input("Question 3 - What is the capital of NY, a: Albany or
> b: Buffalo?")
> if question == "a":
>   print "Yes\n"
> else:
>   print "WRONG\n"
>
> question = raw_input("Question 4 - What is the capital of OH, a: Cleveland
> or b: Columbus? ")
> if question == "b":
>   print "Yes\n"
> else:
>   print "WRONG\n"
>
> question = raw_input("Question 5 - What is the capital of TX, a: Houston or
> b: Austin? ")
> if question == "b":
>   print "Yes\n"
> else:
>   print "WRONG\n"
>
>
> ___
> 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] Escaping double quotes

2005-12-08 Thread Tim Johnson
* w chun <[EMAIL PROTECTED]> [051207 22:16]:
> > > >>> label = 'this is "quoted"'
> > > >>> label.replace('"','\"')
> > > 'this is "quoted"'
> > > ## This works
> > > >>> label.replace('"','\'')
> > > "this is 'quoted'"
> >
> >   
> >   What I should have been using is label.replace('"','\\"')
> >   :-) Nevermind.
> 
> 
> hold on a second pardner! :-)  what were you trying to do, provide
> escaped quotes?  because if it was something else, like replacing the
> double quotes with singles, you had it correct.
 
  Yes. Escaped quotes. To be inserted in web content as escaped quotes
  in code for a Javascript Alert call

> otherwise your solution can also be done with raw strings:
> 
> >>> label.replace('"','\\"')
> 'this is \\"quoted\\"'
> >>> label.replace('"',r'\"')
> 'this is \\"quoted\\"'
 
  Good tip tho'  I keep forgetting about raw strings.

> ok, i'll stop thinking about it now.  ;-)
 
  Thanks!
  tj

> cheers,
> -wesley

-- 
Tim Johnson <[EMAIL PROTECTED]>
  http://www.alaska-internet-solutions.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to extract text by specifying an element using ElementTree

2005-12-08 Thread ps python
Hi, 

using ElementTree, how can I extract text of a
particular element, or a child node. 

For example:


   
   Signal transduction
   
   




__ 
Yahoo! India Matrimony: Find your partner now. Go to http://yahoo.shaadi.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to extract text by specifying an element using ElementTree

2005-12-08 Thread ps python
Hi, 

using ElementTree, how can I extract text of a
particular element, or a child node. 

For example:


   
   Signal transduction
   
   
   Energy process



I looked at some tutorials (eg. Ogbuji).  Those
examples described to extract all text of nodes and
child nodes.

In the case where I already know which element tags
have the information that I need, in such case how do
i get that specific text. 

Thanks
Mdan




__ 
Yahoo! India Matrimony: Find your partner now. Go to http://yahoo.shaadi.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract text by specifying an element using ElementTree

2005-12-08 Thread Danny Yoo


> For example:
>
> 
>
>Signal transduction
>
>
>Energy process
> 
> 
>
> I looked at some tutorials (eg. Ogbuji).  Those
> examples described to extract all text of nodes and
> child nodes.

Hi Mdan,

The following might help:

http://article.gmane.org/gmane.comp.python.tutor/24986
http://mail.python.org/pipermail/tutor/2005-December/043817.html

The second post shows how we can use the findtext() method from an
ElementTree.

Here's another example that demonstrates how we can treat elements as
sequences of their subelements:

##
from elementtree import ElementTree
from StringIO import StringIO

text = """


skywalker
luke


valentine
faye


reynolds
mal


"""

people = ElementTree.fromstring(text)
for person in people:
print "here's a person:",
print person.findtext("firstName"), person.findtext('lastName')
##


Does this make sense?  The API allows us to treat an element as a sequence
that we can march across, and the loop above marches across every person
subelement in people.


Another way we could have written the loop above would be:

###
>>> for person in people.findall('person'):
... print person.find('firstName').text,
... print person.find('lastName').text
...
luke skywalker
faye valentine
mal reynolds
###


Or we might go a little funkier, and just get the first names anywhere in
people:

###
>>> for firstName in people.findall('.//firstName'):
... print firstName.text
...
luke
faye
mal
###

where the subelement "tag" that we're giving findall is really an
XPath-query.  ".//firstName" is an query in XPath format that says "Give
me all the firstName elements anywhere within the current element."


The documentation in:

http://effbot.org/zone/element.htm#searching-for-subelements

should also be helpful.


If you have more questions, please feel free to ask.

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


Re: [Tutor] how to extract text by specifying an element using ElementTree

2005-12-08 Thread Kent Johnson
ps python wrote:
> Hi, 
> 
> using ElementTree, how can I extract text of a
> particular element, or a child node. 
> 
> For example:
> 
> 
>
>Signal transduction
>
>
>Energy process
> 
> 
> 
> In the case where I already know which element tags
> have the information that I need, in such case how do
> i get that specific text. 

Use find() to get the nodes of interest. The text attribute of the node 
contains the text. For example:

data = '''

Signal transduction


Energy process
 

'''

from elementtree import ElementTree

tree = ElementTree.fromstring(data)

for process in tree.findall('biological_process'):
   print process.text.strip()


prints
Signal transduction
Energy process

You will have to modify the path in the findall to match your actual 
data, assuming what you have shown is just a snippet.

I stripped whitespace from the text because otherwise it includes the 
newlines and indents exactly as in the original.

Kent

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


Re: [Tutor] My First Program

2005-12-08 Thread Joseph Quigley
Hi! My first "ever" program that I've created is a simple game called "State
Capitals". It's straight forward; 5 simple questions about state capitals ina non-GUI format. Can someone look over my code and offer tips, suggestions,criticism? Also, I'd like to be able to end the program if the user misses 3
questions (i.e., kick them out after the 3rd wrong answer). How can I dothis? Thanks!print "\nState Capitals! Answer a or b to the following questions.\n"question = raw_input("Question 1 - What is the capital of NC, a: Raleigh or
b: Atlanta? ")if question == "a":print "Yes\n"else:print "WRONG\n"question = raw_input("Question 2 - What is the capital of SC, a: Greenville
or b: Columbia? ")if question == "b":print "Yes\n"else:print "WRONG\n"question = raw_input("Question 3 - What is the capital of NY, a: Albany or
b: Buffalo?")if question == "a":print "Yes\n"else:print "WRONG\n"question = raw_input("Question 4 - What is the capital of OH, a: Cleveland
or b: Columbus? ")if question == "b":print "Yes\n"else:print "WRONG\n"question = raw_input("Question 5 - What is the capital of TX, a: Houston or
b: Austin? ")if question == "b":print "Yes\n"else:print "WRONG\n"
Hi! Glad you picked up Python. You'll probably enjoy it.

This might be over your head, but I'd use functions for your Yes and WRONG:

def yes():
    print "Yes\n"
def WRONG():
    print "WRONG\n"

# Now for the sample code:
question = raw_input("Question 4 - What is the capital of OH, a: Cleveland
or b: Columbus? ")
if question == "b":
    yes()
else:
    WRONG()

Hope this helps.
Joe

-- There are 10 different types of people in the world.Those who understand binary and those who don't.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract text by specifying an element using ElementTree

2005-12-08 Thread ps python
 Kent and Dany, 
Thanks for your replies.  

Here fromstring() assuming that the input is in a kind
of text format. 

what should be the case when I am reading files
directly. 

I am using the following :

from elementtree.ElementTree import ElementTree
mydata = ElementTree(file='1.xml')
iter = root.getiterator()

Here the whole XML document is loaded as element tree
and how should this iter into a format where I can
apply findall() method. 

thanks
mdan



--- Kent Johnson <[EMAIL PROTECTED]> wrote:

> ps python wrote:
> > Hi, 
> > 
> > using ElementTree, how can I extract text of a
> > particular element, or a child node. 
> > 
> > For example:
> > 
> > 
> >
> >Signal transduction
> >
> >
> >Energy process
> > 
> > 
> > 
> > In the case where I already know which element
> tags
> > have the information that I need, in such case how
> do
> > i get that specific text. 
> 
> Use find() to get the nodes of interest. The text
> attribute of the node 
> contains the text. For example:
> 
> data = '''
> 
> Signal transduction
> 
> 
> Energy process
>  
> 
> '''
> 
> from elementtree import ElementTree
> 
> tree = ElementTree.fromstring(data)
> 
> for process in tree.findall('biological_process'):
>print process.text.strip()
> 
> 
> prints
> Signal transduction
> Energy process
> 
> You will have to modify the path in the findall to
> match your actual 
> data, assuming what you have shown is just a
> snippet.
> 
> I stripped whitespace from the text because
> otherwise it includes the 
> newlines and indents exactly as in the original.
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




__ 
Yahoo! India Matrimony: Find your partner now. Go to http://yahoo.shaadi.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to extract text by specifying an element using ElementTree

2005-12-08 Thread Kent Johnson
ps python wrote:
>  Kent and Dany, 
> Thanks for your replies.  
> 
> Here fromstring() assuming that the input is in a kind
> of text format. 

Right, that is for the sake of a simple example.
> 
> what should be the case when I am reading files
> directly. 
> 
> I am using the following :
> 
> from elementtree.ElementTree import ElementTree
> mydata = ElementTree(file='1.xml')
> iter = root.getiterator()
> 
> Here the whole XML document is loaded as element tree
> and how should this iter into a format where I can
> apply findall() method. 

Call findall() directly on mydata, e.g.
for process in mydata.findall('//biological_process'):
   print process.text

The path //biological_process means find any biological_process element 
at any depth from the root element.

Kent

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


Re: [Tutor] My First Program

2005-12-08 Thread Danny Yoo

> > Hi! My first "ever" program that I've created is a simple game called
> > "State Capitals". It's straight forward; 5 simple questions about
> > state capitals in a non-GUI format.

[some code cut]

Hi Trent,


Looks good so far!  There is one direct thing we can do to make the
program a little shorter: we can use "functions".  If you want, we can go
through an example to see what these things do.


There's a fairly consistant pattern to each of the quiz questions.  If we
look at the first two blocks of questions, for example:


> > question = raw_input("Question 1 - What is the capital of NC, a: Raleigh
> > or
> > b: Atlanta? ")
> > if question == "a":
> > print "Yes\n"
> > else:
> > print "WRONG\n"


> > question = raw_input("Question 2 - What is the capital of SC, a:
> > Greenville
> > or b: Columbia? ")
> > if question == "b":
> > print "Yes\n"
> > else:
> > print "WRONG\n"


and if we squint our eyes a bit, we might see a pattern here, something
like:

question = raw_input(  {some question here}  )
if question ==  {some right answer here}:
print "Yes\n"
else:
print "Wrong\n"

where I've put placeholders (the stuff in braces) to mark the places
that are different.


There is a feature in many programming languages called the "function"
that allows us to capture this pattern and give it a name.  Think Mad
Libs: what we can do is make a mad-lib game form, and then let people fill
in what they want.

The block above can be turned into this function:

###
def quiz_question(some_question, some_right_answer):
question = raw_input(some_question)
if question == some_right_answer:
print "Yes\n"
else:
print "Wrong\n"
###


'quiz_question' is the name I've given this, though if you want to call
it something else, please feel free to change the name.


Once we have 'quiz_question', how do we use this?  Let try this from the
interactive interpreter:

##
>>> quiz_question("what's your favorite color?", "green")
what's your favorite color?green
Yes

>>> quiz_question("what's your favorite color?", "green")
what's your favorite color?blue no red!
Wrong
##


Does this make sense so far?

By having that function, it'll lets you add more questions to your quiz by
just adding the stuff that's really important: the content of the
questions and their right answers.


The tutorials on:

http://wiki.python.org/moin/BeginnersGuide/NonProgrammers

should talk about functions a bit.  Try playing with functions: it should
make your program shorter, and that'll make it a little easier to think
about how to do the three-strikes-you're-out! thing.

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


[Tutor] bitwise manipulation

2005-12-08 Thread Tim Johnson
Hello Snake Charmers:

I'm designing a database and am thinking about how to store a number
number of boolean values in one field.

Are there any python resources available that can make setting/unsetting 
bits directly? I used to do that in "C" with preprocessor macros that
made calls like set_bit(vInteger,bit_position) 
and unset_bit(vInteger,bit_position). 

Thanks
tim

-- 
Tim Johnson <[EMAIL PROTECTED]>
  http://www.alaska-internet-solutions.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] bitwise manipulation

2005-12-08 Thread John Fouhy
On 09/12/05, Tim Johnson <[EMAIL PROTECTED]> wrote:
> Are there any python resources available that can make setting/unsetting
> bits directly? I used to do that in "C" with preprocessor macros that
> made calls like set_bit(vInteger,bit_position)
> and unset_bit(vInteger,bit_position).

Well, you could write them yourself?

[untested]

def set_bit(i, n):
return i | (1 << n)

def unset_bit(i, n):
return i & ~(1 << n)

def test_bit(i, n):
return i & (1 << n)

Note that integers are immutable, so there's no way to change an existing int.

Also, if you're playing with stuff at that level, you might find the
struct module helpful.

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


Re: [Tutor] bitwise manipulation

2005-12-08 Thread Tim Johnson
* John Fouhy <[EMAIL PROTECTED]> [051208 16:55]:
> On 09/12/05, Tim Johnson <[EMAIL PROTECTED]> wrote:
> > Are there any python resources available that can make setting/unsetting
> > bits directly? I used to do that in "C" with preprocessor macros that
> > made calls like set_bit(vInteger,bit_position)
> > and unset_bit(vInteger,bit_position).
> 
> Well, you could write them yourself?
 
  OK

> [untested]
> 
> def set_bit(i, n):
> return i | (1 << n)
> 
> def unset_bit(i, n):
> return i & ~(1 << n)
> 
> def test_bit(i, n):
> return i & (1 << n)
 
  You just did most of the work for me!

> Note that integers are immutable, so there's no way to change an existing int.
 
  That's what objects are for...

> Also, if you're playing with stuff at that level, you might find the
> struct module helpful.
 
  I believe it would.
  Thanks
  I will call the class Bitters 

  cheers
  Tim (off to brew some Bitters)
  
> --
> John.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Tim Johnson <[EMAIL PROTECTED]>
  http://www.alaska-internet-solutions.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Timer

2005-12-08 Thread bob
At 07:18 PM 12/6/2005, Liam Clarke-Hutchinson wrote:
>Hi,
>
>time.sleep() takes an argument as seconds.

Oh yeah I know that but forgot.Sigh. Thanks for the correction. 

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