Custom sort order for YAML output

Sam Kuper <[email protected]>
Newsgroups gmane.text.yaml.general
Message-ID <[email protected]>
Dear list,

Bearing in mind I'm new to PyYAML, please could you help with the following.

I'm using PyYAML to convert dumped YAML data from Django (which is flow
style by default) into block style via the following Python script:

[BEGIN]
from optparse import OptionParser
import yaml

def main():
    # set up optparse
    usage = "usage: %prog [options] INPUT\nReads yaml from INPUT and returns
it as block style yaml to stdout"
    parser = OptionParser(usage)
    (options, args) = parser.parse_args()
    if len(args) != 1:
        parser.error("incorrect number of arguments")

    # get some flow style YAML from FILE1
    try:
        infile = open(args[0])
    except IOError:
        print 'Cannot open input file: ', args[0]
    else:
        input = infile.read()
        output = yaml.dump(yaml.load(input), default_flow_style=False)
        print output

# execute main()
if __name__ == "__main__":
    main()
[END]

This works, but I'd like to output the data in the order shown
here<http://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-data-with-fixtures>-
in other words, in the form:

- model:
  pk:
  fields:

I'd also like the fields to be sorted alphabetically.

This ticket <http://pyyaml.org/ticket/23> suggests to me that this should be
possible, but I'm not sure how best to insert a "custom representer" into
the script above, in order to get the result I'm seeking.

I'd be grateful for any help you can offer.

Many thanks in advance,

Sam

------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf

_______________________________________________
Yaml-core mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/yaml-core
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.