[Python-ideas] OrderedDict literals

Anthony Towns aj at erisian.com.au
Wed Mar 19 08:39:11 CET 2014


On 19 March 2014 16:44, Andrew Barnert <abarnert at yahoo.com> wrote:
> On Mar 18, 2014, at 17:54, Anthony Towns <aj at erisian.com.au> wrote:
>> I was re-reading some old threads about ordereddict literals to see if
>> any of them had gotten anywhere.
> Skimming the linked thread, there were a bunch of messages with variations of "It might be easier to judge if you came up with a real use case", but nobody ever did.
> So, do you have a real use case here? It might look better with headers from some protocol that need to be in order (except I can't think of any such protocol) or whatever you're hoping for than with examples like 'a' or 'foo' or 1.

Oh, sure. The case that I've done most recently was when writing a
tool to let me convert yaml files to and from jira issues. I want to
specify how to convert each field between yaml and jira, and I also
want to specify the order the fields end up in in the yaml file so
that they're predictable. That's static information that I want to
include in my source, and having it as a single ordereddict is the
easiest way to write and store it. (The reason I want the ordering is
both so that (a) if I dump an issue from jira to a file one day, and
store it in git, then do it again the next day, the diff catches any
intentional changes, not just random ordering changes, and (b) when
I'm looking at the yaml file, the fields are in an order that makes
sense to me so I know where to look for particular information).

The code I ended up with was:

 class InterestingFields(OrderedDict):
    def __init__(self, extras):
        super(InterestingFields, self).__init__()

        D_key = D("key")
        D_name = D("name")
        s = S()
        t = T()
        f = self

        f["project"] =      D_key
        f["issuetype"] =    D_name
        f["summary"] =      s
        # 18 more key/value pairs ...

        for k,v in extras:
            f[k] = v

where D,S,T are classes that provide functions to convert from the
Jira REST representation to the python version of the YAML
representation I want and vice-versa.

It took me a while to do the above in a way I didn't totally hate
("oh, I'll just do an OrderedDict( project=D_key, issuetype=D_name,
...)... huh, why didn't that work?"), and I can't say I *liked* what I
ended up with. Better than specifying the fields twice (once for how
to convert, once for the ordering), and better than all the
heiroglyphics involved in writing it as a list of pairs though.

The use case that was crossing my mind this morning was being able to write:

    return { "result": "ok", "details": "everything worked wonderfully" }

from a function whose result gets turned into json and returned over a
REST API, and have the order preserved so that when debugging with
curl, I wouldn't see the "details" come first and confuse myself.
Haven't decided how much I care about that yet.

Cheers,
aj

-- 
Anthony Towns <aj at erisian.com.au>


More information about the Python-ideas mailing list