Re: [Tutor] beginsWith multiple prefixes
> I simply prefer the built-in one. I had no idea it could take a tuple. Me neither, that was a surprise goody in 2.5 that I hadn't seen before. > What is amazing is that I learn more from this list than I do from any other > source. Me too, and I've been subscribed for over 10 years now! :-) Alan G.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
On Fri, Dec 26, 2008 at 6:47 AM, Alan Gauld wrote: > > "Kent Johnson" wrote > >> for d in os.listdir(): >>> if MyString(d).upper().beginswith(): >>> >> >> But that won't work, the result of calling upper() will be a normal >> str, not a MyString. >> > > Ah yes. Immutability of strings strikes again. upper() returns a new > string, I forgot about that. pity. > > You can do > > if MyString(d.upper()).beginswith(...) > > But that loses a lot in elegance and is hardly better than using a > fiunction. > > Alan G > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Thank you Alan and everybody. I simply prefer the built-in one. I had no idea it could take a tuple. What is amazing is that I learn more from this list than I do from any other source. -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
"Kent Johnson" wrote for d in os.listdir(): if MyString(d).upper().beginswith(): But that won't work, the result of calling upper() will be a normal str, not a MyString. Ah yes. Immutability of strings strikes again. upper() returns a new string, I forgot about that. pity. You can do if MyString(d.upper()).beginswith(...) But that loses a lot in elegance and is hardly better than using a fiunction. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
On Thu, Dec 25, 2008 at 12:46 PM, Alan Gauld wrote: > for d in os.listdir(): > if MyString(d).beginswith(): > > Which isn't that cumbersome. d is still available for > subsequent processing and acces to normal string > methods is still useful , as in: > > for d in os.listdir(): > if MyString(d).upper().beginswith(): But that won't work, the result of calling upper() will be a normal str, not a MyString. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
"Kent Johnson" wrote You could also make this a method of a subclass of string if you prefer: class MyString(str): def beginswith(self, prefixes): for prefix in prefixes: if self.startswith(prefix): return prefix It's an appealing idea but the extra step of wrapping strings in MyString kind of takes the shine off of it. For example something like this is awkward: for d in os.listdir(): d = MyString(d) if d.beginswith(...): I would write that as: for d in os.listdir(): if MyString(d).beginswith(): Which isn't that cumbersome. d is still available for subsequent processing and acces to normal string methods is still useful , as in: for d in os.listdir(): if MyString(d).upper().beginswith(): But it's a matter of taste I guess. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld Some languages allow you to extend a built-in class, this technique works better there. Kent ___ 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] beginsWith multiple prefixes
On 12/25/08, Kent Johnson wrote: > > On Wed, Dec 24, 2008 at 4:04 PM, Emad Nawfal (عماد نوفل) > wrote: > > Hi Tutors, > > I want a function that acts like the startswith method, but can take > > multiple prefixes. As an amateur programmer, I came up with this one, and > it > > works fine, but my experience tells me that my solutions are not always > the > > best ones around. Can you please tell me what a better option might be: > > > Since Python 2.5, startswith() accepts a tuple as the match parameter, > so you can write > def beginsWith(word, listname): > return word.startswith(tuple(listname)) > > or just us startswith() directly. > > > Kent > Thank you Kent. I read the documentation, but did not notice it, now I can see it clearly. This also applies to endswith Thank you all for your helpfulness. -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
2008/12/25 Alan Gauld : > You could also make this a method of a subclass of string if you prefer: > > class MyString(str): > def beginswith(self, prefixes): > for prefix in prefixes: >if self.startswith(prefix): > return prefix > > Now you can create instances of MyString that will have all > the regular vstring methiods plus the new beginswith(): > > s = MyString("Welcome to my world") > if s.beginswith(["Welcome", "Hello","Howdy"]): > print "It's friendly" > > Which looks more consistent. It's an appealing idea but the extra step of wrapping strings in MyString kind of takes the shine off of it. For example something like this is awkward: for d in os.listdir(): d = MyString(d) if d.beginswith(...): Some languages allow you to extend a built-in class, this technique works better there. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
On Wed, Dec 24, 2008 at 4:04 PM, Emad Nawfal (عماد نوفل) wrote: > Hi Tutors, > I want a function that acts like the startswith method, but can take > multiple prefixes. As an amateur programmer, I came up with this one, and it > works fine, but my experience tells me that my solutions are not always the > best ones around. Can you please tell me what a better option might be: Since Python 2.5, startswith() accepts a tuple as the match parameter, so you can write def beginsWith(word, listname): return word.startswith(tuple(listname)) or just us startswith() directly. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
"Emad Nawfal (عماد نوفل)" wrote >> I want a function that acts like the startswith method, but can >> take >> multiple prefixes. Above does a lot more work than necessary. Try: def beginsWith(word, listname): for prefix in listname: if word.startswith(prefix): return True You could also make this a method of a subclass of string if you prefer: class MyString(str): def beginswith(self, prefixes): for prefix in prefixes: if self.startswith(prefix): return prefix Now you can create instances of MyString that will have all the regular vstring methiods plus the new beginswith(): s = MyString("Welcome to my world") if s.beginswith(["Welcome", "Hello","Howdy"]): print "It's friendly" Which looks more consistent. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
On 12/25/08, Bill Campbell wrote: > > On Wed, Dec 24, 2008, bob gailer wrote: > > Emad Nawfal ( ) wrote: > >> Hi Tutors, > >> I want a function that acts like the startswith method, but can take > >> multiple prefixes. As an amateur programmer, I came up with this one, > >> and it works fine, but my experience tells me that my solutions are > >> not always the best ones around. Can you please tell me what a better > >> option might be: > > ... > > > Above does a lot more work than necessary. Try: > > > > def beginsWith(word, listname): > >for prefix in listname: > > if word.startswith(prefix): > > return True > > > It might be more useful to return the prefix that matched as the > caller already knows what ``word'' is. > > Bill > -- > INTERNET: b...@celestial.com Bill Campbell; Celestial Software LLC > URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way > Voice: (206) 236-1676 Mercer Island, WA 98040-0820 > Fax:(206) 232-9186 > > Basic Definitions of Science: > If it's green or wiggles, it's biology. > If it stinks, it's chemistry. > If it doesn't work, it's physics. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > Thank you so much Bob and Bill. This is really much better than mine. Bill's suggestion is not applicable in my current script, but I believe I will need that soon. Thank you both. Thank you all tutors -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
On Wed, Dec 24, 2008, bob gailer wrote: > Emad Nawfal ( ) wrote: >> Hi Tutors, >> I want a function that acts like the startswith method, but can take >> multiple prefixes. As an amateur programmer, I came up with this one, >> and it works fine, but my experience tells me that my solutions are >> not always the best ones around. Can you please tell me what a better >> option might be: ... > Above does a lot more work than necessary. Try: > > def beginsWith(word, listname): >for prefix in listname: > if word.startswith(prefix): > return True It might be more useful to return the prefix that matched as the caller already knows what ``word'' is. Bill -- INTERNET: b...@celestial.com Bill Campbell; Celestial Software LLC URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way Voice: (206) 236-1676 Mercer Island, WA 98040-0820 Fax:(206) 232-9186 Basic Definitions of Science: If it's green or wiggles, it's biology. If it stinks, it's chemistry. If it doesn't work, it's physics. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] beginsWith multiple prefixes
Emad Nawfal (عماد نوفل) wrote: Hi Tutors, I want a function that acts like the startswith method, but can take multiple prefixes. As an amateur programmer, I came up with this one, and it works fine, but my experience tells me that my solutions are not always the best ones around. Can you please tell me what a better option might be: def beginsWith(word, listname): count = 0 x = range(len(word)) for i in x: if word[:i] in listname: count+=1 break if count > 0: return True else: return False Above does a lot more work than necessary. Try: def beginsWith(word, listname): for prefix in listname: if word.startswith(prefix): return True # main text = "ana mary fify floor school security".split() prefixes = ["an", "ma", "fi", "sec"] for word in text: if beginsWith(word, prefixes): print(word+" (Match)") else: print(word) #This produces the following: IDLE 3.0 No Subprocess >>> ana (Match) mary (Match) fify (Match) floor school security (Match) >>> -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] beginsWith multiple prefixes
Hi Tutors, I want a function that acts like the startswith method, but can take multiple prefixes. As an amateur programmer, I came up with this one, and it works fine, but my experience tells me that my solutions are not always the best ones around. Can you please tell me what a better option might be: def beginsWith(word, listname): count = 0 x = range(len(word)) for i in x: if word[:i] in listname: count+=1 break if count > 0: return True else: return False # main text = "ana mary fify floor school security".split() prefixes = ["an", "ma", "fi", "sec"] for word in text: if beginsWith(word, prefixes): print(word+" (Match)") else: print(word) #This produces the following: IDLE 3.0 No Subprocess >>> ana (Match) mary (Match) fify (Match) floor school security (Match) >>> -- لا أعرف مظلوما تواطأ الناس علي هضمه ولا زهدوا في إنصافه كالحقيقة.محمد الغزالي "No victim has ever been more repressed and alienated than the truth" Emad Soliman Nawfal Indiana University, Bloomington http://emnawfal.googlepages.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor