Skip to content

Instantly share code, notes, and snippets.

@dmsurti
Created July 19, 2017 13:35
Show Gist options
  • Save dmsurti/2dfdb41c92ca99fc82709f89873a5fd9 to your computer and use it in GitHub Desktop.
Save dmsurti/2dfdb41c92ca99fc82709f89873a5fd9 to your computer and use it in GitHub Desktop.
Using python-jsonschema-objects to generate a class for blockMeshDict
import json
import python_jsonschema_objects as pjs
schema = {
"title": "Example Schema",
"type" : "object",
"properties": {
"blocks": {
"id": "/properties/blocks",
"properties": {
"cell_ratios": {
"id": "/properties/blocks/properties/cell_ratios",
"items": {
"id": "/properties/blocks/properties/cell_ratios/items",
"type": "integer"
},
"type": "array"
},
"edge_grading": {
"id": "/properties/blocks/properties/edge_grading",
"type": "boolean"
},
"multi_grading": {
"id": "/properties/blocks/properties/multi_grading",
"type": "boolean"
},
"multi_grading_ratios": {
"id": "/properties/blocks/properties/multi_grading_ratios",
"properties": {
"x": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/x",
"items": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/x/items",
"items": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/x/items/items",
"type": "integer"
},
"type": "array"
},
"type": "array"
},
"y": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/y",
"items": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/y/items",
"items": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/y/items/items",
"type": "integer"
},
"type": "array"
},
"type": "array"
},
"z": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/z",
"items": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/z/items",
"items": {
"id": "/properties/blocks/properties/multi_grading_ratios/properties/z/items/items",
"type": "integer"
},
"type": "array"
},
"type": "array"
}
},
"type": "object"
},
"num_cells": {
"id": "/properties/blocks/properties/num_cells",
"items": {
"id": "/properties/blocks/properties/num_cells/items",
"type": "integer"
},
"type": "array"
},
"simple_grading": {
"id": "/properties/blocks/properties/simple_grading",
"type": "boolean"
},
"vertex_nums": {
"id": "/properties/blocks/properties/vertex_nums",
"items": {
"id": "/properties/blocks/properties/vertex_nums/items",
"type": "integer"
},
"type": "array"
}
},
"type": "object"
},
"boundary": {
"id": "/properties/boundary",
"items": {
"id": "/properties/boundary/items",
"properties": {
"cyclic": {
"id": "/properties/boundary/items/properties/cyclic",
"type": "boolean"
},
"empty": {
"id": "/properties/boundary/items/properties/empty",
"type": "boolean"
},
"faces": {
"id": "/properties/boundary/items/properties/faces",
"items": {
"id": "/properties/boundary/items/properties/faces/items",
"items": {
"id": "/properties/boundary/items/properties/faces/items/items",
"type": "integer"
},
"type": "array"
},
"type": "array"
},
"name": {
"id": "/properties/boundary/items/properties/name",
"type": "string"
},
"neighbor_patch": {
"id": "/properties/boundary/items/properties/neighbor_patch",
"type": "string"
},
"patch": {
"id": "/properties/boundary/items/properties/patch",
"type": "boolean"
},
"wall": {
"id": "/properties/boundary/items/properties/wall",
"type": "boolean"
}
},
"type": "object"
},
"type": "array"
},
"class": {
"id": "/properties/class",
"type": "string"
},
"convertToMeters": {
"id": "/properties/convertToMeters",
"type": "number"
},
"edges": {
"id": "/properties/edges",
"properties": {
"BSpline": {
"id": "/properties/edges/properties/BSpline",
"type": "boolean"
},
"arc": {
"id": "/properties/edges/properties/arc",
"type": "boolean"
},
"end": {
"id": "/properties/edges/properties/end",
"type": "integer"
},
"interp_points": {
"id": "/properties/edges/properties/interp_points",
"items": {
"id": "/properties/edges/properties/interp_points/items",
"type": "number"
},
"type": "array"
},
"line": {
"id": "/properties/edges/properties/line",
"type": "boolean"
},
"polyLine": {
"id": "/properties/edges/properties/polyLine",
"type": "boolean"
},
"spline": {
"id": "/properties/edges/properties/spline",
"type": "boolean"
},
"start": {
"id": "/properties/edges/properties/start",
"type": "integer"
}
},
"type": "object"
},
"format": {
"id": "/properties/format",
"type": "string"
},
"mergePatchPairs": {
"id": "/properties/mergePatchPairs",
"items": {
"id": "/properties/mergePatchPairs/items",
"items": {
"id": "/properties/mergePatchPairs/items/items",
"type": "string"
},
"type": "array"
},
"type": "array"
},
"object": {
"id": "/properties/object",
"type": "string"
},
"version": {
"id": "/properties/version",
"type": "string"
},
"vertices": {
"id": "/properties/vertices",
"items": {
"id": "/properties/vertices/items",
"items": {
"id": "/properties/vertices/items/items",
"type": "integer"
},
"type": "array"
},
"type": "array"
}
},
"type": "object"
}
builder = pjs.ObjectBuilder(schema)
ns = builder.build_classes()
BMD = ns.ExampleSchema
bmd = BMD()
bmd.version = "2.0"
print(bmd.version, bmd.vertices)
bmd.vertices = []
bmd.vertices.append([1, 2, 3])
bmd.vertices.append([4, 5, 6])
for v in bmd.vertices:
print(v, type(v))
j = json.loads(bmd.serialize())
print(j)
l = [v for v in j["vertices"]]
print(l)
@dmsurti
Copy link
Author

dmsurti commented Jul 19, 2017

Output for a sample run:

2.0 None
<example_schema/vertices_<anonymous_field>=[1, 2, 3]> <class 'abc.example_schema/vertices_<anonymous_field>'>
<example_schema/vertices_<anonymous_field>=[4, 5, 6]> <class 'abc.example_schema/vertices_<anonymous_field>'>
{'version': '2.0', 'vertices': [[1, 2, 3], [4, 5, 6]]}
[[1, 2, 3], [4, 5, 6]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment