Re: xml.etree.ElementTree if element does not exist?

2013-04-29 Thread Stefan Holdermans
Ombongi,

> however, if i pass xml data that DOES NOT contain sepid element, i get an 
> error:
> 
> Traceback (most recent call last):
>   File "/usr/local/bin/receive.py", line 21, in 
> sepid = 
> content.find(".//{http://www.huawei.com.cn/schema/common/v2_1}sepid";).text
> AttributeError: 'NoneType' object has no attribute 'text'
> 
> 
> some messages i receive will have the sepid parameter, other will not have 
> this parameter. How can i cater for this? kinda like an if .. else 
> implementation for xml.etree.ElementTree  ?

What about simply testing whether the value returned by find is None? For 
example:

  $ cat test.py
  from xml.etree import ElementTree

  myTree = ElementTree.fromstring('')
  myElement = myTree.find('orange')

  if myElement is None:
  print 'tree does not contain a child element "orange"'
  else:
  print myElement.text


  $ python test.py
  tree does not contain a child element "orange"

HTH,

  Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: request for help

2013-02-18 Thread Stefan Holdermans
Leonardi,

> i saved the above program from python shell into a file as "circle.py" . when 
> i type "import circle" i get  error..


Next time, please mention what kind of error you're getting.

Was it an indentation error? Because, as you pasted it, your code would lead to 
one.

If I fix the indentation, as in

  import math

  def area(radius):
return math.pi * radius**2

  def circumference(radius):
return 2 * math.pi * radius

it works fine for me.

HTH,

  Stefan
-- 
http://mail.python.org/mailman/listinfo/python-list