Re: How can I create customized classes that have similar properties as 'str'?

2007-12-06 Thread samwyse
On Nov 24, 6:38 pm, Hrvoje Niksic [EMAIL PROTECTED] wrote: samwyse [EMAIL PROTECTED] writes: create a hash that maps your keys to themselves, then use the values of that hash as your keys. The atom function you describe already exists under the name intern. D'oh! That's what I get for

Re: How can I create customized classes that have similar properties as 'str'?

2007-12-03 Thread zooko
On Nov 24, 4:44 am, Licheng Fang [EMAIL PROTECTED] wrote: Yes, millions. In my natural language processing tasks, I almost always need to define patterns, identify their occurrences in a huge data, and count them. Say, I have a big text file, consisting of millions of words, and I want to

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-27 Thread Licheng Fang
On Nov 27, 10:45 am, Steven D'Aprano [EMAIL PROTECTED] wrote: On Sun, 25 Nov 2007 02:42:36 -0800, Licheng Fang wrote: I mentioned trigram counting as an illustrative case. In fact, you'll often need to define patterns more complex than that, and tens of megabytes of text may generate

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-27 Thread Chris Mellon
On Nov 27, 2007 7:16 AM, Licheng Fang [EMAIL PROTECTED] wrote: On Nov 27, 10:45 am, Steven D'Aprano [EMAIL PROTECTED] wrote: On Sun, 25 Nov 2007 02:42:36 -0800, Licheng Fang wrote: I mentioned trigram counting as an illustrative case. In fact, you'll often need to define patterns more

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-26 Thread Steven D'Aprano
On Sun, 25 Nov 2007 02:42:36 -0800, Licheng Fang wrote: I mentioned trigram counting as an illustrative case. In fact, you'll often need to define patterns more complex than that, and tens of megabytes of text may generate millions of them, and I've observed they quickly ate up the 8G memory

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-25 Thread Peter Otten
Steven D'Aprano wrote: store = {} def atom(str): global store if str not in store: store[str] = str return store[str] Oh lordy, that's really made my day! That's the funniest piece of code I've seen for a long time! Worthy of being submitted to the DailyWTF.

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-25 Thread Licheng Fang
On Nov 25, 5:59 am, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: On Sat, 24 Nov 2007 03:44:59 -0800, Licheng Fang wrote: On Nov 24, 7:05 pm, Bjoern Schliessmann usenet- [EMAIL PROTECTED] wrote: Licheng Fang wrote: I find myself frequently in need of classes like this for

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-25 Thread Steven D'Aprano
On Sun, 25 Nov 2007 10:39:38 +0100, Peter Otten wrote: So if you are going to submit Sam's function make sure to bundle it with this little demo... Well Peter, I was going to reply with a comment about not changing the problem domain (tuples of ints to trigrams from a text file for natural

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-25 Thread MonkeeSage
On Nov 24, 6:42 pm, Steven D'Aprano [EMAIL PROTECTED] cybersource.com.au wrote: This has nothing, absolutely NOTHING, to do with memoization. Memoization trades off memory for time, allowing slow functions to return results faster at the cost of using more memory. The OP wants to save memory,

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-25 Thread Colin J. Williams
Steven D'Aprano wrote: On Sun, 25 Nov 2007 01:38:51 +0100, Hrvoje Niksic wrote: samwyse [EMAIL PROTECTED] writes: create a hash that maps your keys to themselves, then use the values of that hash as your keys. The atom function you describe already exists under the name intern. Not

