Re: [Tutor] float question

2005-12-20 Thread Danny Yoo


On Mon, 19 Dec 2005, linda.s wrote:

> what does 2 mean in %2.4f ?

Hello,

It's a "minimal field width" modifier, according to:

http://docs.python.org/lib/typesseq-strings.html


Let's try experimenting with it.

##
>>> '%20f' % 3.14
'3.14'
>>> '%20.2f' % 3.14
'3.14'
##


Does this make sense?  If you have more questions, please feel free to
ask.

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


Re: [Tutor] float question

2005-12-20 Thread linda.s
On 12/20/05, Danny Yoo <[EMAIL PROTECTED]> wrote:
>
>
> On Mon, 19 Dec 2005, linda.s wrote:
>
> > what does 2 mean in %2.4f ?
>
> Hello,
>
> It's a "minimal field width" modifier, according to:
>
>http://docs.python.org/lib/typesseq-strings.html
>
>
> Let's try experimenting with it.
>
> ##
> >>> '%20f' % 3.14
> '3.14'
> >>> '%20.2f' % 3.14
> '3.14'
> ##
>
>
> Does this make sense?  If you have more questions, please feel free to
> ask.

Danny,
Thanks. Still confused. I changed the code a little.
>>> '%3.2f' % 3.14
'3.14'
>>> '%4.2f' % 3.14
'3.14'
there is no difference in the above two codes. so what does 3 and 4 mean?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] float question

2005-12-20 Thread Pujo Aji
Th format %x1.x2fwhere x1 is the total space and x2  is decimal digit place.If your data is longer than the specified x1 and x2.the data rules.Look at 3.14 takes 4 places total# 4 places rules
>>> '%3.2f' % 3.14'3.14'# perfect>>> '%4.2f' % 3.14'3.14'# try this >>> '%5.2f' % 3.14' 3.14'Cheers,pujo
On 12/20/05, linda.s <[EMAIL PROTECTED]> wrote:
On 12/20/05, Danny Yoo <[EMAIL PROTECTED]> wrote:>>> On Mon, 19 Dec 2005, linda.s wrote:>> > what does 2 mean in %2.4f ?
>> Hello,>> It's a "minimal field width" modifier, according to:>>http://docs.python.org/lib/typesseq-strings.html
>>> Let's try experimenting with it.>> ##> >>> '%20f' % 3.14> '3.14'> >>> '%20.2f' % 3.14> '3.14'
> ##>>> Does this make sense?  If you have more questions, please feel free to> ask.Danny,Thanks. Still confused. I changed the code a little.>>> '%3.2f' % 3.14
'3.14'>>> '%4.2f' % 3.14'3.14'there is no difference in the above two codes. so what does 3 and 4 mean?___Tutor maillist  -  
Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] float question

2005-12-20 Thread Alan Gauld
> what does 2 mean in %2.4f ?

In this case nothing because it will always be over-ridden by the 4.

Basically the first number specifies the *minimum* number of
characters output, ie 2 in this case.

The second number specifies the number of decimal placess
output, 4 in this case.

Since 4 decimal places plus the decimal point is 5 characters minimum,
then any value of the first digit less than 5 has no effect in this case.
To be useful at all the first digit must be greater than the second digit 
plus
one (for the decimal point). See the examples below:

>>> '%2.4f' % 456.7
'456.7000'
>>> '%2.4f' % 456.789
'456.7890'
>>> '%5.4f' % 456.789
'456.7890'
>>> '%8.4f' % 456.789
'456.7890'
>>> '%9.4f' % 456.789
' 456.7890'
>>>

Notice we always have 4 decimal digits even when we only supply 1.
And there is no difference in the overall length until the last one with
length 9, which is one greater than the length of the number expanded
to four decimals.

Does that help?

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld





___
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-20 Thread ps python
Dear Drs. Johnson and Yoo , 
 for the last 1 week I have been working on parsing
the elements from a bunch of XML files following your
suggestions. 

until now I have been unsuccessul.  I have no clue why
i am failing. 

I have ~16K XML files. this data obtained from johns
hopkins university (of course these are public data
and is allowed to use for teaching and non-commercial
purposes). 


from elementtree.ElementTree import ElementTree
>>> mydata = ElementTree(file='4.xml')
>>> for process in
mydata.findall('//biological_process'):
print process.text


>>> for proc in mydata.findall('functions'):
print proc


>>> 



I do not understand why I am unable to parse this
file. I questioned if this file is not well structures
(well formedness). I feel it is properly structured
and yet it us unparsable.  


Would you please help me /guide me what the problem
is.  Apologies if i am completely ignoring somethings.
 

PS: Attached is the XML file that I am using. 

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

> 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
> 

Send instant messages to your online friends http://in.messenger.yahoo.com 


  
   Aldehyde dehydrogenase 3

Aldehyde dehydrogenase family 3 subfamily A, member 1


ALDH3


Acetaldehyde dehydrogenase 3


ALDH, Stomach type


ALDHIII

100660 
ALDH3A1

  17p11.2
  
  7774944
  


  NM_000691.3
  NP_000682.3

50398


  ccaggagccc cagttaccgg gagaggctgt
