Test Code

project_082_py00.py - test code

Demonstrates storing data in JSON files.

#!/usr/bin/python3
# ===================================================================
# python JSON demo
# ===================================================================

import json

costs1 = [ [0, [(4,2),(1,4),(5,8)],  "home"],
           [1, [(0,4),(4,6),(2,10)],  None],
           [2, [(3,3),(1,5)],         None],
           [3, [(2,3),(4,4),(5,5)],  "work"],
           [4, [(0,2),(3,4),(1,6)],   None],
           [5, [(3,5),(0,8)],        "shopping"] ]

costs2 =  { "nodes":[
                  {
                  "id":0,
                  "outbound": [
                              {
                              "node":4,
                              "cost":2
                              },
                              {
                              "node":1,
                              "cost":4
                              },
                              {
                              "node":5,
                              "cost":8
                              }
                              ],
                  "name":"home"
                  },
                  {
                  "id":1,
                  "outbound": [
                              {
                              "node":0,
                              "cost":4
                              },
                              {
                              "node":1,
                              "cost":10
                              }
                              ],
                  "name":""
                  },
                  {
                  "id":2,
                  "outbound": [
                              {
                              "node":3,
                              "cost":3
                              },
                              {
                              "node":1,
                              "cost":10
                              }
                              ],
                  "name":""
                  },
                  {
                  "id":3,
                  "outbound": [
                              {
                              "node":2,
                              "cost":3
                              },
                              {
                              "node":4,
                              "cost":4
                              },
                              {
                              "node":5,
                              "cost":5
                              }
                              ],
                  "name":"work"
                  },
                  {
                  "id":4,
                  "outbound": [
                              {
                              "node":0,
                              "cost":2
                              },
                              {
                              "node":3,
                              "cost":4
                              },
                              {
                              "node":1,
                              "cost":6
                              }
                              ],
                  "name":""
                  },
                  {
                  "id":5,
                  "outbound": [
                              {
                              "node":3,
                              "cost":5
                              },
                              {
                              "node":0,
                              "cost":8
                              }
                              ],
                  "name":"shopping"
                  }
                  ]
        }





# -------------------------------------------------------------------
# ---- input,output JSON file
# -------------------------------------------------------------------

def output_json_file(filename,jdata):
    with open(filename,'w') as outfile:
        outfile.write(json.dumps(jdata))
    return

def input_json_file(filename):
    with open(filename,'r') as infile:
        x = json.load(infile)

    return x

# -------------------------------------------------------------------
# ---- test JSON file input/output
# -------------------------------------------------------------------

def test_json_file(jsonfile,jdata):

    print()
    print(f'jdata type = {type(jdata)}')
    print(f'output JSON file {jsonfile}, jdata')
    output_json_file(jsonfile,jdata)

    print()

    print(f'input JSON file {jsonfile}  ==>> xyz')
    xyz = input_json_file(jsonfile)
    print(f'xyz type = {type(xyz)}')


# -------------------------------------------------------------------
# ---- main
# -------------------------------------------------------------------

jsonfile = 'costs.json'

test_json_file(jsonfile,costs1)

print()
print('-------------------------------------------------------')
print()

test_json_file(jsonfile,costs2)