Re: Using String Methods In Jump Tables

2010-08-25 Thread Bruno Desthuilliers
Tim Daneliuk a écrit : On 8/19/2010 7:23 PM, Steven D'Aprano wrote: On Thu, 19 Aug 2010 18:27:11 -0500, Tim Daneliuk wrote: Problem: Given tuples in the form (key, string), use 'key' to determine what string method to apply to the string: table = {'l': str.lower, 'u': str.upper} table['u

Re: Using String Methods In Jump Tables

2010-08-24 Thread John Pinner
On Aug 20, 12:27 am, Tim Daneliuk wrote: > Problem: > >   Given tuples in the form (key, string), use 'key' to determine >   what string method to apply to the string: > >     key           operation >     --- > >      l            lower() >      u            upper() >      t  

Re: Using String Methods In Jump Tables

2010-08-24 Thread Tim Daneliuk
On 8/23/2010 4:22 PM, Hrvoje Niksic wrote: > Tim Daneliuk writes: > >>You can get away with this because all string objects appear to point to >> common >>method objects. That is,: id("a".lower) == id("b".lower) > > A side note: your use of `id' has misled you. id(X)==id(Y) is not a >

Re: Using String Methods In Jump Tables

2010-08-23 Thread Hrvoje Niksic
Tim Daneliuk writes: >You can get away with this because all string objects appear to point to > common >method objects. That is,: id("a".lower) == id("b".lower) A side note: your use of `id' has misled you. id(X)==id(Y) is not a perfect substitue for the X is Y. :) "a".lower and "b

Re: Using String Methods In Jump Tables

2010-08-23 Thread Jon Clements
On 23 Aug, 16:57, Tim Daneliuk wrote: > On 8/23/2010 10:35 AM, Jon Clements wrote: > > > > > On 20 Aug, 01:51, Tim Daneliuk wrote: > >> On 8/19/2010 7:23 PM, Steven D'Aprano wrote: > > >>> On Thu, 19 Aug 2010 18:27:11 -0500, Tim Daneliuk wrote: > > Problem: > >   Given tuples in the for

Re: Using String Methods In Jump Tables

2010-08-23 Thread Terry Reedy
On 8/23/2010 11:57 AM, Tim Daneliuk wrote: On 8/23/2010 10:35 AM, Jon Clements wrote: Another more generic option would be to use methodcaller from the operator module. Could you say a bit more about just why you prefer this approach? Clearly, it *is* more generic, but in looking it over, i

Re: Using String Methods In Jump Tables

2010-08-23 Thread Tim Daneliuk
On 8/23/2010 10:35 AM, Jon Clements wrote: > On 20 Aug, 01:51, Tim Daneliuk wrote: >> On 8/19/2010 7:23 PM, Steven D'Aprano wrote: >> >>> On Thu, 19 Aug 2010 18:27:11 -0500, Tim Daneliuk wrote: >> Problem: >> Given tuples in the form (key, string), use 'key' to determine what st

Re: Using String Methods In Jump Tables

2010-08-23 Thread Jon Clements
On 20 Aug, 01:51, Tim Daneliuk wrote: > On 8/19/2010 7:23 PM, Steven D'Aprano wrote: > > > On Thu, 19 Aug 2010 18:27:11 -0500, Tim Daneliuk wrote: > > >> Problem: > > >>   Given tuples in the form (key, string), use 'key' to determine what > >>   string method to apply to the string: > > tabl

Re: Using String Methods In Jump Tables

2010-08-19 Thread Tim Daneliuk
On 8/19/2010 6:41 PM, Chris Rebert wrote: > >> >> How do you get a reference to a method found in one object instance, but >> actually apply it to another instance of the same class? I'm guessing >> this may >> involve fiddling with some of the internal __ variables, but I'm not >> qu

Re: Using String Methods In Jump Tables

2010-08-19 Thread Tim Daneliuk
On 8/19/2010 7:23 PM, Steven D'Aprano wrote: > On Thu, 19 Aug 2010 18:27:11 -0500, Tim Daneliuk wrote: > >> Problem: >> >> Given tuples in the form (key, string), use 'key' to determine what >> string method to apply to the string: > table = {'l': str.lower, 'u': str.upper} table['u

