Skip to content

Commit c0dc77f

Browse files
authored
Merge pull request #10 from ModECI/development
Fix export as dict->json/yaml; to v0.2.3
2 parents e926a9d + 38e8b4c commit c0dc77f

File tree

7 files changed

+148
-4
lines changed

7 files changed

+148
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
[![Continuous builds](https://github.com/ModECI/modelspec/actions/workflows/ci.yml/badge.svg)](https://github.com/ModECI/modelspec/actions/workflows/ci.yml) [![PyPI](https://img.shields.io/pypi/v/modelspec)](https://pypi.org/project/modelspec/)
55

66

7-
Functionality for specifying models & enabling automatic serialization (e.g. in JSON and YAML format).
7+
Functionality for specifying the structure of models & enabling automatic serialization to them (e.g. in JSON and YAML format).
88

99
This package is being used by [NeuroMLlite](https://github.com/NeuroML/NeuroMLlite) & [MDF](https://github.com/ModECI/MDF).
1010

11-
11+
For an example of the use of this package see [here](examples/README.md).

examples/README.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,49 @@
1-
## Some simple examples of the use of Modelspec
1+
# Some simple examples of the use of Modelspec
22

33
See also packages [NeuroMLlite](https://github.com/NeuroML/NeuroMLlite) & [MDF](https://github.com/ModECI/MDF) for usage of Modelspec.
4+
5+
## A simple definition of the structure of a Document in modelspec
6+
7+
### 1) Define the structure of a "Document"
8+
9+
A Python file defining the structure of the Document, as well as saving an instance (see below) can be found here: [document.py](document.py).
10+
11+
12+
### 2) Generate human and machine readable specifications of what can be in a Document
13+
14+
A generated Markdown file where the structure of a Document is defined is here: [document.md](document.md). To generate this:
15+
```
16+
doc_md = doc.generate_documentation(format="markdown")
17+
```
18+
19+
A similar file can be done in reStructuredText (RST) format ([document.rst](document.rst))
20+
```
21+
doc_rst = doc.generate_documentation(format="rst")
22+
```
23+
24+
For a machine readable version of this, generate in dict format and save to [YAML](document.specification.yaml) or [JSON](document.specification.json).
25+
```
26+
doc_dict = doc.generate_documentation(format="dict")
27+
28+
with open("document.specification.json", "w") as d:
29+
d.write(json.dumps(doc_dict, indent=4))
30+
31+
with open("document.specification.yaml", "w") as d:
32+
d.write(yaml.dump(doc_dict, indent=4, sort_keys=False))
33+
```
34+
35+
### 3) Create an instance of a Document
36+
37+
The actual instance of a document which is built (in [document.py](document.py)) can be saved in [YAML](document.yaml) or [JSON](document.json) format:
38+
```
39+
doc = Document(id="MyBook")
40+
doc.title = "My life in Python"
41+
42+
a = Section(id="Abstract")
43+
doc.sections.append(a)
44+
45+
...
46+
47+
doc.to_json_file("document.json")
48+
doc.to_yaml_file("document.yaml")
49+
```

examples/document.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,17 @@ class Document(Base):
7676

7777
with open("document.rst", "w") as d:
7878
d.write(doc_rst)
79+
80+
81+
print("\nGenerating specification in dict form")
82+
doc_dict = doc.generate_documentation(format="dict")
83+
84+
import json
85+
import yaml
86+
87+
with open("document.specification.json", "w") as d:
88+
d.write(json.dumps(doc_dict, indent=4))
89+
90+
print("Generating specification in YAML")
91+
with open("document.specification.yaml", "w") as d:
92+
d.write(yaml.dump(doc_dict, indent=4, sort_keys=False))

examples/document.specification.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"Document": {
3+
"definition": "A model for documents",
4+
"allowed_parameters": {
5+
"id": {
6+
"type": "str",
7+
"description": "The unique id of the document"
8+
},
9+
"title": {
10+
"type": "str",
11+
"description": "Document title"
12+
},
13+
"ISBN": {
14+
"type": "int",
15+
"description": "International Standard Book Number"
16+
}
17+
},
18+
"allowed_children": {
19+
"sections": {
20+
"type": "Section",
21+
"description": "The sections of the document"
22+
}
23+
}
24+
},
25+
"Section": {
26+
"definition": "A model of a section of the :class:`Document`. Will contain one :class:`Paragraph` or more",
27+
"allowed_parameters": {
28+
"id": {
29+
"type": "str",
30+
"description": "The id of the section"
31+
}
32+
},
33+
"allowed_children": {
34+
"paragraphs": {
35+
"type": "Paragraph",
36+
"description": "The paragraphs"
37+
}
38+
}
39+
},
40+
"Paragraph": {
41+
"definition": "A model of a paragraph",
42+
"allowed_parameters": {
43+
"contents": {
44+
"type": "str",
45+
"description": "Paragraph contents, which make up the _Section_s."
46+
}
47+
}
48+
}
49+
}

examples/document.specification.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
Document:
2+
definition: A model for documents
3+
allowed_parameters:
4+
id:
5+
type: str
6+
description: The unique id of the document
7+
title:
8+
type: str
9+
description: Document title
10+
ISBN:
11+
type: int
12+
description: International Standard Book Number
13+
allowed_children:
14+
sections:
15+
type: Section
16+
description: The sections of the document
17+
Section:
18+
definition: A model of a section of the :class:`Document`. Will contain one :class:`Paragraph`
19+
or more
20+
allowed_parameters:
21+
id:
22+
type: str
23+
description: The id of the section
24+
allowed_children:
25+
paragraphs:
26+
type: Paragraph
27+
description: The paragraphs
28+
Paragraph:
29+
definition: A model of a paragraph
30+
allowed_parameters:
31+
contents:
32+
type: str
33+
description: Paragraph contents, which make up the _Section_s.

src/modelspec/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.2.2"
1+
__version__ = "0.2.3"
22

33
from .base_types import Base, define, has, field, fields, optional, instance_of, in_
44

src/modelspec/base_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,8 @@ def insert_links(text, format=MARKDOWN_FORMAT):
721721
for r in referenced:
722722
if format in (MARKDOWN_FORMAT, RST_FORMAT):
723723
doc_string += r._cls_generate_documentation(format=format)
724+
if format in (DICT_FORMAT):
725+
doc_dict.update(r._cls_generate_documentation(format=format))
724726

725727
if format in (MARKDOWN_FORMAT, RST_FORMAT):
726728
return doc_string

0 commit comments

Comments
 (0)