gtcaaaggcg ccatgagcaa gatcagcgag
gccgtgaagc gcgcccgcgc cgccttcagc
tcgggcagga cccgtccgct gcagttccgg
atccagcagc tggaggcgct gcagcgcctg
atccaggagc aggagcagga gctggtgggc
gcgctggccg cagacctgca caagaatgaa
tggaacgcct actatgagga ggtggtgtac
gtcctagagg agatcgagta catgatccag
aagctccctg agtgggccgc ggatgagccc
gtggagaaga cgagac tcagcaggac
gagctctaca tccactcgga gccactgggc
gtggtcctcg tcattggcac ctggaactac
cccttcaacc tcaccatcca gcccatggtg
ggcgccatcg ctgcagggaa ctcagtggtc
ctcaagccct cggagctgag tgagaacatg
gcgagcctgc tggctaccat catcag
tacctggaca aggatctgta cccagtaatc
aatgtg tccctgagac cacggagctg
ctcaaggaga ggttcgacca tatcctgtac
acgggcagca cgtggg gaagatcatc
atgacggctg ctgccaagca cctgat
gtcacgctgg agctgggagg gaagagtccc
tgctacgtgg acaagaactg tgacctggac
gtggcctgcc gacgcatcgc ctgaaa
ttcatgaaca gtggccagac ctgcgtggcc
cctgactaca tcctctgtga tcgatc
cagaaccaaa ttgtggagaa gctcaagaag
tcactgaaag agttctacgg ggaagatgct
aagaaatccc gggactatgg aagaatcatt
agtgcccggc acttccagag ggtgatgggc
ctgattgagg gccagaaggt ggcttatggg
ggcacc atgccgccac tcgctacata
gcacca tcctcacgga cgtgga
cagtgg tgatgcaaga ggagatcttc
gggcctgtgc tgcccatcgt gtgcgtgcgc
agcctggagg aggccatcca gttcatcaac
cagcgtgaga agtggc cctctacatg
ttctccagca acgacaaggt gattaagaag
atgattgcag agacatccag tggttg
gcggccaacg atgtcatcgt ccacatcacc
ttgcactctc tgcccttcgg gggcgt
aacagcggca tgggatccta ccatggcaag
aagagcttcg agactttctc tcaccgccgc
tcttgcctgg tgaggcctct gatgaatgat
gaaggcctga aggtcagata ccgagc
ccggccaaga tgacccagca ctgaggaggg
gttgctccgc ctggcctggc catactgtgt
cccatcggag tgcggaccac cctcactggc
tctcctggcc ctgggagaat cgctcctgca
gagccc agactc ctctgctgac
ctgctgacct gtgcacaccc cactcccaca
tgggcccagg cctcaccatt ccaagtctcc
atttct agaccaataa agagacgaat
acaact aactcagcaa aa
aa aa aa
aa aa aa
aa aa

  
  
  
  mskiseavkr araafssgrt rplqfriqql
ealqrliqeq eqelvgalaa dlhknewnay
yeevvyvlee ieymiqklpe waadepvekt
pqtqqdelyi hseplgvvlv igtwnypfnl
tiqpmvgaia agnsvvlkps elsenmasll
atiipqyldk dlypvinggv pettellker
fdhilytgst gvgkiimtaa akhltpvtle
lggkspcyvd kncdldvacr riawgkfmns
gqtcvapdyi lcdpsiqnqi veklkkslke
fygedakksr dygriisarh fqrvmglieg
qkvayggtgd aatryiapti ltdvdpqspv
mqeeifgpvl pivcvrslee aiqfinqrek
plalymfssn dkvikkmiae tssggvaand
vivhitlhsl pfggvgnsgm gsyhgkksfe
tfshrrsclv rplmndeglk vryppspakm
tqh



  
CC

 
   

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

2005-12-20 Thread Kent Johnson
ps python wrote:
> Dear Drs. Johnson and Yoo , 
>  for the last 1 week I have been working on parsing
> the elements from a bunch of XML files following your
> suggestions. 
> 
> from elementtree.ElementTree import ElementTree
> 
mydata = ElementTree(file='4.xml')
for process in
> 
> mydata.findall('//biological_process'):
>   print process.text

Looking at the data, neither  nor  elements 
directly
contain text, they have children that contain text. Try
   print process.get('title').text
to print the title.

for proc in mydata.findall('functions'):
>   print proc

I think you want findall('//functions') to find  at any depth in the 
tree.

If this doesn't work please show the results you get and tell us what you 
expect.

Kent

___
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-20 Thread ps python
Thank you for your email Dr. Johnson. 


I need to print :

gene_symbol (from line
ALDH3A1)
entry_cdna  (from line
NM_000691.3)
molecular_class 
(from line
Enzyme:Dehydrogenase)


title (from tags Catalytic
activity)

title (from tags section 
Metabolism)

title (from tags section 
cytoplasm)


This is how I tried:

from elementtree.ElementTree import ElementTree
mydata = ElementTree(file='4.xml')
>>> for process in
mydata.findall('//biological_process'):
print process.get('title').text


>>> for m in mydata.findall('//functions'):
print m.get('molecular_class').text


>>> for m in mydata.findall('//functions'):
print m.find('molecular_class').text.strip()


>>> for process in
mydata.findall('//biological_process'):
print process.get('title').text


>>> for m in mydata.findall('//functions'):
print m.get('molecular_class').text


>>> for m in mydata.findall('//functions'):
print m.get('title').text.strip()


>>> for m in mydata.findall('//biological_processes'):
  print m.get('title').text.strip()

  
>>> 


Result:
I get nothing.  No error.  I have no clue why it is
not giving me the result. 

I also tried this alternate way:

>>> strdata = """
  Enzyme:
Dehydrogenase
  
 
Catalytic activity
0003824
 
  
  
 
Metabolism
0008152
 
 
Energy pathways
0006091
 
  
"""

>>> from elementtree import ElementTree
>>> tree = ElementTree.fromstring(strdata)
>>> for m in tree.findall('//functions'):
print m.find('molecular_class').text



Traceback (most recent call last):
  File "", line 1, in -toplevel-
for m in tree.findall('//functions'):
  File
"C:\Python23\Lib\site-packages\elementtree\ElementTree.py",
line 352, in findall
return ElementPath.findall(self, path)
  File
"C:\Python23\Lib\site-packages\elementtree\ElementPath.py",
line 195, in findall
return _compile(path).findall(element)
  File
"C:\Python23\Lib\site-packages\elementtree\ElementPath.py",
line 173, in _compile
p = Path(path)
  File
"C:\Python23\Lib\site-packages\elementtree\ElementPath.py",
line 74, in __init__
raise SyntaxError("cannot use absolute path on
element")
SyntaxError: cannot use absolute path on element
>>> for m in tree.findall('functions'):
print m.find('molecular_class').text


>>> for m in tree.findall('functions'):
print m.find('molecular_class').text.strip()


>>> for m in tree.findall('functions'):
print m.get('molecular_class').text




Do you thing it is a problem with the XML files
instead. 

Thank you for valuable suggestions. 

kind regards, 
M




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

> ps python wrote:
> > Dear Drs. Johnson and Yoo , 
> >  for the last 1 week I have been working on
> parsing
> > the elements from a bunch of XML files following
> your
> > suggestions. 
> > 
> > from elementtree.ElementTree import ElementTree
> > 
> mydata = ElementTree(file='4.xml')
> for process in
> > 
> > mydata.findall('//biological_process'):
> > print process.text
> 
> Looking at the data, neither 
> nor  elements directly
> contain text, they have children that contain text.
> Try
>print process.get('title').text
> to print the title.
> 
> for proc in mydata.findall('functions'):
> > print proc
> 
> I think you want findall('//functions') to find
>  at any depth in the tree.
> 
> If this doesn't work please show the results you get
> and tell us what you expect.
> 
> Kent
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


Send instant messages to your online friends http://in.messenger.yahoo.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-20 Thread Danny Yoo

> >>> for m in mydata.findall('//functions'):
>   print m.get('molecular_class').text
>
> >>> for m in mydata.findall('//functions'):
>   print m.find('molecular_class').text.strip()
>
> >>> for process in
> mydata.findall('//biological_process'):
>   print process.get('title').text


Hello,

I believe we're running into XML namespace issues.  If we look at all the
tag names in the XML, we can see this:

##
>>> from elementtree import ElementTree
>>> tree = ElementTree.parse(open('4.xml'))
>>> for element in tree.getroot()[0]: print element.tag
...
{org:hprd:dtd:hprdr2}title
{org:hprd:dtd:hprdr2}alt_title
{org:hprd:dtd:hprdr2}alt_title
{org:hprd:dtd:hprdr2}alt_title
{org:hprd:dtd:hprdr2}alt_title
{org:hprd:dtd:hprdr2}alt_title
{org:hprd:dtd:hprdr2}omim
{org:hprd:dtd:hprdr2}gene_symbol
{org:hprd:dtd:hprdr2}gene_map_locus
{org:hprd:dtd:hprdr2}seq_entry
{org:hprd:dtd:hprdr2}molecular_weight
{org:hprd:dtd:hprdr2}entry_sequence
{org:hprd:dtd:hprdr2}protein_domain_architecture
{org:hprd:dtd:hprdr2}expressions
{org:hprd:dtd:hprdr2}functions
{org:hprd:dtd:hprdr2}cellular_component
{org:hprd:dtd:hprdr2}interactions
{org:hprd:dtd:hprdr2}EXTERNAL_LINKS
{org:hprd:dtd:hprdr2}author
{org:hprd:dtd:hprdr2}last_updated
##

(I'm just doing a quick view of the toplevel elements in the tree.)

As we can see, each element's tag is being prefixed with the namespace URL
provided in the XML document.  If we look in our XML document and search
for the attribute 'xmlns', we'll see where this 'org:hprd:dtd:hprdr2'
thing comes from.


So we may need to prepend the namespace to get the proper terms:

##
>>> for process in tree.find("//{org:hprd:dtd:hprdr2}biological_processes"):
... print process.findtext("{org:hprd:dtd:hprdr2}title")
...
Metabolism
Energy pathways
##


To tell the truth, I don't quite understand how to work fluently with XML
namespaces, so perhaps there's an easier way to do what you want.  But the
examples above should help you get started parsing all your Gene Ontology
annotations.



Good luck!

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


[Tutor] Learning books

2005-12-20 Thread Richard
Afternoon all, My son asked me what books I would like for Christmas 
this year. So what would you recommend?

I am a beginner here.


Thanks


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


Re: [Tutor] question !

2005-12-20 Thread Danny Yoo


On Mon, 19 Dec 2005, Krava Magare wrote:

>  How can I remove and add record ( dictionary type) to a file.

Hi Krava,

H... I have to admit that I don't understand the question yet.
*grin*


It looks like you're already pickling lists into your file.  Picking
dictionaries should be similar; are you running into problems with this,
and if so, can you show us?


Some more comments on your program:

>   def  write_file():
>   CIT101 = ["Academic Computer Skills"]
>   CIT111 = ["Database Management"]
>   CIT115 = ["Intro to Computer scince"]
>   CIT127 = ["ACCESS"]
>   CIT211 = ["Systems Analysis and Design"]
>   CIT216 = ["Visual Basic"]
>   CIT218 = ["Intermediate Visual Basic"]
>   CIT234 = ["Decision Support Using Excel"]

This does feel like a place where a dictionary would come in very handy:
The program uses names of the form 'CIT???' which I am assuming are
academic class identifier name, and you're trying to store a relationship
between those names and their descriptsions.  That's a key-value thing.


>   pickle_file.close

Don't forget that Python requires parentheses to make function calls fire
off.  The above call to close doesn't do anything yet: you need parens:

pickle_file.close()



>   def dele_file():
>   word_dele = raw_input("Which record do u want to delete?: ")
>
>   if word_dele in picles.keys():
^^

This is misspelled.


Also, the del call on the next statement:

>   del word_dele

is ineffective because it only drops the local name 'word_dele' and does
not affect your pickle.


You probably meant to write:

del pickles[word_dele]

instead.



It looks like you're getting mixed up when you're using pickles.  Mixing
the 'pickle' and 'shelve' modules together is confusing and something you
may want to avoid.

If you're used to using dictionaries, I'd strongly recommend just sticking
with the 'shelve' module for persistance: it provides a dictionary-like
interface that is fairly straightforward.  You can even say something
like:

pickles['academic_classes'] = {'CIT101' : 'Academic Computer Skills'}

The values that we store in shelves can be Python data structures: they're
not limited to numbers.



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

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


Re: [Tutor] Learning books

2005-12-20 Thread nephish
Learning Python by O'Reilly,
got me started after realizing that Programming Python by O'Reilly was a
tad over me head.

i am new here too.

On Tue, 2005-12-20 at 14:46 -0600, Richard wrote:
> Afternoon all, My son asked me what books I would like for Christmas 
> this year. So what would you recommend?
> 
> I am a beginner here.
> 
> 
> Thanks
> 
> 
> Richard
> ___
> 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] Learning books

2005-12-20 Thread Danny Yoo


On Tue, 20 Dec 2005, Richard wrote:

> Afternoon all, My son asked me what books I would like for Christmas
> this year. So what would you recommend?

Merry Chrismas!

I'm partial to George Polya's "How to Solve It":

http://www.math.utah.edu/~alfeld/math/polya.html

It's not Python nor programming specific, but instead it's more of a
general problem-solving-strategy book.  I wish I had seen it earlier in
life, so I'm hoping this recommendation is helpful.  It's one of those
books that's surprisingly applicable.


I'm also very fond of "The Psycology Of Computer Programming":

http://www.dorsethouse.com/books/psy.html

which also talks about learning.  Again, not Python specific in the
slightest (some of the code in there is Cobol!  *grin*), but very useful
and fun to read.



Good luck to you!

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


Re: [Tutor] Learning books

2005-12-20 Thread Srinivas Iyyer

1. Learn to Program Using Python by Alan Gauld (Stage
1)
2. Learning Python by Mark Lutz and David Ascher
(Stage 2)
3. If u are serious in learning python - my sincere
advise : STICK TO THIS LIST. 
You can never find good teachers with lots of patience
and with real appetite to teach elesewhere. To name
some excellent teachers here on this list: Kent,
Danny, Alan (Alan gauld himself). There are many more
and I do not remember their names. I thank them a lot
every time I try to solve my problems. Without this
list my Ph.D. would not have been possible and I
learned many thing in just 5 months. So, these 3
sources are enough to help you carve a good python
programmer inside you. 
Good luck.







--- Richard <[EMAIL PROTECTED]> wrote:

> Afternoon all, My son asked me what books I would
> like for Christmas 
> this year. So what would you recommend?
> 
> I am a beginner here.
> 
> 
> Thanks
> 
> 
> Richard
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question !

2005-12-20 Thread Brian van den Broek
Krava Magare said unto the world upon 2005-12-19 17:31:
>  How can I remove and add record ( dictionary  type) to a file. This is the 
> program that I'm working on: the program  should create a text file, print 
> the contents of the text file, read  the file after it's been created, add a 
> record and print the contents  of the file, remove a record(s) from the 
> specified file, write it  again, read it again, print the contens of the new 
> file.
>Here is what I have so far: 

Hi,

I see that you did follow up to comp.lang.python with code and then 
found your way here. Both are better than how you started  :-)

