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):
try:
self[key].extend(values)
except KeyError:
self[key] = list(values)
The rationale is to replace the awkward and slow existing idioms for dictionary
based accumulation:
d[key] = d.get(key, 0) + qty
d.setdefault(ke y, []).extend(values )
In simplest form, those two statements would now be coded more readably as:
d.count(key)
d.appendlist(ke y, value)
In their multi-value forms, they would now be coded as:
d.count(key, qty)
d.appendlist(ke y, *values)
The error messages returned by the new methods are the same as those returned by
the existing idioms.
The get() method would continue to exist because it is useful for applications
other than accumulation.
The setdefault() method would continue to exist but would likely not make it
into Py3.0.
PROBLEMS BEING SOLVED
---------------------
The readability issues with the existing constructs are:
* They are awkward to teach, create, read, and review.
* Their wording tends to hide the real meaning (accumulation).
* The meaning of setdefault() 's method name is not self-evident.
The performance issues with the existing constructs are:
* They translate into many opcodes which slows them considerably.
* The get() idiom requires two dictionary lookups of the same key.
* The setdefault() idiom instantiates a new, empty list prior to every call.
* That new list is often not needed and is immediately discarded.
* The setdefault() idiom requires an attribute lookup for extend/append.
* The setdefault() idiom makes two function calls.
The latter issues are evident from a disassembly: dis(compile('d[key] = d.get(key, 0) + qty', '', 'exec'))
1 0 LOAD_NAME 0 (d)
3 LOAD_ATTR 1 (get)
6 LOAD_NAME 2 (key)
9 LOAD_CONST 0 (0)
12 CALL_FUNCTION 2
15 LOAD_NAME 3 (qty)
18 BINARY_ADD
19 LOAD_NAME 0 (d)
22 LOAD_NAME 2 (key)
25 STORE_SUBSCR
26 LOAD_CONST 1 (None)
29 RETURN_VALUE
dis(compile('d. setdefault(key, []).extend(values )', '', 'exec'))
1 0 LOAD_NAME 0 (d)
3 LOAD_ATTR 1 (setdefault)
6 LOAD_NAME 2 (key)
9 BUILD_LIST 0
12 CALL_FUNCTION 2
15 LOAD_ATTR 3 (extend)
18 LOAD_NAME 4 (values)
21 CALL_FUNCTION 1
24 POP_TOP
25 LOAD_CONST 0 (None)
28 RETURN_VALUE
In contrast, the proposed methods use only a single attribute lookup and
function call, they use only one dictionary lookup, they use very few opcodes,
and they directly access the accumulation functions, PyNumber_Add() or
PyList_Append() . IOW, the performance improvement matches the readability
improvement.
ISSUES
------
The proposed names could possibly be improved (perhaps tally() is more active
and clear than count()).
The appendlist() method is not as versatile as setdefault() which can be used
with other object types (perhaps for creating dictionaries of dictionaries).
However, most uses I've seen are with lists. For other uses, plain Python code
suffices in terms of speed, clarity, and avoiding unnecessary instantiation of
empty containers:
if key not in d:
d.key = {subkey:value}
else:
d[key][subkey] = value
Raymond Hettinger
Jul 18 '05
125 7226
[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__(sel f, key): try: return dict.__getitem_ _(self, key) except KeyError: return copy.copy(self. default)
x = safedict(0) x[3] += 1 y = safedict([]) y[5] += range(3) print x, y print x[123], y[234]
safedict() and variants have been previously proposed with the name defaultdict
or some such.
For the most part, adding methods is much less disruptive than introducing a new
type.
As written out above, the += syntax works fine but does not work with append().
As written, the copy.copy() approach is dog slow but can be optimized for lists
and ints while retaining its type flexibility.
BTW, there is no need to make the same post three times.
Raymond
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: self[key].extend(values) except KeyError: self[key] = list(values) For appendlist, I would have expected
def appendlist(self , key, sequence): try: self[key].extend(sequenc e) except KeyError: self[key] = list(sequence)
The original proposal reads better at the point of call when values is a single item. In my experience this will be the typical usage: d.appendlist(ke y, 'some value')
as opposed to your proposal which has to be written d.appendlist(ke y, ['some value'])
The original allows values to be a sequence using d.appendlist(ke y, *value_list)
Kent
Right. I did try the alternatives out and get the issue you point to.
But:
1) In my own code, cases where I'd use the proposed appendlist method
are typically cases where I'd want to add multiple items that have
already been collected in a sequence. But, since I've little coding
under my belt, I concede that considerations drawn from my experience
are not terribly probative. :-)
2) Much more important, IMHO, is that the method name `appendlist'
really does suggest it's a list that will be appended. Hence my stated
expectation. While it would make the method name longer, given the
original interface Raymond posted, I would find appendtolist more
transparent.
out-of-my-depth-ly y'rs,
Brian vdB
"Raymond Hettinger" <vz******@veriz on.net> 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 acceptance.
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 good one and I can also image a graph represented as a dictionary of sets. Still, I don't mind writing out the plain Python for this one if it only comes up once in a blue moon.
Good example. I actually have a directed graph and multigraph module that uses dictionary of sets
internally. It turns out I've used setdefault 8 times in this module alone !
George
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 = ['the', 'quick', 'brownish', 'toad'] max(L, key=len) 'brownish' max(len(x) for x in L)
8
Remain calm. Keep the faith. Guido's design works fine.
No important use cases were left unserved by any() and all(). Raymond Hettinger
Hi
if key not in d:
d[key] = {subkey:value}
else:
d[key][subkey] = value
and
d[(key,subkey)] = value
?
Michel Claveau
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, see the subtle
differences between the two versions of safedict, and you'll be glad
more than one version has been posted. At any rate, what has been
presented is a general idea, nitpicking details is kind of out of
place. Programmers know how to modify a general receipe to suit their
actual needs, right?
(*) In some cases, people do not want to create a dictionary entry when
an inquiry is done on a missing item. In some case, they do. A general
receipe cannot cater to the needs of everybody.
"Aahz" <aa**@pythoncra ft.com> wrote: In article <JbL_d.8237$qN3 .2116@trndny01> , Raymond Hettinger <py****@rcn.com > 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(): Unfriendly to non-native english speakers.
+0.5 for add, increment. If incrementing a negative is unacceptable, how about
update/updateby/updateBy ?
+1 for accumulate. I don't think that separating the two cases -- adding to a scalar or appending to
a list -- is that essential; a self-respecting program should make this obvious by the name of the
parameter anyway ("dictionary.ac cumulate('hello ', words)" vs "a.accumulate(' hello', b)").
George
George Sakkis wrote: "Aahz" <aa**@pythoncra ft.com> wrote: In article <JbL_d.8237$qN3 .2116@trndny01> , Raymond Hettinger <py****@rcn.com > wrote: The proposed names could possibly be improved (perhaps tally() is
more activeand clear than count()). +1 tally()
-1 for count(): Implies an accessor, not a mutator. -1 for tally(): Unfriendly to non-native english speakers. +0.5 for add, increment. If incrementing a negative is unacceptable,
how about update/updateby/updateBy ? +1 for accumulate. I don't think that separating the two cases --
adding to a scalar or appending to a list -- is that essential; a self-respecting program should make
this obvious by the name of the parameter anyway ("dictionary.ac cumulate('hello ', words)" vs
"a.accumulate(' hello', b)").
What about no name at all for the scalar case:
a['hello'] += 1
a['bye'] -= 2
and append() (or augmented assignment) for the list case:
a['hello'].append(word)
a['bye'] += [word]
?
On Sat, 19 Mar 2005 15:17:59 GMT,
"Raymond Hettinger" <vz******@veriz on.net> 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 two cases is essential. Also, the wording should contain strong cues that remind you of addition and of building a list.
Agreed, with a slight hedge towards accumulation or tabulation rather
than addition. I don't think "summation" gets us anywhere, either.
Are the use cases for qty != 1 for weighted averages (that's the only
one I can think of off the top of my head)? Is something like this:
def accumulate( self, key, *values ):
if values == ( ):
values = 1
try:
self[ key ] += values
except KeyError:
if type( key ) == int:
self[ key ] = 1
else
self[ key ] = *values
possible? It's more "klunky" than I thought it would be before I
started typing it out.
Then we'd have these two use cases:
histogram = { }
for word in text.split( ):
histogram.accum ulate( word )
and
org_chart = { }
for employee in employees:
org_chart.accum ulate( employee.manage r, employee.name )
Regards,
Dan
--
Dan Sommers
<http://www.tombstoneze ro.net/dan/>
μ₀ × ε₀ × c² = 1
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): try: self[key].extend(values) except KeyError: self[key] = list(values)
-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 extendable. Why not
subtractable, dividable or right-shiftable? Because of majority
approval? I'm mot a speed fetishist and destroying the clarity of a
very fundamental data structure for speedup rather arbitrary
accumulations seems to be a bad idea. I would move this stuff in a
subclass.
Regards Kay This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Headless |
last post by:
I've marked up song lyrics with the <pre> tag because it seems the most
appropriate type of markup for the type of data. This results in
inefficient use of horizontal space due to UA's default rendering of
<pre> in a fixed width font.
To change that I'd have to specify a proportional font family, thereby
falling into the size pitfall that is associated with any sort of author
specified font family:
a) If I specify a sans serif font...
|
by: Alan Illeman |
last post by:
How do I set several different properties for PRE in
a CSS stylesheet, rather than resorting to this:
<BODY>
<PRE STYLE="font-family:monospace;
font-size:0.95em;
width:40%;
border:red 2px solid;
color:red;
|
by: Buck Turgidson |
last post by:
I want to have a css with 2 PRE styles, one bold with large font, and
another non-bold and smaller font.
I am new to CSS (and not exactly an expert in HTML, for that matter). Is
there a way to do this in CSS?
<STYLE TYPE="text/css">
pre{ font-size:xx-large;
|
by: Michael Shell |
last post by:
Greetings,
Consider the XHTML document attached at the end of this post.
When viewed under Firefox 1.0.5 on Linux, highlighting
and pasting (into a text editor) the <pre> tag listing will
preserve formatting (white space and line feeds). However, this
is not true when doing the same with the <code> tag listing
(it will all be pasted on one line with multiple successive spaces
treated as a single space) despite the fact that...
|
by: Jarno Suni not |
last post by:
It seems to be invalid in HTML 4.01, but valid in XHTML 1.0. Why is there the difference? Can that pose a problem when such a XHTML document is served as text/html?
| |
by: Rocky Moore |
last post by:
I have a web site called HintsAndTips.com. On this site people post tips
using a very simply webform with a multi line TextBox for inputing the tip
text. This text is encode to HTML so that no tags will remain making the
page safe (I have to convert the linefeeds to <BR>s because the
Server.EncodeHTML does not do that it seems).
The problem is that users can use a special tag when editing the top to
specify an area of the tip that will...
|
by: Eric Lindsay |
last post by:
I can't figure how to best display little snippets of shell script using
<pre>. I just got around to organising to bulk validate some of my web
pages, and one of the problems occurs with Bash shell pieces like this:
<pre><code>
#!/bin/sh
ftp -i -n ftp.server.com< <EOF
user username password
epsv4
cd /
|
by: Xah Lee |
last post by:
The Concepts and Confusions of Pre-fix, In-fix, Post-fix and Fully
Functional Notations
Xah Lee, 2006-03-15
Let me summarize: The LISP notation, is a functional notation, and is
not a so-called pre-fix notation or algebraic notation.
Algebraic notations have the concept of operators, meaning, symbols
placed around arguments. In algebraic in-fix notation, different
|
by: Schraalhans Keukenmeester |
last post by:
I am building a default sheet for my linux-related pages.
Since many linux users still rely on/prefer viewing textmode and
unstyled content I try to stick to the correct html tags to pertain good
readibility on browsers w/o css-support.
For important notes, warnings etc I use the <pre> tag, which shows in a
neat bordered box when viewed with css, and depending on its class a
clarifying background-image is shown.
I would like the...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |