Re: How to improve writing code in python?
Banh banh0...@gmail.com writes: Hi, I have a problem with learning Python. My code is really bad and I can't solve many problems. I really want to improve it. Do you know any website which helps me to learn python effectively (for beginners)? This is my first programming language and I am studying about manipulating the images using Python. If you also know any websites relating to manipulate the images, can you guys tell me? Thank you! For learning programming you could do the Udacity course Introduction to Computer Science: https://www.udacity.com/course/cs101 -- Piet van Oostrum p...@vanoostrum.org WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve writing code in python?
- Original Message - Hi, I have a problem with learning Python. My code is really bad and I can't solve many problems. I really want to improve it. Do you know any website which helps me to learn python effectively (for beginners)? This is my first programming language and I am studying about manipulating the images using Python. If you also know any websites relating to manipulate the images, can you guys tell me? Thank you! -- http://mail.python.org/mailman/listinfo/python-list Google is your best friend, for your entire python life. python tutorial http://docs.python.org/2/tutorial/ python object http://www.tutorialspoint.com/python/python_classes_objects.htm etc... Since you're not specific about what causing you problems I can only give you these general pointers. I use a search engine every day for anything, when I'm stuck on a specific problem I ask this list with a detailed description of the exact issue I'm facing, trying to find the minimum piece of executable code reproducing the issue. cheers, JM -- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you. -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve writing code in python?
On Feb 6, 4:52 am, Banh banh0...@gmail.com wrote: Hi, I have a problem with learning Python. My code is really bad and I can't solve many problems. I really want to improve it. Do you know any website which helps me to learn python effectively (for beginners)? This is my first programming language and I am studying about manipulating the images using Python. If you also know any websites relating to manipulate the images, can you guys tell me? Thank you! One general recommendation I make to beginners is to learn to play around in the interpreter more rather than trying and struggling to write correct code. ie learn http://www.youtube.com/watch?v=_bjKDJD-CLc before http://www.youtube.com/watch?v=3L0Rncqx1yQ For playing with images, Ive heard that IPython is very neat [Not tried much myself] -- http://mail.python.org/mailman/listinfo/python-list
How to improve writing code in python?
Hi, I have a problem with learning Python. My code is really bad and I can't solve many problems. I really want to improve it. Do you know any website which helps me to learn python effectively (for beginners)? This is my first programming language and I am studying about manipulating the images using Python. If you also know any websites relating to manipulate the images, can you guys tell me? Thank you! -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve this code?
elements_present = lambda seq, match: any(((x in match) for x in seq)) On Mon, 14 Sep 2009 19:08:59 -0600, Oltmans rolf.oltm...@gmail.com wrote: Hello, Is there someway I can improve the following code(pythonically)? (Copying from IDLE) match=[1,2,3,4,5] def elementsPresent(aList): result=False if not aList: return False for e in aList: if e in match: result=True else: result = False return result elementsPresent([6,7,8,9,5]) # should return True because 5 is present in list named match. Is there somehow I can improve code in elementsPresent()? I'm not a very good programmer but I sense that idea of using a variable named 'result' inside elementsPresent() doesn't sound very good. Any ideas will be highly appreciated. Thanks, Oltmans -- Kind regards, Sergey Simonenko. -- http://mail.python.org/mailman/listinfo/python-list
Fwd: Re: How to improve this code?
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?
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?
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
Re: How to improve this code?
On Tuesday 15 September 2009 03:08:59 Oltmans wrote: match=[1,2,3,4,5] def elementsPresent(aList): result=False if not aList: return False for e in aList: if e in match: result=True else: result = False return result elementsPresent([6,7,8,9,5]) # should return True because 5 is present in list named match. What are you trying to do? Your code will only work if the last item in aList is in the match global. Is this what you want? or do you want: (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? - Hendrik -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve this code?
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.oltm...@gmail.com wrote: 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve this code?
Sol Toure wrote: 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) Unless I'm missing something, (and I didn't bother to read the original code so I may be) that's a subset test: set (searchList) = set (searchList) TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve this code?
Tim Golden wrote: Unless I'm missing something, (and I didn't bother to read the original code so I may be) that's a subset test: set (searchList) = set (searchList) (cough) or, rather: set (searchList) = set (sourceList) TJG -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: How to improve this code?
Oltmans wrote: 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 def are_all_elements_present(elems): if not elems: return False #because you want it backward return all( selenium.is_element_present(elem) for elem in elems ) The extra check is there because you're specifying that an empty list should return False, and all() returns True if no matches. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve this code?
On Mon, 14 Sep 2009 18:33:17 -0700 (PDT) André andre.robe...@gmail.com wrote: Here's an example using sets: def is_present(list_1, list_2): ...if set(list_1).intersection(set(list_2)): ... return True ...return False ... Not that it matters, but I'd probably write: def is_present(test, match): return bool(set(test) set(match)) /W -- INVALID? DE! -- http://mail.python.org/mailman/listinfo/python-list
How to improve this code?
Hello, Is there someway I can improve the following code(pythonically)? (Copying from IDLE) match=[1,2,3,4,5] def elementsPresent(aList): result=False if not aList: return False for e in aList: if e in match: result=True else: result = False return result elementsPresent([6,7,8,9,5]) # should return True because 5 is present in list named match. Is there somehow I can improve code in elementsPresent()? I'm not a very good programmer but I sense that idea of using a variable named 'result' inside elementsPresent() doesn't sound very good. Any ideas will be highly appreciated. Thanks, Oltmans -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve this code?
Oltmans schrieb: Hello, Is there someway I can improve the following code(pythonically)? (Copying from IDLE) match=[1,2,3,4,5] def elementsPresent(aList): result=False if not aList: return False for e in aList: if e in match: result=True else: result = False return result elementsPresent([6,7,8,9,5]) # should return True because 5 is present in list named match. Is there somehow I can improve code in elementsPresent()? I'm not a very good programmer but I sense that idea of using a variable named 'result' inside elementsPresent() doesn't sound very good. Any ideas will be highly appreciated. 1) don't use a global variable for your function. Pass both parameters 2) make the lookup O(1) instead O(n) for you match by using a set 3) stop when something is found 4) Unless you want the code to be working with things that are not a list, but False, the first if is superflous def elementsPresent(aList, match): match = set(match) for item in aList: if item in match: return True return False It might actually be that turning both lists to sets checking if these overlap is faster because it's in pure C. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve this code?
On Sep 14, 10:16 pm, Diez B. Roggisch de...@nospam.web.de wrote: Oltmans schrieb: Hello, Is there someway I can improve the following code(pythonically)? (Copying from IDLE) match=[1,2,3,4,5] def elementsPresent(aList): result=False if not aList: return False for e in aList: if e in match: result=True else: result = False return result elementsPresent([6,7,8,9,5]) # should return True because 5 is present in list named match. Is there somehow I can improve code in elementsPresent()? I'm not a very good programmer but I sense that idea of using a variable named 'result' inside elementsPresent() doesn't sound very good. Any ideas will be highly appreciated. 1) don't use a global variable for your function. Pass both parameters 2) make the lookup O(1) instead O(n) for you match by using a set 3) stop when something is found 4) Unless you want the code to be working with things that are not a list, but False, the first if is superflous def elementsPresent(aList, match): match = set(match) for item in aList: if item in match: return True return False It might actually be that turning both lists to sets checking if these overlap is faster because it's in pure C. Diez Here's an example using sets: def is_present(list_1, list_2): ...if set(list_1).intersection(set(list_2)): ... return True ...return False ... is_present([1,2,3], [4,5,6]) False is_present([1,2,3], [0,2,4]) True André -- http://mail.python.org/mailman/listinfo/python-list
Re: How to improve this code?
def elementsPresent(aList, match): match = set(match) for item in aList: if item in match: return True return False This could be rewritten in Python2.5+ as def elementsPresent(aList, match): match = set(match) return any(elem in match for elem in aList) though as suggested both places, the set intersection may be fastest. -tkc -- http://mail.python.org/mailman/listinfo/python-list