How to improve this code?

Sol Toure sol2ray at gmail.com
Tue Sep 15 12:08:58 EDT 2009


def are_elements_present(sourceList, searchList):    for e in searchList:
          if e not in sourceList:
               return False
    return True


Using set:
  def are_elements_present(sourceList, searchList):
         return len(set(sourceList).intersection(set(searchList)) ==
len(searchList)


On Tue, Sep 15, 2009 at 11:11 AM, Oltmans <rolf.oltmans at gmail.com> wrote:

> On Sep 15, 1:13 pm, Hendrik van Rooyen <hend... at 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090915/32cf48ce/attachment.html>


More information about the Python-list mailing list