How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Licheng Fang
I mean, all the class instances that equal to each other should be reduced into only one instance, which means for instances of this class there's no difference between a is b and a==b. Thank you. -- http://mail.python.org/mailman/listinfo/python-list

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Licheng Fang
I find myself frequently in need of classes like this for two reasons. First, it's efficient in memory. Second, when two instances are compared for equality only their pointers are compared. (I think that's how Python compares 'str's. On Nov 24, 6:31 pm, Licheng Fang [EMAIL PROTECTED] wrote: I

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bjoern Schliessmann
Licheng Fang wrote: I mean, all the class instances that equal to each other should be reduced into only one instance, which means for instances of this class there's no difference between a is b and a==b. If you only want that if a == b is True also a is b is True, overload the is_ attribute

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bjoern Schliessmann
Licheng Fang wrote: I find myself frequently in need of classes like this for two reasons. First, it's efficient in memory. Are you using millions of objects, or MB size objects? Otherwise, this is no argument. BTW, what happens if you, by some operation, make a == b, and afterwards change b

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Licheng Fang
On Nov 24, 7:05 pm, Bjoern Schliessmann usenet- [EMAIL PROTECTED] wrote: Licheng Fang wrote: I find myself frequently in need of classes like this for two reasons. First, it's efficient in memory. Are you using millions of objects, or MB size objects? Otherwise, this is no argument. Yes,

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bjoern Schliessmann
Licheng Fang wrote: On Nov 24, 7:05 pm, Bjoern Schliessmann usenet- BTW, what happens if you, by some operation, make a == b, and afterwards change b so another object instance must be created? This instance management is quite a runtime overhead. I probably need this class to be

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread samwyse
On Nov 24, 5:44 am, Licheng Fang [EMAIL PROTECTED] wrote: Yes, millions. In my natural language processing tasks, I almost always need to define patterns, identify their occurrences in a huge data, and count them. [...] So I end up with unnecessary duplicates of keys. And this can be a great

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Marc 'BlackJack' Rintsch
On Sat, 24 Nov 2007 13:40:40 +0100, Bjoern Schliessmann wrote: Licheng Fang wrote: On Nov 24, 7:05 pm, Bjoern Schliessmann usenet- Wow, I didn't know this. But exactly how Python manage these strings? I don't know (use the source, Luke). :) Or perhaps there is a Python Elder here that

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Licheng Fang
On Nov 24, 9:42 pm, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Sat, 24 Nov 2007 13:40:40 +0100, Bjoern Schliessmann wrote: Licheng Fang wrote: On Nov 24, 7:05 pm, Bjoern Schliessmann usenet- Wow, I didn't know this. But exactly how Python manage these strings? I don't know

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bruno Desthuilliers
Licheng Fang a écrit : I mean, all the class instances that equal to each other should be reduced into only one instance, which means for instances of this class there's no difference between a is b and a==b. Here's a QD attempt - without any garantee, and to be taylored to your needs.

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread samwyse
On Nov 24, 10:35 am, Licheng Fang [EMAIL PROTECTED] wrote: Thanks. Then, is there a way to make python treat all strings this way, or any other kind of immutable objects? The word generally used is 'atom' when referring to strings that are set up such that 'a == b' implies 'a is b'. This is

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread samwyse
On Nov 24, 5:44 am, Licheng Fang [EMAIL PROTECTED] wrote: Yes, millions. In my natural language processing tasks, I almost always need to define patterns, identify their occurrences in a huge data, and count them. Say, I have a big text file, consisting of millions of words, and I want to

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sat, 24 Nov 2007 03:44:59 -0800, Licheng Fang wrote: On Nov 24, 7:05 pm, Bjoern Schliessmann usenet- [EMAIL PROTECTED] wrote: Licheng Fang wrote: I find myself frequently in need of classes like this for two reasons. First, it's efficient in memory. Are you using millions of objects,

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sat, 24 Nov 2007 04:54:43 -0800, samwyse wrote: create a hash that maps your keys to themselves, then use the values of that hash as your keys. store = {} def atom(str): global store if str not in store: store[str] = str return store[str] Oh lordy,

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sat, 24 Nov 2007 12:00:25 +0100, Bjoern Schliessmann wrote: Licheng Fang wrote: I mean, all the class instances that equal to each other should be reduced into only one instance, which means for instances of this class there's no difference between a is b and a==b. If you only want that

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread George Sakkis
On Nov 24, 4:59 pm, Steven D'Aprano [EMAIL PROTECTED] wrote: On Sat, 24 Nov 2007 03:44:59 -0800, Licheng Fang wrote: On Nov 24, 7:05 pm, Bjoern Schliessmann usenet- [EMAIL PROTECTED] wrote: Licheng Fang wrote: I find myself frequently in need of classes like this for two reasons.

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Bjoern Schliessmann
Steven D'Aprano wrote: No advantage? That's for sure. There is no is_ attribute of generic classes, and even if there was, it would have no special meaning. Argl, I confused the operator module's attributes with objects ;) Regards, Björn -- BOFH excuse #378: Operators killed by year

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sat, 24 Nov 2007 14:58:50 -0800, George Sakkis wrote: On Nov 24, 4:59 pm, Steven D'Aprano [EMAIL PROTECTED] wrote: On Sat, 24 Nov 2007 03:44:59 -0800, Licheng Fang wrote: On Nov 24, 7:05 pm, Bjoern Schliessmann usenet- [EMAIL PROTECTED] wrote: Licheng Fang wrote: I find myself

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Hrvoje Niksic
samwyse [EMAIL PROTECTED] writes: create a hash that maps your keys to themselves, then use the values of that hash as your keys. The atom function you describe already exists under the name intern. -- http://mail.python.org/mailman/listinfo/python-list

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Steven D'Aprano
On Sun, 25 Nov 2007 01:38:51 +0100, Hrvoje Niksic wrote: samwyse [EMAIL PROTECTED] writes: create a hash that maps your keys to themselves, then use the values of that hash as your keys. The atom function you describe already exists under the name intern. Not really. intern() works very

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread Hrvoje Niksic
Steven D'Aprano [EMAIL PROTECTED] writes: On Sun, 25 Nov 2007 01:38:51 +0100, Hrvoje Niksic wrote: samwyse [EMAIL PROTECTED] writes: create a hash that maps your keys to themselves, then use the values of that hash as your keys. The atom function you describe already exists under the

Re: How can I create customized classes that have similar properties as 'str'?

2007-11-24 Thread George Sakkis
On Nov 24, 7:42 pm, Steven D'Aprano To the OP: yes, your use case is quite valid; the keyword you are looking for is memoize. You can find around a dozen of recipes in the Cookbook and posted in this list; here's one starting point: