Skip to content

Commit 2189d34

Browse files
committed
added jupyter notebook with simple� examples
1 parent 02a923e commit 2189d34

File tree

3 files changed

+134
-2
lines changed

3 files changed

+134
-2
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
/jacoco.exec
1313
/jacoco.xml
1414

15+
# Jupyter Notebook
16+
.ipynb_checkpoints
17+
*-checkpoint.ipynb
18+
1519
# OS: macOS
1620
.DS_Store
1721

README.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,14 @@ as script
306306
307307
optional arguments:
308308
-h, --help show this help message and exit
309-
--version show program's version number and exit
309+
--version show program\'s version number and exit
310310
-v VERBOSITY, --verbosity VERBOSITY
311311
level of verbosity, from 0 to 100 (default: 100)
312312
--get-dependencies, --deps
313313
download dependencies and exit (default: False)
314314
315-
Copyright 2017 Mateusz Bysiek https://mbdevpl.github.io/, Apache License 2.0
315+
Copyright 2017-2018 by the contributors, Apache License 2.0,
316+
https://github.com/mbdevpl/open-fortran-parser-xml
316317
317318
318319
as library
@@ -324,6 +325,8 @@ as library
324325
325326
xml = parse('my_legacy_code.f', verbosity=0)
326327
328+
More examples available in `<examples.ipynb>`_.
329+
327330
328331
testing
329332
-------

examples.ipynb

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Open Fortran Parser XML wrapper examples"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"These examples are for Python wrapper to the OFP XML. Run `ant` to build the OFP XML itself before executing them. "
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"import pathlib\n",
24+
"import tempfile\n",
25+
"import xml.etree.ElementTree as ET\n",
26+
"\n",
27+
"import open_fortran_parser"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 2,
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"<ofp version=\"0.8.4\">\n",
40+
" <file col_begin=\"6\" col_end=\"24\" line_begin=\"2\" line_end=\"8\" path=\"/Users/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f\">\n",
41+
" <start-of-file filename=\"test/examples/empty.f\" path=\"/Users/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f\" rule=\"-2\" />\n",
42+
" <comment col_begin=\"6\" col_end=\"36\" line_begin=\"2\" line_end=\"2\" text=\"! minimalistic Fortran program\" />\n",
43+
" <program col_begin=\"6\" col_end=\"24\" line_begin=\"4\" line_end=\"8\" name=\"empty\">\n",
44+
" <main-program__begin addendum=\"begin\" rule=\"1101\" />\n",
45+
" <header />\n",
46+
" <program-stmt col_begin=\"6\" col_end=\"20\" eos=\"&#10;\" id=\"empty\" line_begin=\"4\" line_end=\"4\" programKeyword=\"program\" rule=\"1102\" />\n",
47+
" <body col_begin=\"6\" col_end=\"20\" line_begin=\"6\" line_end=\"6\">\n",
48+
" <specification col_begin=\"6\" col_end=\"20\" declarations=\"0\" implicits=\"1\" imports=\"0\" line_begin=\"6\" line_end=\"6\" uses=\"0\">\n",
49+
" <declaration col_begin=\"6\" col_end=\"20\" line_begin=\"6\" line_end=\"6\" subtype=\"none\" type=\"implicit\">\n",
50+
" <implicit-stmt col_begin=\"6\" col_end=\"20\" eos=\"&#10;\" hasImplicitSpecList=\"false\" implicitKeyword=\"implicit\" line_begin=\"6\" line_end=\"6\" noneKeyword=\"none\" rule=\"549\" />\n",
51+
" </declaration>\n",
52+
" <declaration />\n",
53+
" <specification-part numDeclConstructs=\"0\" numImplicitStmts=\"1\" numImportStmts=\"0\" numUseStmts=\"0\" rule=\"204\" />\n",
54+
" </specification>\n",
55+
" <statement />\n",
56+
" </body>\n",
57+
" <end-program-stmt col_begin=\"6\" col_end=\"24\" endKeyword=\"end\" eos=\"&#10;\" id=\"empty\" line_begin=\"8\" line_end=\"8\" programKeyword=\"program\" rule=\"1103\" />\n",
58+
" <main-program hasExecutionPart=\"false\" hasInternalSubprogramPart=\"false\" hasProgramStmt=\"true\" rule=\"1101\" />\n",
59+
" </program>\n",
60+
" <end-of-file filename=\"test/examples/empty.f\" path=\"/Users/mateusz/Projects/open-fortran-parser-xml/test/examples/empty.f\" rule=\"-2\" />\n",
61+
" </file>\n",
62+
"</ofp>\n"
63+
]
64+
}
65+
],
66+
"source": [
67+
"xml_tree = open_fortran_parser.parse(pathlib.Path('test', 'examples', 'empty.f'))\n",
68+
"ET.dump(xml_tree)"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 3,
74+
"metadata": {
75+
"scrolled": true
76+
},
77+
"outputs": [
78+
{
79+
"name": "stdout",
80+
"output_type": "stream",
81+
"text": [
82+
"HowBadCanItBe\n"
83+
]
84+
}
85+
],
86+
"source": [
87+
"code = '''\n",
88+
"program HowBadCanItBe\n",
89+
"\n",
90+
" goto main_sub3\n",
91+
"\n",
92+
"end program\n",
93+
"'''\n",
94+
"\n",
95+
"with tempfile.NamedTemporaryFile('w+') as tmp:\n",
96+
" print(code, file=tmp, flush=True)\n",
97+
" xml_tree = open_fortran_parser.parse(pathlib.Path(tmp.name), raise_on_error=True)\n",
98+
"\n",
99+
"for prog in xml_tree.findall('.//program'):\n",
100+
" print(prog.attrib['name'])"
101+
]
102+
}
103+
],
104+
"metadata": {
105+
"kernelspec": {
106+
"display_name": "Python 3",
107+
"language": "python",
108+
"name": "python3"
109+
},
110+
"language_info": {
111+
"codemirror_mode": {
112+
"name": "ipython",
113+
"version": 3
114+
},
115+
"file_extension": ".py",
116+
"mimetype": "text/x-python",
117+
"name": "python",
118+
"nbconvert_exporter": "python",
119+
"pygments_lexer": "ipython3",
120+
"version": "3.6.6"
121+
}
122+
},
123+
"nbformat": 4,
124+
"nbformat_minor": 2
125+
}

0 commit comments

Comments
 (0)