> from itertools import count, izip > > def dict2str(d, preferred_order = ['gid', 'type', 'parent', 'name']): > last = len(preferred_order) > rank = dict(izip(preferred_order, count())) > pairs = d.items() > pairs.sort(key=lambda (k,v): rank.get(k, (last, k, v))) > return '{%s}' % repr(pairs)[1:-1]
P.S. If you need the string to evaluatable like repr(d), then change the last line to: return 'dict(%r)' % pairs -- http://mail.python.org/mailman/listinfo/python-list