Question

I'm looking to use OrderedDict in my code since the Ruby version of this project uses YAML for maps and settings but OrderedDict hasn't been updated for Python 3.

The Ruby version of the project uses this file: https://mega.co.nz/#!zhYRwA4B!HceqC3-NmmN44U70--jMxiAWQ4wz5MdAeilteKAOnSM

I changed the import lines to read as:

from collections import UserDict
from collections import MutableMapping as DictMixin

I already know v3 doesn't make use of iterkeys so how would I change iterkeys to something MutableMapping understands?

iterkeys = DictMixin.iterkeys
itervalues = DictMixin.itervalues
iteritems = DictMixin.iteritems

I've seen many examples of how to change it but I don't understand how to use them here.

Maybe I should use something else for YAML file support in Python 3, perhaps?

Edit: I called my file yaml.py. I shouldn't have done that. I also had a file path in there it didn't like. I've removed it and it works.

Edit 2: I've just realised I've screwed up my question. It should have been about OMAPs in Python not YAML. I'll open a different question for that. Thanks for the help guys.

Was it helpful?

Solution

Step by step:

  1. Create python 3 vitrtualenv

    mkvirtualenv -p python3 testyaml
    
  2. Install pyaml inside virtualenv

    pip install pyaml
    
  3. Now create some yaml file (example from https://en.wikipedia.org/wiki/YAML#Examples):

    receipt:     Oz-Ware Purchase Invoice
    date:        2012-08-06
    customer:
        given:   Dorothy
        family:  Gale
    
    items:
        - part_no:   A4786
          descrip:   Water Bucket (Filled)
          price:     1.47
          quantity:  4
    
        - part_no:   E1628
          descrip:   High Heeled "Ruby" Slippers
          size:      8
          price:     100.27
          quantity:  1
    
    bill-to:  &id001
        street: |
                123 Tornado Alley
                Suite 16
        city:   East Centerville
        state:  KS
    
    ship-to:  *id001
    
    specialDelivery:  >
        Follow the Yellow Brick
        Road to the Emerald City.
        Pay no attention to the
        man behind the curtain.
    
  4. And python script

    import yaml
    
    d = yaml.load(open('foo.yaml'))
    
    print(d.keys())
    print(d.values())
    
  5. Run script

    python3 foo.py
    
  6. Check results

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top