Fwd: Re: How to improve this code?

2009-09-16 Thread Hendrik van Rooyen

From a private email, forwarded to the list:

--  Forwarded Message  --

Subject: Re: How to improve this code?
Date: Tuesday 15 September 2009
From: Oltmans rolf.oltm...@gmail.com
To: hend...@microcorp.co.za

On Sep 15, 1:13 pm, Hendrik van Rooyen hend...@microcorp.co.za
wrote:


 (i)  a True if All the elements in match are in aList, else False?
 (ii) a True if any one or more of the members of match  are in aList?
 (iii)  Something else?


That's a good question because I failed miserably in explaining my
problem clearly. My original question isn't what I'm trying to solve.
My apologies. I will try to explain here clearly. I'm using a 3rd-
party library named Selenium (used for web-automation) and it has a
method named is_element_present(ele) i.e. takes one element and return
true if it finds this element in the page's HTML and returns false
otherwise. Given this, I'm just trying to write a method
are_elements_present(aList) whose job is to return True if and only if
all elements in aList are present in page's HTML. So here is how
are_elements_present() looks like


  def are_elements_present(eleLocators):
elePresent=False
if not eleLocators:
return False

for ele in eleLocators:
if selenium.is_element_present(ele):
elePresent=True
else:
elePresent=False
print 'cannot find this element= '+str(ele)
break
return elePresent



Now suppose page HTML contains with these IDs ( ID is an attribute
like input id=inp1 /) = div1,div2,div3,div4,div5,inp1,inp2
and if I call the above method this way are_elements_present
([div1,div2,inp1,inp2]) then it should return True. If I call like
are_elements_present([div1,div2,div10,inp1]) it should return False.
So I hope I've explained myself. Now all I'm looking for is to write
are_elements_presents() in a more Pythonic way. So please let me know
if I can write are_elements_present() in more smart/shorter way.

Thanks a lot for your help, in advance.
Best regards,
Oltmans


---

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


Re: Fwd: Re: How to improve this code?

2009-09-16 Thread Thomas Lehmann
 otherwise. Given this, I'm just trying to write a method
 are_elements_present(aList) whose job is to return True if and only if
 all elements in aList are present in page's HTML. So here is how


missingItems = [str(ele) for ele in eleLocators if not
selenium.is_element_present(ele)]
if len(missingItems)  0:
   print missing items: %s % (missingItems)


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


Re: Fwd: Re: How to improve this code?

2009-09-16 Thread Duncan Booth
Hendrik van Rooyen hend...@microcorp.co.za wrote:

   def are_elements_present(eleLocators):
 elePresent=False
 if not eleLocators:
 return False
 
 for ele in eleLocators:
 if selenium.is_element_present(ele):
 elePresent=True
 else:
 elePresent=False
 print 'cannot find this element= '+str(ele)
 break
 return elePresent
 
 
 
 Now suppose page HTML contains with these IDs ( ID is an attribute
 like input id=inp1 /) = div1,div2,div3,div4,div5,inp1,inp2
 and if I call the above method this way are_elements_present
 ([div1,div2,inp1,inp2]) then it should return True. If I call like
 are_elements_present([div1,div2,div10,inp1]) it should return False.
 So I hope I've explained myself. Now all I'm looking for is to write
 are_elements_presents() in a more Pythonic way. So please let me know
 if I can write are_elements_present() in more smart/shorter way.
 
The obvious way to write this would seem to me to be (untested code):

  def are_elements_present(eleLocators):
 return all(selenium.is_element_present(ele) for ele in eleLocators)

However be aware that inverts the result for the case where eleLocators is 
an empty list which may or may not matter in this situation.

-- 
Duncan Booth http://kupuguy.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list