XClose

COMP0233: Research Software Engineering With Python

Home
Menu
In [1]:
house = {
    'living': {
        'exits': {
            'north': 'kitchen',
            'outside': 'garden',
            'upstairs': 'bedroom'
        },
        'people': ['James'],
        'capacity': 2
    },
    'kitchen': {
        'exits': {
            'south': 'living'
        },
        'people': [],
        'capacity': 1
    },
    'garden': {
        'exits': {
            'inside': 'living'
        },
        'people': ['Sue'],
        'capacity': 3
    },
    'bedroom': {
        'exits': {
            'downstairs': 'living',
            'jump': 'garden'
        },
        'people': [],
        'capacity': 1
    }
}

Save the maze with json:

In [2]:
import json
In [3]:
with open('maze.json', 'w') as json_maze_out:
    json_maze_out.write(json.dumps(house))

Consider the file on the disk:

In [4]:
%%bash
cat 'maze.json'
{"living": {"exits": {"north": "kitchen", "outside": "garden", "upstairs": "bedroom"}, "people": ["James"], "capacity": 2}, "kitchen": {"exits": {"south": "living"}, "people": [], "capacity": 1}, "garden": {"exits": {"inside": "living"}, "people": ["Sue"], "capacity": 3}, "bedroom": {"exits": {"downstairs": "living", "jump": "garden"}, "people": [], "capacity": 1}}

and now load it into a different variable:

In [5]:
with open('maze.json') as json_maze_in:
    maze_again = json.load(json_maze_in)
In [6]:
maze_again
Out[6]:
{'living': {'exits': {'north': 'kitchen',
   'outside': 'garden',
   'upstairs': 'bedroom'},
  'people': ['James'],
  'capacity': 2},
 'kitchen': {'exits': {'south': 'living'}, 'people': [], 'capacity': 1},
 'garden': {'exits': {'inside': 'living'}, 'people': ['Sue'], 'capacity': 3},
 'bedroom': {'exits': {'downstairs': 'living', 'jump': 'garden'},
  'people': [],
  'capacity': 1}}

Or with YAML:

In [7]:
import yaml
In [8]:
with open('maze.yaml', 'w') as yaml_maze_out:
    yaml_maze_out.write(yaml.dump(house))
In [9]:
%%bash
cat 'maze.yaml'
bedroom:
  capacity: 1
  exits:
    downstairs: living
    jump: garden
  people: []
garden:
  capacity: 3
  exits:
    inside: living
  people:
  - Sue
kitchen:
  capacity: 1
  exits:
    south: living
  people: []
living:
  capacity: 2
  exits:
    north: kitchen
    outside: garden
    upstairs: bedroom
  people:
  - James
In [10]:
with open('maze.yaml') as yaml_maze_in:
    maze_again = yaml.safe_load(yaml_maze_in)
In [11]:
maze_again
Out[11]:
{'bedroom': {'capacity': 1,
  'exits': {'downstairs': 'living', 'jump': 'garden'},
  'people': []},
 'garden': {'capacity': 3, 'exits': {'inside': 'living'}, 'people': ['Sue']},
 'kitchen': {'capacity': 1, 'exits': {'south': 'living'}, 'people': []},
 'living': {'capacity': 2,
  'exits': {'north': 'kitchen', 'outside': 'garden', 'upstairs': 'bedroom'},
  'people': ['James']}}