But you really might want to read 
 to understand why 
you've had no replies. This list (tutor) is far less strict about the 
etiquette, but the advice is still good. (Keep in mind it is a busy 
time of year, too :-)

Looking at what you have:

>   import cPickle, shelve
>   def  write_file(): 
>   CIT101 = ["Academic Computer Skills"]

>   CIT234 = ["Decision Support Using Excel"]
>   pickle_file = open("pickles1.dat","w")
>   
>   cPickle.dump(CIT101, pickle_file)

>   cPickle.dump(CIT234, pickle_file)
>   print "A file has been created and the required specifications have 
> been added"
>   pickle_file.close




I suspect you don't really understand what pickle does. The task you 
describe is to create text files, whereas pickle and cpickle are used 
to save state of Python objects. They produce just barely human 
readable files. For instance:

 >>> pickled = open('pick', 'w')
 >>> pickle.dump("This is a string", pickled)
 >>> pickle.dump(["This is a list containing a string"], pickled)
 >>> pickled.close()

produces this file:

S'This is a string'
p0
.(lp0
S'This is a list containing a string'
p1
a.

where the strings are still there, but surrounded by 'gunk' that lets 
the pickle modules reconstruct what was pickled.

In a more complicated case, pickled files are not at all readable. H 
here is a pickle of an instance of a simple class:

ccopy_reg
_reconstructor
p0
(c__main__
A
p1
c__builtin__
object
p2
Ntp3
Rp4
(dp5
S'a'
p6
I1
sS'c'
p7
I3
sS'b'
p8
I2
sb.ccopy_reg
_reconstructor
p0
(c__main__
A
p1
c__builtin__
object
p2
Ntp3
Rp4
(dp5
S'a'
p6
I4
sS'c'
p7
I6
sS'b'
p8
I5
sb.


Definitely not the sort of thing that is useful to a human!

I think you should consider switching tactics, and examine file() and 
the read, readlines, write, and writelines methods of file objects.

HTH,

Brian vdB

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


[Tutor] How do I fix this IndexError?

2005-12-20 Thread Nathan Pinno



Here is the 
error:
The Currency Exchange 
ProgramBy Nathan Pinno
 
Traceback (most recent 
call last):  File "D:\Python24\exchange.py", line 28, in 
-toplevel-    exch = pickle.load(store)  File 
"D:\Python24\lib\pickle.py", line 1390, in load    return 
Unpickler(file).load()  File "D:\Python24\lib\pickle.py", line 872, in 
load    dispatch[key](self)  File 
"D:\Python24\lib\pickle.py", line 1207, in load_appends    
mark = self.marker()  File "D:\Python24\lib\pickle.py", line 888, in 
marker    while stack[k] is not mark: k = k-1IndexError: 
list index out of range
 
and here is the 
relevant code:
[code]
import picklerates 
= {'can_us' : 0.80276, 
'us_can' : 1.245702, 
'can_euro' : 1.488707, 
'euro_can' : 0.671724}
 
def 
menu():    print "1. Change Canadian currency into 
American."    print "2. Change American currency into 
Canadian."    print "3. Change Canadian currency into 
Euros."    print "4. Change Euros into Canadian 
currency."    print "5. Update exchange 
rates."    print "9. Save and Exit"
 
def 
exchange_update():    print "1. Update Canadian to US 
rate."    print "2. Update US to Canadian 
rate."    print "3. Update Canadian to Euro 
rate."    print "4. Update Euro to Canadian 
update."    print "5. Main menu"
 
def 
menu_choice():    return int(raw_input("Which option? 
"))
 
print "The Currency 
Exchange Program"print "By Nathan Pinno"store = open('exch.txt', 
'rb')#loadexch = pickle.load(store)store.close()while 
1:    menu()    menu_option = 
menu_choice()    if menu_option == 
1:    can = float(raw_input("Canadian 
$"))    print "US 
$",can*rates['can_us']    elif menu_option == 
2:    us = float(raw_input("US 
$"))    print "CAN 
$",us*rates['us_can']    elif menu_option == 
3:    can = float(raw_input("CAN 
$"))    print 
"Euros",can*rates['can_euro']    elif menu_option == 
4:    euro = 
float(raw_input("Euros"))    print 
"CAN $",euro*rates['euro_can']    elif menu_option == 
5:    while 
1:    
exchange_update()    
sub = 
menu_choice()    
if sub == 
1:    
new_can = float(raw_input("New CAN-US Exchange rate: 
"))    
rates['can_us'] = 
new_can    
print "Exchange rate successfully 
updated!"    
elif sub == 
2:    
new_us = float(raw_input("New US-CAN Exchange rate: 
"))    
rates['us_can'] = 
new_us    
print "Exchange rate successfully 
updated!"    
elif sub == 
3:    
new_cxr = float(raw_input("New CAN-Euro Exchange rate: 
"))    
rates['can_euro'] = 
new_cxr    
print "Exchange rate successfully 
updated!"    
elif sub == 
4:    
new_euro = float(raw_input("New Euro-CAN Exchange rate: 
"))    
rates['euro_can'] = 
new_euro    
print "Exchange rate successfully 
updated!"    
elif sub == 
5:    
break    elif menu_option == 
9:    store = open("exch.txt", 'wb') 
#save    pickle.dump(exch, 
store)    
store.close()    breakprint 
"Goodbye."
[/code]
 
How do I fix the 
IndexError?
 
Thanks,
Nathan Pinno,

MSN Messenger: [EMAIL PROTECTED]
Yahoo! Messenger: spam_swatter31
AIM: f3mighty
ICQ: 199020705  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning books

2005-12-20 Thread Alan Gauld
> Afternoon all, My son asked me what books I would like for Christmas this 
> year. So what would you recommend?

I love these questions! :-)

> I am a beginner here.

The question you need to answer is what do you want to get out of the book?

For example you could get a book that teaches you the basics of programming,
(and from this list probably doing so in Python). But after you understand 
it you
probably won't read it again.

My book, Learning Python and a few others fit in here.

Alternatively you could get a reference book that will be used constantly
*after* you learn but will be of limited use until then.

Programming Python, Python Prog on Win32, Python in a Nutshell etc
are good examples here

Or you might pick a specific subject area(Web, Databases, GUI, Games)
and get a book that focuses on that area. ie. A specialised tutorial and 
reference.

Text Processing in Python, Python & Tkinter Programming, etc are examples
of this genre

Finally you might get a more general computer science type book that applies
to all programming languages.

Code Complete, Programming Pearls, The Pragmatic Programmer and
Structure and Interpretation of Computer Programs are all examples here.

Or of course you might just opt for a novel, put your feet up and relax for
a few hours! :-)

FWIW The books I actually use most (for Python programming) are:

Python in a Nutshell
Python Programming on Win32
Tcl/Tk in a Nutshell (for Tkinter/Tix stuff)
Using C on the Unix System (for low level OS stuff)
[This is now superceded by Unix Systems Programming for SVR4
but I prefer the older books shorter explanations!]
Java in a Nutshell (for Jython)
Programming Pearls
HTML The Definitive Guide

And for general computing:

Code Complete
Programming Pearls
Software Engineering, A Practitioners Approach(2nd and 5th editions)
OO Design & Analysis(1st edition) by Grady Booch
Oracle 8 the Complete Reference (for SQL/Database topics)

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] How do I fix this IndexError?

2005-12-20 Thread Simon Gerber
Hi Nathan,

> Traceback (most recent call last):
>   File "D:\Python24\exchange.py", line 28, in -toplevel-
> exch = pickle.load(store)
>   File "D:\Python24\lib\pickle.py", line 1390, in load
> return Unpickler(file).load()
>   File "D:\Python24\lib\pickle.py", line 872, in load
> dispatch[key](self)
>   File "D:\Python24\lib\pickle.py", line 1207, in load_appends
> mark = self.marker()
>   File "D:\Python24\lib\pickle.py", line 888, in marker
> while stack[k] is not mark: k = k-1
> IndexError: list index out of range

As you can see from the traceback, the error is occuring entirely in
the pickle module. Which probably means your pickle file is corrupted.
Or that you're trying to load your old exch.txt file which wasn't in
pickle format.

Simply delete 'exch.txt' and try again. Although you'll notice your
program still doesn't work, because it tries to load 'exch.txt' even
if there isn't an exch.txt.

You should consider rewriting your program so that it checks to see if
an 'exch.txt' file exists. If it does, it unpickles it. If not, it
uses your default 'rates' variables.

Speaking of rates, you have two typos in your code which you also need to fix.

> exch = pickle.load(store)

Should be

rates = pick.load(store)

And similarly,

> pickle.dump(exch, store)

pickle.dump(rates, store)

Otherwise you're not saving or loading anyhing!

Cheers,

--
Seen in the release notes for ACPI-support 0.34:

'The "I do not wish to discuss it" release
  * Add workaround for prodding fans back into life on resume
  * Add sick evil code for doing sick evil things to sick evil
screensavers'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I fix this IndexError?

2005-12-20 Thread Alan Gauld
Nathan,

I haven't read your code in detail but from the error message:

> Traceback (most recent call last):
>  File "D:\Python24\exchange.py", line 28, in -toplevel-
>exch = pickle.load(store)
>  File "D:\Python24\lib\pickle.py", line 1390, in load
>return Unpickler(file).load()
>  File "D:\Python24\lib\pickle.py", line 872, in load
>dispatch[key](self)
>  File "D:\Python24\lib\pickle.py", line 1207, in load_appends
>mark = self.marker()
>  File "D:\Python24\lib\pickle.py", line 888, in marker
>while stack[k] is not mark: k = k-1
> IndexError: list index out of range


The exception is happeming deep inside the picklle library so 
that usually indicates that you are not calling pickle properly 
- maybe a wrong type of data in the argument?

> store = open('exch.txt', 'rb')#load
> exch = pickle.load(store)

Have you checked that the exch.txt file is in fact a valid pickle file?
And that a readable binary file is what pickle.load() expects - from 
memory I think it is - so I'd check that exch.txt is a valid pickle file.

And check that its the right exch.txt - are there two files of that name 
that you might be picking up the wrong name for example?

Sorry, no definitive answer just some ideas for you to check out.

Alan G.

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


Re: [Tutor] How do I fix this IndexError?

2005-12-20 Thread Nathan Pinno
Simon and all,

What would you recommend? I don't know what I should code, because I haven't
coded anything that loads a file. 

Thanks,
Nathan Pinno
MSN Messenger: [EMAIL PROTECTED]
Yahoo! Messenger: spam_swatter31
AIM: f3mighty
ICQ: 199020705  

-Original Message-
From: Simon Gerber [mailto:[EMAIL PROTECTED] 
Sent: December 20, 2005 3:38 PM
To: Nathan Pinno
Cc: tutor@python.org
Subject: Re: [Tutor] How do I fix this IndexError?

Hi Nathan,

> Traceback (most recent call last):
>   File "D:\Python24\exchange.py", line 28, in -toplevel-
> exch = pickle.load(store)
>   File "D:\Python24\lib\pickle.py", line 1390, in load
> return Unpickler(file).load()
>   File "D:\Python24\lib\pickle.py", line 872, in load
> dispatch[key](self)
>   File "D:\Python24\lib\pickle.py", line 1207, in load_appends
> mark = self.marker()
>   File "D:\Python24\lib\pickle.py", line 888, in marker
> while stack[k] is not mark: k = k-1
> IndexError: list index out of range

As you can see from the traceback, the error is occuring entirely in the
pickle module. Which probably means your pickle file is corrupted.
Or that you're trying to load your old exch.txt file which wasn't in pickle
format.

Simply delete 'exch.txt' and try again. Although you'll notice your program
still doesn't work, because it tries to load 'exch.txt' even if there isn't
an exch.txt.

You should consider rewriting your program so that it checks to see if an
'exch.txt' file exists. If it does, it unpickles it. If not, it uses your
default 'rates' variables.

Speaking of rates, you have two typos in your code which you also need to
fix.

> exch = pickle.load(store)

Should be

rates = pick.load(store)

And similarly,

> pickle.dump(exch, store)

pickle.dump(rates, store)

Otherwise you're not saving or loading anyhing!

Cheers,

--
Seen in the release notes for ACPI-support 0.34:

'The "I do not wish to discuss it" release
  * Add workaround for prodding fans back into life on resume
  * Add sick evil code for doing sick evil things to sick evil
screensavers'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I fix this IndexError?

2005-12-20 Thread Simon Gerber
Hi Nathan,

I've attached a crude hack that fixes the problem. It should give you
an idea of how to proceed. I've put comments next to the things I've
changed. Bear in mind I'm hardly more than a beginner myself, so there
are definitely better ways to do this. But it's somewhere to start,
anyway.

Cheers!

[code]

import pickle, os #LOAD THE OS MODULE

def menu():
print "1. Change Canadian currency into American."
print "2. Change American currency into Canadian."
print "3. Change Canadian currency into Euros."
print "4. Change Euros into Canadian currency."
print "5. Update exchange rates."
print "9. Save and Exit"

def exchange_update():
print "1. Update Canadian to US rate."
print "2. Update US to Canadian rate."
print "3. Update Canadian to Euro rate."
print "4. Update Euro to Canadian update."
print "5. Main menu"

def menu_choice():
return int(raw_input("Which option? "))

print "The Currency Exchange Program"
print "By Nathan Pinno"

if os.path.exists('exch.txt'): # Check to see if 'exch.txt' exists
store = open('exch.txt', 'rb')  # Only open the file if it exists
rates = pickle.load(store)# Save as 'rates', not 'exch'
store.close()
else:
# If there's no exch.txt file, use these default rates.
rates = {'can_us' : 0.80276,
'us_can' : 1.245702,
'can_euro' : 1.488707,
'euro_can' : 0.671724}

while 1:
menu()
menu_option = menu_choice()
if menu_option == 1:
can = float(raw_input("Canadian $"))
print "US $",can*rates['can_us']
elif menu_option == 2:
us = float(raw_input("US $"))
print "CAN $",us*rates['us_can']
elif menu_option == 3:
can = float(raw_input("CAN $"))
print "Euros",can*rates['can_euro']
elif menu_option == 4:
euro = float(raw_input("Euros"))
print "CAN $",euro*rates['euro_can']
elif menu_option == 5:
while 1:
exchange_update()
sub = menu_choice()
if sub == 1:
new_can = float(raw_input("New CAN-US Exchange rate: "))
rates['can_us'] = new_can
print "Exchange rate successfully updated!"
elif sub == 2:
new_us = float(raw_input("New US-CAN Exchange rate: "))
rates['us_can'] = new_us
print "Exchange rate successfully updated!"
elif sub == 3:
new_cxr = float(raw_input("New CAN-Euro Exchange rate: "))
rates['can_euro'] = new_cxr
print "Exchange rate successfully updated!"
elif sub == 4:
new_euro = float(raw_input("New Euro-CAN Exchange rate: "))
rates['euro_can'] = new_euro
print "Exchange rate successfully updated!"
elif sub == 5:
break
elif menu_option == 9:
store = open("exch.txt", 'wb') #save
pickle.dump(rates, store) # Save 'rates' variable, not 'exch'.
store.close()
break
print "Goodbye."


--
Seen in the release notes for ACPI-support 0.34:

'The "I do not wish to discuss it" release
  * Add workaround for prodding fans back into life on resume
  * Add sick evil code for doing sick evil things to sick evil
screensavers'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I fix this IndexError?

2005-12-20 Thread Nathan Pinno
Thanks Simon!

It worked like a charm! All I have to do now is change it into GUI format,
and then use py2exe to create an executable file. 

Thanks for all the help!
Nathan Pinno
MSN Messenger: [EMAIL PROTECTED]
Yahoo! Messenger: spam_swatter31
AIM: f3mighty
ICQ: 199020705  

-Original Message-
From: Simon Gerber [mailto:[EMAIL PROTECTED] 
Sent: December 20, 2005 3:55 PM
To: Nathan Pinno
Cc: tutor@python.org
Subject: Re: [Tutor] How do I fix this IndexError?

Hi Nathan,

I've attached a crude hack that fixes the problem. It should give you an
idea of how to proceed. I've put comments next to the things I've changed.
Bear in mind I'm hardly more than a beginner myself, so there are definitely
better ways to do this. But it's somewhere to start, anyway.

Cheers!

[code]

import pickle, os #LOAD THE OS MODULE

def menu():
print "1. Change Canadian currency into American."
print "2. Change American currency into Canadian."
print "3. Change Canadian currency into Euros."
print "4. Change Euros into Canadian currency."
print "5. Update exchange rates."
print "9. Save and Exit"

def exchange_update():
print "1. Update Canadian to US rate."
print "2. Update US to Canadian rate."
print "3. Update Canadian to Euro rate."
print "4. Update Euro to Canadian update."
print "5. Main menu"

def menu_choice():
return int(raw_input("Which option? "))

print "The Currency Exchange Program"
print "By Nathan Pinno"

if os.path.exists('exch.txt'): # Check to see if 'exch.txt' exists
store = open('exch.txt', 'rb')  # Only open the file if it exists
rates = pickle.load(store)# Save as 'rates', not 'exch'
store.close()
else:
# If there's no exch.txt file, use these default rates.
rates = {'can_us' : 0.80276,
'us_can' : 1.245702,
'can_euro' : 1.488707,
'euro_can' : 0.671724}

while 1:
menu()
menu_option = menu_choice()
if menu_option == 1:
can = float(raw_input("Canadian $"))
print "US $",can*rates['can_us']
elif menu_option == 2:
us = float(raw_input("US $"))
print "CAN $",us*rates['us_can']
elif menu_option == 3:
can = float(raw_input("CAN $"))
print "Euros",can*rates['can_euro']
elif menu_option == 4:
euro = float(raw_input("Euros"))
print "CAN $",euro*rates['euro_can']
elif menu_option == 5:
while 1:
exchange_update()
sub = menu_choice()
if sub == 1:
new_can = float(raw_input("New CAN-US Exchange rate: "))
rates['can_us'] = new_can
print "Exchange rate successfully updated!"
elif sub == 2:
new_us = float(raw_input("New US-CAN Exchange rate: "))
rates['us_can'] = new_us
print "Exchange rate successfully updated!"
elif sub == 3:
new_cxr = float(raw_input("New CAN-Euro Exchange rate: "))
rates['can_euro'] = new_cxr
print "Exchange rate successfully updated!"
elif sub == 4:
new_euro = float(raw_input("New Euro-CAN Exchange rate: "))
rates['euro_can'] = new_euro
print "Exchange rate successfully updated!"
elif sub == 5:
break
elif menu_option == 9:
store = open("exch.txt", 'wb') #save
pickle.dump(rates, store) # Save 'rates' variable, not 'exch'.
store.close()
break
print "Goodbye."


--
Seen in the release notes for ACPI-support 0.34:

'The "I do not wish to discuss it" release
  * Add workaround for prodding fans back into life on resume
  * Add sick evil code for doing sick evil things to sick evil
screensavers'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning books

2005-12-20 Thread Kent Johnson
Richard wrote:
> Afternoon all, My son asked me what books I would like for Christmas 
> this year. So what would you recommend?

I have started a book list on my web site.
http://personalpages.tds.net/~kent37/BookList.html

Kent

___
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-20 Thread ps python
Dear drs. Yoo and johnson, 
Thank you very much for your help. I successully
parsed my GO annotation from all 16,000 files. 
thanks again for your kind help



--- Danny Yoo <[EMAIL PROTECTED]> wrote:

> 
> > >>> for m in mydata.findall('//functions'):
> > print m.get('molecular_class').text
> >
> > >>> for m in mydata.findall('//functions'):
> > print m.find('molecular_class').text.strip()
> >
> > >>> for process in
> > mydata.findall('//biological_process'):
> > print process.get('title').text
> 
> 
> Hello,
> 
> I believe we're running into XML namespace issues. 
> If we look at all the
> tag names in the XML, we can see this:
> 
> ##
> >>> from elementtree import ElementTree
> >>> tree = ElementTree.parse(open('4.xml'))
> >>> for element in tree.getroot()[0]: print
> element.tag
> ...
> {org:hprd:dtd:hprdr2}title
> {org:hprd:dtd:hprdr2}alt_title
> {org:hprd:dtd:hprdr2}alt_title
> {org:hprd:dtd:hprdr2}alt_title
> {org:hprd:dtd:hprdr2}alt_title
> {org:hprd:dtd:hprdr2}alt_title
> {org:hprd:dtd:hprdr2}omim
> {org:hprd:dtd:hprdr2}gene_symbol
> {org:hprd:dtd:hprdr2}gene_map_locus
> {org:hprd:dtd:hprdr2}seq_entry
> {org:hprd:dtd:hprdr2}molecular_weight
> {org:hprd:dtd:hprdr2}entry_sequence
> {org:hprd:dtd:hprdr2}protein_domain_architecture
> {org:hprd:dtd:hprdr2}expressions
> {org:hprd:dtd:hprdr2}functions
> {org:hprd:dtd:hprdr2}cellular_component
> {org:hprd:dtd:hprdr2}interactions
> {org:hprd:dtd:hprdr2}EXTERNAL_LINKS
> {org:hprd:dtd:hprdr2}author
> {org:hprd:dtd:hprdr2}last_updated
> ##
> 
> (I'm just doing a quick view of the toplevel
> elements in the tree.)
> 
> As we can see, each element's tag is being prefixed
> with the namespace URL
> provided in the XML document.  If we look in our XML
> document and search
> for the attribute 'xmlns', we'll see where this
> 'org:hprd:dtd:hprdr2'
> thing comes from.
> 
> 
> So we may need to prepend the namespace to get the
> proper terms:
> 
> ##
> >>> for process in
>
tree.find("//{org:hprd:dtd:hprdr2}biological_processes"):
> ... print
> process.findtext("{org:hprd:dtd:hprdr2}title")
> ...
> Metabolism
> Energy pathways
> ##
> 
> 
> To tell the truth, I don't quite understand how to
> work fluently with XML
> namespaces, so perhaps there's an easier way to do
> what you want.  But the
> examples above should help you get started parsing
> all your Gene Ontology
> annotations.
> 
> 
> 
> Good luck!
> 
> 


Send instant messages to your online friends http://in.messenger.yahoo.com 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor