-2

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.

bricky149
  • 81
  • 1
  • 1
  • 8
  • What do you mean by "OrderedDict hasn't been updated for Python 3"? – Janne Karila Sep 09 '13 at 12:45
  • Do you mean some other ordered dict than [`collections.OrderedDict`](http://docs.python.org/3/library/collections.html#collections.OrderedDict)? – Janne Karila Sep 09 '13 at 12:53
  • I'm still fairly new to Python and I'm looking for something which can read the YAML files I have. The YAML files are used for displaying the map terrain, user settings, etc. Would using collections.OrderedDict be suitable for it? – bricky149 Sep 09 '13 at 12:57
  • Can you provide some working example? Input file, what do you have right now and expected output. – zero323 Sep 09 '13 at 12:57
  • Pyaml http://pyyaml.org/ supports python3 since 2008. – zero323 Sep 09 '13 at 12:58
  • @zero323 I meant the OrderedDict file I was using from PyPi. My bad! – bricky149 Sep 09 '13 at 13:01
  • So what is the connection between OrderedDict and YAML files support? I want to read YAML, use pyymal. If you want OrderedDict why not use collections.OrderedDict? – zero323 Sep 09 '13 at 13:23

1 Answers1

0

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

zero323
  • 322,348
  • 103
  • 959
  • 935