Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
py-namelist
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Martin Claus
py-namelist
Commits
3eceda0a
Commit
3eceda0a
authored
Oct 05, 2018
by
Martin Claus
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
*Fixed* string output for index assignments
parent
b3a5d90e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
13 deletions
+31
-13
namelist/namelist.py
namelist/namelist.py
+8
-3
tests/test_parsing.py
tests/test_parsing.py
+23
-10
No files found.
namelist/namelist.py
View file @
3eceda0a
...
@@ -62,8 +62,13 @@ class Namelist(DictClass):
...
@@ -62,8 +62,13 @@ class Namelist(DictClass):
"""
"""
retstr
=
"&%s
\n
"
%
str
(
self
.
name
)
retstr
=
"&%s
\n
"
%
str
(
self
.
name
)
for
k
,
v
in
self
.
items
():
for
k
,
v
in
self
.
items
():
if
hasattr
(
v
,
'__iter__'
):
if
hasattr
(
v
,
'islower'
):
# is a string
retstr
+=
"%s = (/ "
%
k
retstr
+=
"{} = {}
\n
"
.
format
(
k
,
repr
(
v
))
elif
hasattr
(
v
,
'items'
):
# is a dictionary
for
i
,
ival
in
v
.
items
():
retstr
+=
"{}({}) = {}
\n
"
.
format
(
k
,
i
,
repr
(
ival
))
elif
hasattr
(
v
,
'index'
):
# is an array
retstr
+=
"{} = (/ "
.
format
(
k
)
tmpstr
=
""
tmpstr
=
""
for
vv
in
v
:
for
vv
in
v
:
if
isinstance
(
vv
,
bool
):
if
isinstance
(
vv
,
bool
):
...
@@ -226,7 +231,7 @@ def _add_value(container, value, key):
...
@@ -226,7 +231,7 @@ def _add_value(container, value, key):
# test if container is a dict like object
# test if container is a dict like object
container
.
keys
()
container
.
keys
()
container
[
key
]
=
value
container
[
key
]
=
value
except
:
# container is a list
except
AttributeError
:
# container is a list
container
.
append
(
value
)
container
.
append
(
value
)
...
...
tests/test_parsing.py
View file @
3eceda0a
...
@@ -58,6 +58,7 @@ def test_parse_file(string):
...
@@ -58,6 +58,7 @@ def test_parse_file(string):
"&nml
\n
val1 = .FALSE.
\n
/
\n
"
,
"&nml
\n
val1 = .FALSE.
\n
/
\n
"
,
"&nml
\n
val1 = (/ 1,2,3,4,5,6 /)
\n
/
\n
"
,
"&nml
\n
val1 = (/ 1,2,3,4,5,6 /)
\n
/
\n
"
,
"&nml
\n
val1 = (/ .TRUE.,.FALSE.,.TRUE.,.TRUE. /)
\n
/
\n
"
,
"&nml
\n
val1 = (/ .TRUE.,.FALSE.,.TRUE.,.TRUE. /)
\n
/
\n
"
,
"&nml
\n
val1 = 3
\n
val2 = 5
\n
/
\n
"
,
]
]
)
)
def
test_string_out
(
string
):
def
test_string_out
(
string
):
...
@@ -189,6 +190,7 @@ def test_var_bool(string, val):
...
@@ -189,6 +190,7 @@ def test_var_bool(string, val):
)
)
def
test_var_string
(
string
,
val
):
def
test_var_string
(
string
,
val
):
nml
=
namelist
.
parse_namelist_string
(
string
)[
"nml"
]
nml
=
namelist
.
parse_namelist_string
(
string
)[
"nml"
]
print
(
str
(
nml
))
assert
nml
[
"val"
]
==
val
assert
nml
[
"val"
]
==
val
...
@@ -243,30 +245,41 @@ def test_index_assignment(string, val1, d):
...
@@ -243,30 +245,41 @@ def test_index_assignment(string, val1, d):
assert
nml
[
"val"
]
==
d
assert
nml
[
"val"
]
==
d
@
pytest
.
mark
.
parametrize
(
"string"
,
[
"&nml {} /"
,
"&nml {} val2=34 /"
])
@
pytest
.
mark
.
parametrize
(
"string"
,
[
"&nml
\n
{}/
\n
"
,
"&nml
\n
{}val2 = 34
\n
/
\n
"
])
@
pytest
.
mark
.
parametrize
(
"val1"
,
[
"lala"
,
"((1 + 2) + 3)"
,
3
,
3.0
,
3e6
,
".T."
])
@
pytest
.
mark
.
parametrize
(
@
pytest
.
mark
.
parametrize
(
"d"
,
[{
1
:
None
,
2
:
35
},{
1
:
None
}])
"val1"
,
[
"some_string"
,
"((1 + 2) + 3)"
,
3
,
3.0
,
3e6
,
".T."
]
)
@
pytest
.
mark
.
parametrize
(
"d"
,
[{
1
:
None
,
2
:
35
},
{
1
:
None
}])
def
test_index_assignment_to_str
(
string
,
d
,
val1
):
def
test_index_assignment_to_str
(
string
,
d
,
val1
):
val_str
=
""
try
:
try
:
int
(
eval
(
val1
))
int
(
eval
(
val1
))
is_expression
=
True
is_expression
=
True
except
:
except
(
NameError
,
SyntaxError
,
TypeError
)
:
is_expression
=
False
is_expression
=
False
is_bool
=
(
val1
==
".T."
)
is_bool
=
(
val1
==
".T."
)
d
[
1
]
=
val1
d
[
1
]
=
val1
val_str
=
""
for
k
,
v
in
d
.
items
():
for
k
,
v
in
d
.
items
():
if
is_expression
|
is_bool
:
if
is_expression
|
is_bool
:
val_str
+=
"val({}) = {}
"
.
format
(
k
,
v
)
val_str
+=
"val({}) = {}
\n
"
.
format
(
k
,
v
)
else
:
else
:
val_str
+=
"val({}) = {} "
.
format
(
k
,
repr
(
v
))
val_str
+=
"val({}) = {}
\n
"
.
format
(
k
,
repr
(
v
))
nml_string
=
string
.
format
(
val_str
)
nml
=
namelist
.
parse_namelist_string
(
nml_string
)[
"nml"
]
if
is_expression
:
if
is_expression
:
d
[
1
]
=
eval
(
d
[
1
])
d
[
1
]
=
eval
(
d
[
1
])
if
is_bool
:
if
is_bool
:
d
[
1
]
=
(
val1
==
".T."
)
d
[
1
]
=
(
val1
==
".T."
)
nml_string
=
string
.
format
(
val_str
)
valid_String
=
""
nml
=
namelist
.
parse_namelist_string
(
nml_string
)[
"nml"
]
for
k
,
v
in
d
.
items
():
assert
nml_string
==
str
(
nml
)
valid_String
+=
"val({}) = {}
\n
"
.
format
(
k
,
repr
(
v
))
valid_String
=
string
.
format
(
valid_String
)
print
(
valid_String
,
nml_string
,
str
(
nml
))
print
(
is_expression
,
is_bool
)
assert
valid_String
==
str
(
nml
)
@
pytest
.
mark
.
parametrize
(
"string"
,
[
"&nml val= {}, val2='lsl'/"
])
@
pytest
.
mark
.
parametrize
(
"string"
,
[
"&nml val= {}, val2='lsl'/"
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment