Re: Pre-PEP: Dictionary accumulator methods

2005-04-01 Thread Steven Bethard
Greg Ewing wrote: Steven Bethard wrote: py def defaultdict(*args, **kwargs): ... defaultfactory, args = args[0], args[1:] which can be written more succinctly as def defaultdict(defaultfactory, *args, **kwargs): ... Not if you want to allow the defaultfactory to be called with a keyword

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-31 Thread Greg Ewing
Steven Bethard wrote: I'd argue that for the same reasons that dict.fromkeys is a dict classmethod, the itertools methods could be iter classmethods (or staticmethods). The basic idea being that it's nice to place the methods associated with a type in that type's definiton. The parallel's a

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-31 Thread Greg Ewing
Ville Vainio wrote: The issue that really bothers me here is bloating the builtin space. We already have an uncomfortable amount of builtin functions. Maybe what we're really after here is the notion of a builtin module that's pre-imported into the builtin namespace. -- Greg Ewing, Computer

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-30 Thread Steven Bethard
Ville Vainio wrote: The issue that really bothers me here is bloating the builtin space. We already have an uncomfortable amount of builtin functions. Of course the additions that have been suggested would not pollute the builtin namespace, but they would still be there, taking space. I'd rather

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-30 Thread Steven Bethard
Ville Vainio wrote: A minimal set would not be that offensive, yes. But then we would have two places to look for itertools functionality, which may not be desirable. True, though this is currently necessary with str objects if you want to use, say string.maketrans, so it's not without some

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Raymond Hettinger
[Jack Diederich] itertools to iter transition, huh? I slipped that one in, I mentioned it to Raymond at PyCon and he didn't flinch. It would be nice not to have to sprinkle 'import itertools as it' in code. iter could also become a type wrapper instead of a function, so an iter

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Ville Vainio
Raymond == Raymond Hettinger [EMAIL PROTECTED] writes: Raymond If the experience works out, then all you're left with is Raymond the trivial matter of convincing Guido that function Raymond attributes are a sure cure for the burden of typing Raymond import statements. For one

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Steven Bethard
Ville Vainio wrote: Raymond == Raymond Hettinger [EMAIL PROTECTED] writes: Raymond If the experience works out, then all you're left with is Raymond the trivial matter of convincing Guido that function Raymond attributes are a sure cure for the burden of typing Raymond import

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Michael Spencer
Steven Bethard wrote: Ville Vainio wrote: Raymond == Raymond Hettinger [EMAIL PROTECTED] writes: Raymond If the experience works out, then all you're left with is Raymond the trivial matter of convincing Guido that function Raymond attributes are a sure cure for the burden of typing

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread George Sakkis
Steven Bethard [EMAIL PROTECTED] wrote: [snip] I guess the real questions are[1]: * How much does iter feel like a type? * How closely are the itertools functions associated with iter? STeVe [1] There's also the question of how much you believe in OO tenets like functions closely

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Jack Diederich
On Tue, Mar 29, 2005 at 12:38:42PM +0300, Ville Vainio wrote: Raymond == Raymond Hettinger [EMAIL PROTECTED] writes: Raymond If the experience works out, then all you're left with is Raymond the trivial matter of convincing Guido that function Raymond attributes are a sure cure

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Bengt Richter
On Tue, 29 Mar 2005 11:32:33 -0800, Michael Spencer [EMAIL PROTECTED] wrote: [...] While we're on the topic, what do you think of having unary, non-summary builtins automatically map themselves when called with an iterable that would otherwise be an illegal argument: That last otherwise is

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-29 Thread Steven Bethard
Terry Reedy wrote: But if classmethods are intended to provide alternate constructors But I do not remember that being given as a reason for classmethod(). But I am not sure what was. Well I haven't searched thoroughly, but I know one place that it's referenced is in descrintro[1]: Factoid:

Re: Pre-PEP: Dictionary accumulator methods

2005-03-28 Thread Jack Diederich
On Sun, Mar 27, 2005 at 02:20:33PM -0700, Steven Bethard wrote: Michele Simionato wrote: I am surprised nobody suggested we put those two methods into a separate module (say dictutils or even UserDict) as functions: from dictutils import tally, listappend tally(mydict, key)

itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-28 Thread Steven Bethard
Jack Diederich wrote: itertools to iter transition, huh? I slipped that one in, I mentioned it to Raymond at PyCon and he didn't flinch. It would be nice not to have to sprinkle 'import itertools as it' in code. iter could also become a type wrapper instead of a function, so an iter

Re: itertools to iter transition (WAS: Pre-PEP: Dictionary accumulator methods)

2005-03-28 Thread Jack Diederich
On Mon, Mar 28, 2005 at 10:28:29AM -0700, Steven Bethard wrote: Jack Diederich wrote: itertools to iter transition, huh? I slipped that one in, I mentioned it to Raymond at PyCon and he didn't flinch. It would be nice not to have to sprinkle 'import itertools as it' in code. iter could

Re: Pre-PEP: Dictionary accumulator methods

2005-03-28 Thread Michael Spencer
Jack Diederich wrote: On Sun, Mar 27, 2005 at 02:20:33PM -0700, Steven Bethard wrote: Michele Simionato wrote: I am surprised nobody suggested we put those two methods into a separate module (say dictutils or even UserDict) as functions: from dictutils import tally, listappend tally(mydict, key)

Re: Pre-PEP: Dictionary accumulator methods

2005-03-27 Thread Steven Bethard
Michele Simionato wrote: I am surprised nobody suggested we put those two methods into a separate module (say dictutils or even UserDict) as functions: from dictutils import tally, listappend tally(mydict, key) listappend(mydict, key, value) Sorry to join the discussion so late (I've been away

Re: Pre-PEP: Dictionary accumulator methods

2005-03-27 Thread Steven Bethard
Michele Simionato wrote: FWIW, here is my take on the defaultdict approach: def defaultdict(defaultfactory, dictclass=dict): class defdict(dictclass): def __getitem__(self, key): try: return super(defdict, self).__getitem__(key) except KeyError:

Re: Pre-PEP: Dictionary accumulator methods

2005-03-22 Thread Steve Holden
Greg Ewing wrote: Michele Simionato wrote: def defaultdict(defaultfactory, dictclass=dict): class defdict(dictclass): def __getitem__(self, key): try: return super(defdict, self).__getitem__(key) except KeyError: return

Re: Pre-PEP: Dictionary accumulator methods

2005-03-22 Thread bearophileHUGS
R.H.: The setdefault() method would continue to exist but would likely not make it into Py3.0. I agee to remove the setdefault. I like the new count method, but I don't like the appendlist method, because I think it's too much specilized. I too use sets a lot; recently I've suggested to add a

Re: Pre-PEP: Dictionary accumulator methods

2005-03-22 Thread TZOTZIOY
On Sat, 19 Mar 2005 01:24:57 GMT, rumours say that Raymond Hettinger [EMAIL PROTECTED] might have written: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError:

Re: Pre-PEP: Dictionary accumulator methods

2005-03-21 Thread haraldarminmassa
Raymond, I am +1 for both suggestions, tally and appendlist. Extended: Also, in all of my code base, I've not run across a single opportunity to use something like unionset(). This is surprising because I'm the set() author and frequently use set based algorithms.Your example was a

Re: Pre-PEP: Dictionary accumulator methods

2005-03-21 Thread AndrewN
Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError: self[key] = qty def appendlist(self, key, *values):

Re: Pre-PEP: Dictionary accumulator methods

2005-03-21 Thread Michele Simionato
FWIW, here is my take on the defaultdict approach: def defaultdict(defaultfactory, dictclass=dict): class defdict(dictclass): def __getitem__(self, key): try: return super(defdict, self).__getitem__(key) except KeyError: return

Re: Pre-PEP: Dictionary accumulator methods

2005-03-21 Thread George Sakkis
Michele Simionato [EMAIL PROTECTED] wrote: FWIW, here is my take on the defaultdict approach: def defaultdict(defaultfactory, dictclass=dict): class defdict(dictclass): def __getitem__(self, key): try: return super(defdict, self).__getitem__(key)

Re: Pre-PEP: Dictionary accumulator methods

2005-03-21 Thread Evan Simpson
Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): def appendlist(self, key, *values): -1.0 When I need these, I just use subtype recipes. They seem way too special-purpose for the base dict type. class

Re: Pre-PEP: Dictionary accumulator methods

2005-03-21 Thread Greg Ewing
Michele Simionato wrote: def defaultdict(defaultfactory, dictclass=dict): class defdict(dictclass): def __getitem__(self, key): try: return super(defdict, self).__getitem__(key) except KeyError: return self.setdefault(key,

Re: Pre-PEP: Dictionary accumulator methods

2005-03-21 Thread Roose
I agree -- I find myself NEEDING sets more and more. I use them with this idiom quite often. Once they become more widely available (i.e. Python 2.3 is installed everywhere), I will use them almost as much as lists I bet. So any solution IMO needs to at least encompass sets. But preferably

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Mike Rovner
Paul Rubin wrote: If the compiler can do some type inference, it can optimize out those multiple calls pretty straightforwardly. It can be tipped like that: di = dict(int) di.setdefault(0) di[key] += 1 dl = dict(list) dl.setdefault([]) dl.append(word) dl.extend(mylist) But the point is that if

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Paul Rubin
Mike Rovner [EMAIL PROTECTED] writes: It can be tipped like that: di = dict(int) di.setdefault(0) di[key] += 1 ... But the point is that if method not found in dict it delegated to container type specified in constructor. It solves dict specialization without bloating dict class and is

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Duncan Booth
Raymond Hettinger wrote: The rationale is to replace the awkward and slow existing idioms for dictionary based accumulation: d[key] = d.get(key, 0) + qty d.setdefault(key, []).extend(values) How about the alternative approach of allowing the user to override the action to be

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Matteo Dell'Amico
Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError: self[key] = qty def appendlist(self, key, *values):

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Reinhold Birkenfeld
Mike Rovner wrote: Paul Rubin wrote: If the compiler can do some type inference, it can optimize out those multiple calls pretty straightforwardly. It can be tipped like that: di = dict(int) di.setdefault(0) di[key] += 1 Interesting, but why do you need to give the int type to the

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Reinhold Birkenfeld
John Machin wrote: Reinhold Birkenfeld wrote: John Machin wrote: Are you kidding? If you know what set and default means, you will be able to guess what setdefault means. Same goes for updateBy. No I'm not kidding -- people from some cultures have no difficulty at all in mentally

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Reinhold Birkenfeld
George Sakkis wrote: -1 form me. I'm not very glad with both of them ( not a naming issue ) because i think that the dict type should offer only methods that apply to each dict whatever it contains. count() specializes to dict values that are addable and appendlist to those that are

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Roose
How about the alternative approach of allowing the user to override the action to be taken when accessing a non-existent key? d.defaultValue(0) I like this a lot. It makes it more clear from the code what is going on, rather than having to figure out what the name appendlist, count,

Re: Pre-PEP: Dictionary accumulator methods - typing initialising

2005-03-20 Thread Kay Schluehr
Duncan Booth wrote: Raymond Hettinger wrote: The rationale is to replace the awkward and slow existing idioms for dictionary based accumulation: d[key] = d.get(key, 0) + qty d.setdefault(key, []).extend(values) How about the alternative approach of allowing the user to

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Max
Paul Rubin wrote: Reinhold Birkenfeld [EMAIL PROTECTED] writes: Any takers for tally()? Well, as a non-native speaker, I had to look up this one in my dictionary. That said, it may be bad luck on my side, but it may be that this word is relatively uncommon and there are many others who would be

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Peter Hansen
Max wrote: Has anyone _not_ heard Jeff Probst say, I'll go tally the votes?! :) Who is Jeff Probst? -- http://mail.python.org/mailman/listinfo/python-list

Re: Pre-PEP: Dictionary accumulator methods - typing initialising

2005-03-20 Thread Matteo Dell'Amico
Kay Schluehr wrote: Why do You set d.defaultValue(0) d.defaultValue(function=list) but not d.defaultValue(0) d.defaultValue([]) ? I think that's because you have to instantiate a different object for each different key. Otherwise, you would instantiate just one list as a default value for *all*

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Magnus Lie Hetland
In article [EMAIL PROTECTED], Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError: self[key] = qty Yes, yes, YES!

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread BJörn Lindqvist
I like count() and appendlist() or whatever they will be named. But I have one question/idea: Why does the methods have to be put in dict? Can't their be a subtype of dict that includes those two methods? I.e.: .histogram = counting_dict() .for ch in text: .histogram.count(ch) Then maybe

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Duncan Booth
Roose wrote: I think someone mentioned that it might be a problem to add another piece of state to all dicts though. I don't know enough about the internals to say anything about this. setdefault gets around this by having you pass in the value every time, so it doesn't have to store it.

Re: Pre-PEP: Dictionary accumulator methods - typing initialising

2005-03-20 Thread Kay Schluehr
Matteo Dell'Amico wrote: Kay Schluehr wrote: Why do You set d.defaultValue(0) d.defaultValue(function=list) but not d.defaultValue(0) d.defaultValue([]) ? I think that's because you have to instantiate a different object for each different key. Otherwise, you would

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Colin J. Williams
Paul Rubin wrote: El Pitonero [EMAIL PROTECTED] writes: What about no name at all for the scalar case: a['hello'] += 1 a['bye'] -= 2 I like this despite the minor surprise that it works even when a['hello'] is uninitialized. +1 and if the value is a list: a['hello']= [1, 2, 3] a['hello']+= [4]

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Roose
Another option with no storage overhead which goes part way to reducing the awkwardness would be to provide a decorator class accessible through dict. The decorator class would take a value or function to be used as the default, but apart from __getitem__ would simply forward all other

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Jeremy Bowers
On Sat, 19 Mar 2005 20:07:40 -0800, Kay Schluehr wrote: It is bad OO design, George. I want to be a bit more become more specific on this and provide an example: Having thought about this for a bit, I agree it is a powerful counter-argument and in many other languages I'd consider this a total

Re: Pre-PEP: Dictionary accumulator methods - typing initialising

2005-03-20 Thread Matteo Dell'Amico
Kay Schluehr wrote: I think that's because you have to instantiate a different object for each different key. Otherwise, you would instantiate just one list as a default value for *all* default values. Or the default value will be copied, which is not very hard either or type(self._default)() will

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Alexander Schmolck
Beni Cherniavsky [EMAIL PROTECTED] writes: The relatively recent improvement of the dict constructor signature (``dict(foo=bar,...)``) obviously makes it impossible to just extend the constructor to ``dict(default=...)`` (or anything else for that matter) which would seem much less ad hoc.

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Francis Girard
Hi, I really do not like it. So -1 for me. Your two methods are very specialized whereas the dict type is very generic. Usually, when I see something like this in code, I can smell it's a patch to overcome some shortcomings on a previous design, thereby making the economy of re-designing.

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Mike Rovner
Reinhold Birkenfeld wrote: I don't quite understand that. Which dict item are you extending? Don't you need something like dl[key].append(word) Rigth. It was just a typo on my part. Thanks for fixing. Mike -- http://mail.python.org/mailman/listinfo/python-list

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Aahz
In article [EMAIL PROTECTED], Michele Simionato [EMAIL PROTECTED] wrote: I am surprised nobody suggested we put those two methods into a separate module (say dictutils or even UserDict) as functions: from dictutils import tally, listappend tally(mydict, key) listappend(mydict, key, value) That

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread David Eppstein
In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Aahz) wrote: I am surprised nobody suggested we put those two methods into a separate module (say dictutils or even UserDict) as functions: from dictutils import tally, listappend tally(mydict, key) listappend(mydict, key, value) That

Re: Pre-PEP: Dictionary accumulator methods

2005-03-20 Thread Ron
On Sun, 20 Mar 2005 15:14:22 -0800, David Eppstein [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], [EMAIL PROTECTED] (Aahz) wrote: I am surprised nobody suggested we put those two methods into a separate module (say dictutils or even UserDict) as functions: from dictutils import

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Michele Simionato
Raymond Hettinger: Any takers for tally()? Dunno, to me tally reads counts the numbers of votes for a candidate in an election. We should avoid abbreviations like inc() or incr() that different people tend to abbreviate differently (for example, that is why the new partial() function has its

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Paul Rubin
Reinhold Birkenfeld [EMAIL PROTECTED] writes: Any takers for tally()? Well, as a non-native speaker, I had to look up this one in my dictionary. That said, it may be bad luck on my side, but it may be that this word is relatively uncommon and there are many others who would be happier with

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
[Michele Simionato] Dunno, to me tally reads counts the numbers of votes for a candidate in an election. That isn't a pleasant image ;-) The only right name would be get_and_possibly_set but it is a bit long to type. Even if a wording is found that better describes the both the get and

any() and all() Was: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
Py2.5 is already going to include any() and all() as builtins. The signature does not include a function, identity or otherwise. Instead, the caller can write a listcomp or genexp that evaluates to True or False: any(x = 42 for x in data) [Roose] Oh great, I just saw that. . .

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Roose
Py2.5 is already going to include any() and all() as builtins. The signature does not include a function, identity or otherwise. Instead, the caller can write a listcomp or genexp that evaluates to True or False: Actually I was just looking at Python 2.5 docs since you mentioned this.

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Peter Otten
Roose wrote: Not to belabor the point, but in the example on that page, max(L, key=len) could be written max(len(x) for x in L). No, it can't: Python 2.5a0 (#2, Mar 5 2005, 17:44:37) [GCC 3.3.3 (SuSE Linux)] on linux2 Type help, copyright, credits or license for more information. max([a,

any() and all() Was: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
[Roose] Actually I was just looking at Python 2.5 docs since you mentioned this. http://www.python.org/dev/doc/devel/whatsnew/node3.html It says min() and max() will gain a key function parameter, and sort() gained one in Python 2.4 (news to me). It also appears in itertools.groupby() and,

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread El Pitonero
On Sat, 19 Mar 2005 01:24:57 GMT, Raymond Hettinger [EMAIL PROTECTED] wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError: self[key] = qty

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Kent Johnson
Bengt Richter wrote: On Sat, 19 Mar 2005 01:24:57 GMT, Raymond Hettinger [EMAIL PROTECTED] wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError:

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Carl Banks
Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError: self[key] = qty def appendlist(self, key,

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Kent Johnson
Brian van den Broek wrote: Raymond Hettinger said unto the world upon 2005-03-18 20:24: I would like to get everyone's thoughts on two new dictionary methods: def appendlist(self, key, *values): try: self[key].extend(values) except KeyError:

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Dan Sommers
On Sat, 19 Mar 2005 01:24:57 GMT, Raymond Hettinger [EMAIL PROTECTED] wrote: The proposed names could possibly be improved (perhaps tally() is more active and clear than count()). Curious that in this lengthy discussion, a method name of accumulate never came up. I'm not sure how to separate

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Pierre Barbier de Reuille
Ivan Van Laningham a écrit : Hi All-- Maybe I'm not getting it, but I'd think a better name for count would be add. As in d.add(key) d.add(key,-1) d.add(key,399) etc. [...] There is no existing add() method for dictionaries. Given the name change, I'd like to see it. Metta, Ivan I don't think

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Jeff Epler
[Jeff Epler] Maybe something for sets like 'appendlist' ('unionset'?) On Sat, Mar 19, 2005 at 04:18:43AM +, Raymond Hettinger wrote: I do not follow. Can you provide a pure python equivalent? Here's what I had in mind: $ python /tmp/unionset.py Set(['set', 'self', 'since', 's', 'sys',

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Ivan Van Laningham
Hi All-- Raymond Hettinger wrote: [Michele Simionato] +1 for inc instead of count. Any takers for tally()? Sure. Given the reasons for avoiding add(), tally()'s a much better choice than count(). What about d.tally(key,0) then? Deleting the key as was suggested by Michael Spencer

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Peter Hansen
Michele Simionato wrote: +1 for inc instead of count. -1 for inc, increment, or anything that carries a connotation of *increasing* the value, so long as the proposal allows for negative numbers to be involved. Incrementing by -1 is a pretty silly picture. +1 for add and, given the above, I'm

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Reinhold Birkenfeld
Peter Hansen wrote: Michele Simionato wrote: +1 for inc instead of count. -1 for inc, increment, or anything that carries a connotation of *increasing* the value, so long as the proposal allows for negative numbers to be involved. Incrementing by -1 is a pretty silly picture. +1 for

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Peter Hansen
Reinhold Birkenfeld wrote: Peter Hansen wrote: +1 for add and, given the above, I'm unsure there's a viable alternative (unless this is restricted to positive values, or perhaps even to +1 specifically). What about `addto()'? add() just has the connotation of adding something to the dict and not

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
[Jeff Epler] Maybe something for sets like 'appendlist' ('unionset'?) While this could work and potentially be useful, I think it is better to keep the proposal focused on the two common use cases. Adding a third would reduce the chance of acceptance. Also, in all of my code base, I've not run

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
[Dan Sommers] Curious that in this lengthy discussion, a method name of accumulate never came up. I'm not sure how to separate the two cases (accumulating scalars vs. accumulating a list), though. Separating the two cases is essential. Also, the wording should contain strong cues that remind

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Denis S. Otkidach
On 18 Mar 2005 21:03:52 -0800 Michele Simionato wrote: MS +1 for inc instead of count. MS appendlist seems a bit too specific (I do not use dictionaries of MS lists that often). inc is too specific too. MS The problem with setdefault is the name, not the functionality. The problem with

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Ivan Van Laningham
Hi All-- Raymond Hettinger wrote: Separating the two cases is essential. Also, the wording should contain strong cues that remind you of addition and of building a list. For the first, how about addup(): d = {} for word in text.split(): d.addup(word) I still

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread El Pitonero
Dan Sommers wrote: On Sat, 19 Mar 2005 01:24:57 GMT, Raymond Hettinger [EMAIL PROTECTED] wrote: The proposed names could possibly be improved (perhaps tally() is more active and clear than count()). Curious that in this lengthy discussion, a method name of accumulate never came up. I'm

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
[Ivan Van Laningham] What about adding another method, setincrement()? . . . Not that there's any real utility in that. That was a short lived suggestion ;-) Also, it would entail storing an extra value in the dictionary header. That alone would be a killer. Raymond --

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Paul McGuire
-1 on set increment. I think this makes your intent much clearer: .d={} .for word in text.split(): .d.tally(word) .if word.lower() in [a,an,the]: .d.tally(word,-1) or perhaps simplest: .d={} .for word in text.split(): .if word.lower() not in [a,an,the]: .

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Aahz
In article [EMAIL PROTECTED], Raymond Hettinger [EMAIL PROTECTED] wrote: The proposed names could possibly be improved (perhaps tally() is more active and clear than count()). +1 tally() -- Aahz ([EMAIL PROTECTED]) * http://www.pythoncraft.com/ The joy of coding Python should

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread El Pitonero
Raymond Hettinger wrote: Separating the two cases is essential. Also, the wording should contain strong cues that remind you of addition and of building a list. For the first, how about addup(): d = {} for word in text.split(): d.addup(word) import copy class

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Aahz
In article [EMAIL PROTECTED], Raymond Hettinger [EMAIL PROTECTED] wrote: How about countkey() or tabulate()? Those rank roughly equal to tally() for me, with a slight edge to these two for clarity and a slight edge to tally() for conciseness. -- Aahz ([EMAIL PROTECTED]) *

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
[El Pitonero] Is it even necessary to use a method name? import copy class safedict(dict): def __init__(self, default=None): self.default = default def __getitem__(self, key): try: return dict.__getitem__(self, key) except KeyError:

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Brian van den Broek
Kent Johnson said unto the world upon 2005-03-19 07:19: Brian van den Broek wrote: Raymond Hettinger said unto the world upon 2005-03-18 20:24: I would like to get everyone's thoughts on two new dictionary methods: def appendlist(self, key, *values): try:

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread George Sakkis
Raymond Hettinger [EMAIL PROTECTED] wrote: [Jeff Epler] Maybe something for sets like 'appendlist' ('unionset'?) While this could work and potentially be useful, I think it is better to keep the proposal focused on the two common use cases. Adding a third would reduce the chance of

Re: any() and all() Was: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Roose
Ah OK, I stand corrected. Whoops. I just read the web page and thought the wrong thing, that makes sense. Think about it. A key= function is quite a different thing. It provides a *temporary* comparison key while retaining the original value. IOW, your re-write is incorrect: L =

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Do Re Mi chel La Si Do
Hi if key not in d: d[key] = {subkey:value} else: d[key][subkey] = value and d[(key,subkey)] = value ? Michel Claveau -- http://mail.python.org/mailman/listinfo/python-list

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread El Pitonero
Raymond Hettinger wrote: As written out above, the += syntax works fine but does not work with append(). ... BTW, there is no need to make the same post three times. The append() syntax works, if you use the other definition of safedict (*). There are more than one way of defining safedict,

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread George Sakkis
Aahz [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], Raymond Hettinger [EMAIL PROTECTED] wrote: The proposed names could possibly be improved (perhaps tally() is more active and clear than count()). +1 tally() -1 for count(): Implies an accessor, not a mutator. -1 for tally():

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread El Pitonero
George Sakkis wrote: Aahz [EMAIL PROTECTED] wrote: In article [EMAIL PROTECTED], Raymond Hettinger [EMAIL PROTECTED] wrote: The proposed names could possibly be improved (perhaps tally() is more active and clear than count()). +1 tally() -1 for count(): Implies an accessor, not a

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Dan Sommers
On Sat, 19 Mar 2005 15:17:59 GMT, Raymond Hettinger [EMAIL PROTECTED] wrote: [Dan Sommers] Curious that in this lengthy discussion, a method name of accumulate never came up. I'm not sure how to separate the two cases (accumulating scalars vs. accumulating a list), though. Separating the

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Kay Schluehr
Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self[key] += qty except KeyError: self[key] = qty def appendlist(self, key, *values):

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Paul Rubin
El Pitonero [EMAIL PROTECTED] writes: What about no name at all for the scalar case: a['hello'] += 1 a['bye'] -= 2 I like this despite the minor surprise that it works even when a['hello'] is uninitialized. -- http://mail.python.org/mailman/listinfo/python-list

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Bengt Richter
On Sat, 19 Mar 2005 07:13:15 -0500, Kent Johnson [EMAIL PROTECTED] wrote: Bengt Richter wrote: On Sat, 19 Mar 2005 01:24:57 GMT, Raymond Hettinger [EMAIL PROTECTED] wrote: I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1):

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread John Machin
Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: +1 for each. PROBLEMS BEING SOLVED - The readability issues with the existing constructs are: * They are awkward to teach, create, read, and review. * Their wording tends

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Reinhold Birkenfeld
John Machin wrote: Raymond Hettinger wrote: I would like to get everyone's thoughts on two new dictionary methods: +1 for each. PROBLEMS BEING SOLVED - The readability issues with the existing constructs are: * They are awkward to teach, create, read, and review.

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Paul Rubin
Raymond Hettinger [EMAIL PROTECTED] writes: I find the disassembly (presented in the first post) to be telling. The compiler has done a great job and there is no fluff -- all of those steps have been specified by the programmer and he/she must at some level be aware of every one of them

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread John Machin
Reinhold Birkenfeld wrote: John Machin wrote: Are you kidding? If you know what set and default means, you will be able to guess what setdefault means. Same goes for updateBy. No I'm not kidding -- people from some cultures have no difficulty at all in mentally splitting up words like

Re: Pre-PEP: Dictionary accumulator methods

2005-03-19 Thread Raymond Hettinger
[Bengt Richter] IMO Raymond's Zen concerns are the ones to think about first, and then efficiency, which was one of the motivators in the first place ;-) Well said. I find the disassembly (presented in the first post) to be telling. The compiler has done a great job and there is no fluff --

  1   2   >