Re: Using String Methods In Jump Tables

2010-08-19 Thread Steven D'Aprano
On Thu, 19 Aug 2010 18:27:11 -0500, Tim Daneliuk wrote: > Problem: > > Given tuples in the form (key, string), use 'key' to determine what > string method to apply to the string: >>> table = {'l': str.lower, 'u': str.upper} >>> table['u']('hello world') 'HELLO WORLD' [...] > As I said, I k

Re: Using String Methods In Jump Tables

2010-08-19 Thread Chris Rebert
On Thu, Aug 19, 2010 at 4:27 PM, Tim Daneliuk wrote: > Problem: > >  Given tuples in the form (key, string), use 'key' to determine >  what string method to apply to the string: > >    key           operation >    --- > >     l            lower() >     u            upper() >  

Using String Methods In Jump Tables

2010-08-19 Thread Tim Daneliuk
Problem: Given tuples in the form (key, string), use 'key' to determine what string method to apply to the string: key operation --- llower() uupper() ttitle() ... Commentary: Easy, right? Wel

Re: string methods of a str subclass

2007-04-16 Thread Daniel Nogradi
> > Why is the strip( ) method returning something that is not a mystr > > instance? I would expect all methods operating on a string instance > > and returning another string instance to correctly operate on a mystr > > instance and return a mystr instance. > > Why would you expect that? > Would y

Re: string methods of a str subclass

2007-04-16 Thread 7stud
On Apr 16, 3:28 am, "Daniel Nogradi" <[EMAIL PROTECTED]> wrote: > I would expect all methods operating on a string instance > and returning another string instance Ok, then this: class A(object): def __init__(self, s): self.s = s def strip(self): return self.s class mystr

Re: string methods of a str subclass

2007-04-16 Thread Duncan Booth
"Daniel Nogradi" <[EMAIL PROTECTED]> wrote: > Why is the strip( ) method returning something that is not a mystr > instance? I would expect all methods operating on a string instance > and returning another string instance to correctly operate on a mystr > instance and return a mystr instance. Wh

Re: string methods of a str subclass

2007-04-16 Thread 7stud
On Apr 16, 3:28 am, "Daniel Nogradi" <[EMAIL PROTECTED]> wrote: > I am probably misunderstanding some basic issue here but this > behaviour is not what I would expect: > > Python 2.4 (#1, Mar 22 2005, 21:42:42) > [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2 > Type "help", "copyright", "

string methods of a str subclass

2007-04-16 Thread Daniel Nogradi
I am probably misunderstanding some basic issue here but this behaviour is not what I would expect: Python 2.4 (#1, Mar 22 2005, 21:42:42) [GCC 3.3.5 20050117 (prerelease) (SUSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> class mystr( str ): ...

Re: string methods

2005-07-31 Thread Martin v. Löwis
anthonyberet wrote: > For example if I wanted to replace the 4th character in 'foobar' (the > b)with the contents of another string, newchar, what would be the > easiest way? Depends on how your input is specified. If you know it is the b you want to replace, you write >>> text="foobar" >>> t

Re: string methods

2005-07-30 Thread Peter Hansen
Brian Beck wrote: > anthonyberet wrote: >>I know this touches on immutability etc, but I can't find string methods >>to return the first 3 characters, and then the last 2 characters, which >>I could concatenate with newchar to make a new string. > > As tiissa sa

Re: string methods

2005-07-30 Thread Brian Beck
anthonyberet wrote: > I know this touches on immutability etc, but I can't find string methods > to return the first 3 characters, and then the last 2 characters, which > I could concatenate with newchar to make a new string. As tiissa said, you want slicing: py> s = "foob

Re: string methods

2005-07-30 Thread tiissa
anthonyberet wrote: > I know this touches on immutability etc, but I can't find string methods > to return the first 3 characters, and then the last 2 characters, which > I could concatenate with newchar to make a new string. > > I know the string methods are there, but

string methods

2005-07-30 Thread anthonyberet
ents of another string, newchar, what would be the easiest way? I know this touches on immutability etc, but I can't find string methods to return the first 3 characters, and then the last 2 characters, which I could concatenate with newchar to make a new string. I know the string methods are t

Re: string methods (warning, newbie)

2005-02-28 Thread TZOTZIOY
On Sun, 27 Feb 2005 18:12:17 -0700, rumours say that Steven Bethard <[EMAIL PROTECTED]> might have written: [snip Nick Coghlan's list comprehension] [STeVe] >On the other hand, filter doesn't do the same thing: > >py> s = u'The Beatles - help - 03 - Ticket to ride' >py> filter(str.isalpha, s) >Tr

Re: string methods (warning, newbie)

2005-02-28 Thread TZOTZIOY
On Sun, 27 Feb 2005 18:12:17 -0700, rumours say that Steven Bethard <[EMAIL PROTECTED]> might have written: [snip Nick Coghlan's list comprehension] [STeVe] >On the other hand, filter doesn't do the same thing: > >py> s = u'The Beatles - help - 03 - Ticket to ride' >py> filter(str.isalpha, s) >Tr

Re: string methods (warning, newbie)

2005-02-27 Thread Steven Bethard
Nick Coghlan wrote: Jimmy Retzlaff wrote: The approach you are considering may be easier than you think: filter(str.isalpha, 'The Beatles - help - 03 - Ticket to ride') 'TheBeatleshelpTickettoride' Hmm, I think this is a case where filter is significantly clearer than the equivalent list comprehen

Re: string methods (warning, newbie)

2005-02-27 Thread anthonyberet
Jimmy Retzlaff wrote: Anthonyberet wrote: Is there a string mething to return only the alpha characters of a string? eg 'The Beatles - help - 03 - Ticket to ride', would be 'TheBeatlesTickettoride' If not then how best to approach this? I have some complicated plan to cut the string into individual

Re: string methods (warning, newbie)

2005-02-26 Thread Nick Coghlan
Jimmy Retzlaff wrote: The approach you are considering may be easier than you think: filter(str.isalpha, 'The Beatles - help - 03 - Ticket to ride') 'TheBeatleshelpTickettoride' Hmm, I think this is a case where filter is significantly clearer than the equivalent list comprehension: Py> "".join(

Re: string methods (warning, newbie)

2005-02-26 Thread Terry Reedy
"anthonyberet" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Is there a string mething to return only the alpha characters of a > string? > eg 'The Beatles - help - 03 - Ticket to ride', would be > 'TheBeatlesTickettoride' I believe you can do this with string.translate (string

RE: string methods (warning, newbie)

2005-02-26 Thread Jimmy Retzlaff
Anthonyberet wrote: > Is there a string mething to return only the alpha characters of a string? > eg 'The Beatles - help - 03 - Ticket to ride', would be > 'TheBeatlesTickettoride' > > If not then how best to approach this? > I have some complicated plan to cut the string into individual > charac

Re: string methods (warning, newbie)

2005-02-26 Thread Peter Hansen
anthonyberet wrote: Is there a string mething to return only the alpha characters of a string? eg 'The Beatles - help - 03 - Ticket to ride', would be 'TheBeatlesTickettoride' If not then how best to approach this? I have some complicated plan to cut the string into individual characters and the

Re: string methods (warning, newbie)

2005-02-26 Thread anthonyberet
anthonyberet wrote: Is there a string mething [method] to return only the alpha characters of a string? eg 'The Beatles - help - 03 - Ticket to ride', would be 'TheBeatlesTickettoride' erm, no it wouldn't, it would be 'TheBeatleshelpTickettoride', but you get me, I am sure. If not then how bes

string methods (warning, newbie)

2005-02-26 Thread anthonyberet
Is there a string mething to return only the alpha characters of a string? eg 'The Beatles - help - 03 - Ticket to ride', would be 'TheBeatlesTickettoride' If not then how best to approach this? I have some complicated plan to cut the string into individual characters and then concatenate a new