Proposal: New syntax for OrderedDict, maybe built-in
Hi there, I'd like to propose that the OrderedDict get a more readable syntax, the way the syntax for set changed from set(['spam', 'ham', 'eggs']) to {'spam', 'ham', 'eggs'} when it became a built-in type in Python 2.4. The way set is unordered like the keys of a dict, similarly list is ordered like the keys of an OrderedDict. In that sense, what {'ham', 'eggs'} is to {'ham': 'spam', 'eggs': 'spam'}, so ['ham', 'eggs'] is to ... my proposal of a cleaner OrderedDict syntax ... ['ham': 'spam', 'eggs': 'spam']. So a "dict" is like a "set" (hence curly braces) of key-value pairs. And an "OrderedDict" is like a "list" (hence square braces) of key-value pairs. (Of course, I am ignoring how lists can have duplicate items. An ordered set would carry the analogy further, but it wouldn't help illustrate my syntax proposal.) I find "['ham': 'spam', 'eggs': 'spam']"" unambiguous, and more readable than "OrderedDict([('ham', 'spam'), ('eggs', 'spam')])". I work with OrderedDict a lot, because JSON represents an OrderedDict and I need to work with JSON a lot. In an attempt to make the OrderedDicts that I work with more readable, I wrote a module to parse a string representation of an OrderedDict that uses my proposed syntax. You can find it here: https://github.com/kaapstorm/listdict With the ubiquity of JSON, it may also be time to promote OrderedDict to a built-in type too. Kind regards, Norman
On Thu, Sep 04, 2014 at 07:35:06PM +0200, Norman Hooper wrote:
I work with OrderedDict a lot, because JSON represents an OrderedDict and I need to work with JSON a lot.
ECMAScript: 4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method. JSON: 1. Introduction An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
With the ubiquity of JSON, it may also be time to promote OrderedDict to a built-in type too.
Neither JSON objects nor JavaScript object properties preserve enumeration order. This is a common misconception since implementations tend to preserve order when the number of keys is small, since they may use a more efficient internal representation in that case, whose enumeration order depends on order the properties were defined in. David
Hi, On 09/04/2014 08:00 PM, David Wilson wrote:
On Thu, Sep 04, 2014 at 07:35:06PM +0200, Norman Hooper wrote:
I work with OrderedDict a lot, because JSON represents an OrderedDict and I need to work with JSON a lot. ECMAScript:
4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.
JSON:
1. Introduction An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
You are absolutely right. And as you point out ...
implementations tend to preserve order when the number of keys is small, since they may use a more efficient internal representation in that case, whose enumeration order depends on order the properties were defined in.
I am not aware of any implementations (although I'm also not aware of all implementations) that do not preserve order. So (a bit like natural languages) one needs to conform with how it is used, not how it is defined, if one wants to be fully understood. Kind regards, Norman
Any word on when Richard will be releasing a fsf/gnu friendly cell phone? thanks, Jason s.o.f.t. www.leap.cc On Thu, Sep 4, 2014 at 1:11 PM, Norman Hooper <norman@kaapstorm.com> wrote:
Hi,
On 09/04/2014 08:00 PM, David Wilson wrote:
On Thu, Sep 04, 2014 at 07:35:06PM +0200, Norman Hooper wrote:
I work with OrderedDict a lot, because JSON represents an OrderedDict
and I need to work with JSON a lot.
ECMAScript:
4.3.3 Object An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.
JSON:
1. Introduction An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.
You are absolutely right. And as you point out ...
implementations
tend to preserve order when the number of keys is small, since they may use a more efficient internal representation in that case, whose enumeration order depends on order the properties were defined in.
I am not aware of any implementations (although I'm also not aware of all implementations) that do not preserve order.
So (a bit like natural languages) one needs to conform with how it is used, not how it is defined, if one wants to be fully understood.
Kind regards,
Norman
_______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On Thu, Sep 04, 2014 at 08:11:10PM +0200, Norman Hooper wrote:
I am not aware of any implementations (although I'm also not aware of all implementations) that do not preserve order.
$ jsc >>> function keys(o) { ... var k = []; ... for(var key in o) k.push(key); ... return k; ... } undefined >>> keys({"5": 1, "4": 1}) 4,5 David
On Thu, Sep 4, 2014 at 1:18 PM, David Wilson <dw+python-ideas@hmmz.org> wrote:
$ jsc >>> function keys(o) { ... var k = []; ... for(var key in o) k.push(key); ... return k; ... } undefined >>> keys({"5": 1, "4": 1}) 4,5
I love a simple existence proof! Can we now discard the presumed equivalence of OrderDict and JS objects? Besides, even if JS and Python were the only two languages we cared about, JSON is supported in many other languages. I'd be really surprised if all of them guaranteed key order. If fact, I wouldn't be surprised if none of them did. Skip
On Thu, Sep 4, 2014 at 2:00 PM, David Wilson <dw+python-ideas@hmmz.org> wrote:
On Thu, Sep 04, 2014 at 07:35:06PM +0200, Norman Hooper wrote:
I work with OrderedDict a lot, because JSON represents an OrderedDict and I need to work with JSON a lot.
With the ubiquity of JSON, it may also be time to promote OrderedDict to a built-in type too.
Neither JSON objects nor JavaScript object properties preserve enumeration order. This is a common misconception since implementations tend to preserve order when the number of keys is small, since they may use a more efficient internal representation in that case, whose enumeration order depends on order the properties were defined in.
JSON/JavaScript might not be the best use case for this reason. But YAML is! YAML has an "omap" construct [1] for representing ordered mappings. In "block style" this is represented like: - aardvark: African pig-like ant eater. Ugly. - anteater: South-American ant eater. Two species. - anaconda: South-American constrictor snake. Scaly. This syntax is basically the same as that for a list of one-element mappings. In YAML's "flow style" this equates to exactly the OP's proposal: ["aardvark": "...", "anteater": "...", "anaconda": "..."] Unfortunately there's no way for the YAML parser to disambiguate this from a list of one-element mappings, unless the "!!omap" tag is explicitly prepended. But in some of my own applications I find it useful enough to just assume this should be an OrderedDict. Though it's just as easy only make an OrderedDict when "!!omap" is used. In PyYAML this is supported by returning a list of tuples, though it's easy to then wrap that in an OrderedDict:
yaml.load("['a': 1, 'b': 2]") [{'a': 1}, {'b': 2}] yaml.load("!!omap ['a': 1, 'b': 2]") [('a', 1), ('b', 2)]
I have one application that uses this extensively and would personally like to see it in Python. But I think the objections thus far are (mostly) reasonable. Erik [1] http://yaml.org/type/omap.html
participants (5)
-
David Wilson
-
Erik Bray
-
Jason Bursey
-
Norman Hooper
-
Skip Montanaro