[Python-ideas] Dict literal use for custom dict classes
Andrew Barnert
abarnert at yahoo.com
Tue Dec 15 14:01:10 EST 2015
On Tuesday, December 15, 2015 4:24 AM, Franklin? Lee <leewangzhong+python at gmail.com> wrote:
> > On Sun, Dec 13, 2015 at 12:15 AM, Andrew Barnert via Python-ideas
> <python-ideas at python.org> wrote:
>> And I think there's some precedent here. IIRC, in YAML, {1:2, 3:4}
> is unordered dict a la JSON (and Python), but [1:2, 3:4] is... actually, I think
> it's ambiguous between an ordered dict and a list of pairs, and you can
> resolve that by declaring !odict or !seq, or you can just leave it up to the
> implementation to pick one if you don't care... but let's pretend it
> wasn't ambiguous; either one covers the use case (and Python only has the
> latter option anyway, unless OrderedDict becomes a builtin).
>
> For YAML, I read it as a list of dicts. My Python's yaml module
> (pyyaml?) agrees.
According to the YAML 1.1 (and 1.2, since AFAICR they never created the separate repo for 1.2) type-repo omap spec (http://yaml.org/type/omap.html):
> Most programming languages do not have a built-in native data type for supporting ordered maps. Such data types are usually provided by libraries. If no such data type is available, an application may resort to loading an “!!omap” into a native array of hash tables containing one key each.
> The “!!omap” tag may be given explicitly. Alternatively, the application may choose to implicitly type a sequence of single-key mappings to ordered maps. In this case, an explicit “!seq” transfer must be given to sequences of single-key mappings that do not represent ordered maps.
So, if you don't specify either "!!omap" or "!seq", it's up to your implementation whether you've designated an ordered dict or a list of dicts.
I was wrong on some of the details--it's "!!omap" rather than "!odict", and it's a list of one-element dicts rather than a list of pairs, and it sounds like even if you _do_ explicitly specify one type the implementation is allowed to give you the other...
But still, as I said, YAML is precedent for Python interpreting ['one': 1, 'two': 2] as OrderedDict([('one', 1), ('two', 2)]).
Of course it's also precedent for Python interpreting that as a list of dicts [{'one': 1}, {'two': 2}], and I don't think anyone wants that... I suppose you can find precedent for almost any idea , no matter how silly, as long as it's implementable. :)
>>>> import yaml
>>>> yaml.load('[a: 1, b: 2]')
> [{'a': 1}, {'b': 2}]
>
> However, YAML's website (page: http://www.yaml.org/refcard.html) lists
> the !!omap type cast as using this syntax:
>
> '!!omap': [ one: 1, two: 2 ]
>
> I tried using !!seq. Not sure if I'm doing it right.
>
>>>> yaml.load('!!seq [a: 1, b: 2]')
> [{'a': 1}, {'b': 2}]
>>>> yaml.load('!!omap [a: 1, b: 2]')
> [('a', 1), ('b', 2)]
>
> (Huh. PyYAML module thinks that an omap should be a list of pairs. It
> might eventually change to OrderedDict, though.)
Ha, so I was wrong about YAML allowing that, but PyYAML does it anyway?
More information about the Python-ideas
mailing list