Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
nomad-lab
python-common
Commits
f3ca80af
Commit
f3ca80af
authored
7 years ago
by
Berk Onat
Browse files
Options
Downloads
Patches
Plain Diff
Fixed numpy array update at MetaInfoStorage and string to list conversion at SmartParser
parent
7e66bd52
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
common/python/nomadcore/metainfo_storage/MetaInfoStorage.py
+96
-30
96 additions, 30 deletions
common/python/nomadcore/metainfo_storage/MetaInfoStorage.py
common/python/nomadcore/smart_parser/SmartParserCommon.py
+56
-5
56 additions, 5 deletions
common/python/nomadcore/smart_parser/SmartParserCommon.py
with
152 additions
and
35 deletions
common/python/nomadcore/metainfo_storage/MetaInfoStorage.py
+
96
−
30
View file @
f3ca80af
...
@@ -5,6 +5,7 @@ import logging
...
@@ -5,6 +5,7 @@ import logging
import
json
import
json
import
os
import
os
import
re
import
re
import
ast
from
collections
import
namedtuple
from
collections
import
namedtuple
COMMON_META_INFO_PATH
=
os
.
path
.
normpath
(
os
.
path
.
join
(
COMMON_META_INFO_PATH
=
os
.
path
.
normpath
(
os
.
path
.
join
(
...
@@ -15,7 +16,60 @@ PUBLIC_META_INFO_PATH = os.path.normpath(os.path.join(
...
@@ -15,7 +16,60 @@ PUBLIC_META_INFO_PATH = os.path.normpath(os.path.join(
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)),
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)),
"
../../../../../nomad-meta-info/meta_info/nomad_meta_info/public.nomadmetainfo.json
"
))
"
../../../../../nomad-meta-info/meta_info/nomad_meta_info/public.nomadmetainfo.json
"
))
NOTEXCEPT
=
re
.
compile
(
r
'
[^a-cf-zA-CF-Z!\.?,]
'
)
NOTEXCEPT
=
re
.
compile
(
r
'
[a-cf-zA-CF-Z!\?,{}\[\]]
'
)
def
is_number
(
val
):
try
:
float
(
val
)
return
True
except
ValueError
:
return
False
def
strcleaner
(
val
):
unwantedkeys
=
[
"
system
"
,
"
eval
"
,
"
return
"
,
"
ctypes
"
,
"
setup
"
,
"
import
"
,
"
git
"
,
"
swig
"
,
"
cython
"
]
for
keyword
in
unwantedkeys
:
val
=
val
.
replace
(
keyword
,
''
)
return
val
def
strisinstance
(
val
,
typ
):
typlist
=
[]
if
isinstance
(
typ
,
str
):
typlist
.
append
(
typ
)
elif
isinstance
(
typ
,
(
tuple
,
list
)):
for
ty
in
typ
:
typlist
.
append
(
ty
)
anytype
=
None
for
t
in
typlist
:
try
:
if
(
"
list
"
in
t
and
"
[
"
in
val
and
"
]
"
in
val
and
"
,
"
in
val
):
if
isinstance
(
literal_eval
(
strcleaner
(
val
)),
list
):
anytype
=
"
list
"
break
elif
isinstance
(
literal_eval
(
strcleaner
(
val
)),
np
.
ndarray
):
anytype
=
"
np.ndarray
"
break
elif
(
"
tuple
"
in
t
and
"
(
"
in
val
and
"
)
"
in
val
and
"
,
"
in
val
):
if
isinstance
(
literal_eval
(
strcleaner
(
val
)),
tuple
):
anytype
=
"
tuple
"
break
except
(
TypeError
,
ValueError
,
AttributeError
):
pass
return
anytype
def
literal_eval
(
val
):
try
:
return
ast
.
literal_eval
(
val
)
except
(
TypeError
,
ValueError
):
return
val
class
Container
(
object
):
class
Container
(
object
):
"""
The container class for nested data storage
"""
The container class for nested data storage
...
@@ -299,18 +353,31 @@ class Container(object):
...
@@ -299,18 +353,31 @@ class Container(object):
return
storeValue
,
updateValue
,
localdict
return
storeValue
,
updateValue
,
localdict
def
convertToNumber
(
self
,
updateValue
,
valtype
):
def
convertToNumber
(
self
,
updateValue
,
valtype
):
acceptvals
=
[
"
float
"
,
"
int
"
,
"
list
"
,
"
tuple
"
,
"
np.asarray
"
,
"
np.array
"
,
"
set
"
,
"
boolean
"
,
"
bytes
"
]
if
valtype
in
acceptvals
:
if
(
isinstance
(
updateValue
,
list
)
or
isinstance
(
updateValue
,
tuple
)):
if
(
isinstance
(
updateValue
,
list
)
or
isinstance
(
updateValue
,
tuple
)):
try
:
newUpdateValue
=
[
eval
(
newUpdateValue
=
[
eval
(
valtype
+
"
(
"
+
str
(
ival
)
+
"
)
"
valtype
+
"
(
"
+
literal_eval
(
str
(
ival
)
)
+
"
)
"
)
for
ival
in
updateValue
]
)
for
ival
in
updateValue
]
except
(
TypeError
,
ValueError
):
newUpdateValue
=
updateValue
elif
isinstance
(
updateValue
,
np
.
ndarray
):
elif
isinstance
(
updateValue
,
np
.
ndarray
):
try
:
newUpdateValue
=
np
.
asarray
([
eval
(
newUpdateValue
=
np
.
asarray
([
eval
(
valtype
+
"
(
"
+
str
(
ival
)
+
"
)
"
valtype
+
"
(
"
+
literal_eval
(
str
(
ival
)
)
+
"
)
"
)
for
ival
in
updateValue
])
)
for
ival
in
updateValue
])
elif
self
.
is_number
(
updateValue
):
except
(
TypeError
,
ValueError
):
newUpdateValue
=
updateValue
elif
is_number
(
updateValue
):
newUpdateValue
=
eval
(
valtype
+
"
(
"
+
str
(
updateValue
)
+
"
)
"
)
newUpdateValue
=
eval
(
valtype
+
"
(
"
+
str
(
updateValue
)
+
"
)
"
)
else
:
else
:
# I hope you know what you are doing
# I hope you know what you are doing
try
:
newUpdateValue
=
float
(
updateValue
)
except
(
TypeError
,
ValueError
):
newUpdateValue
=
updateValue
newUpdateValue
=
updateValue
return
newUpdateValue
return
newUpdateValue
...
@@ -322,17 +389,20 @@ class Container(object):
...
@@ -322,17 +389,20 @@ class Container(object):
elif
isinstance
(
updateValue
,
np
.
ndarray
):
elif
isinstance
(
updateValue
,
np
.
ndarray
):
updateValue
=
updateValue
*
self
.
unitConverter
(
updateValue
=
updateValue
*
self
.
unitConverter
(
unit
,
unitdict
)
unit
,
unitdict
)
#updateValue = np.asarray([self.unitConverter(
elif
is_number
(
updateValue
):
# ival, unit, unitdict
# ) for ival in updateValue])
elif
self
.
is_number
(
updateValue
):
updateValue
=
self
.
convertToNumber
(
updateValue
,
"
float
"
)
updateValue
=
self
.
convertToNumber
(
updateValue
,
"
float
"
)
if
updateValue
:
updateValue
=
updateValue
*
self
.
unitConverter
(
updateValue
=
updateValue
*
self
.
unitConverter
(
unit
,
unitdict
)
unit
,
unitdict
)
el
se
:
el
if
isinstance
(
updateValue
,
str
)
:
# I hope you know what you are doing
# I hope you know what you are doing
updateValue
=
float
(
updateValue
)
*
self
.
unitConverter
(
try
:
newUpdateVal
=
strcleaner
(
updateValue
)
newUpdateVal
=
NOTEXCEPT
.
sub
(
''
,
newUpdateVal
)
updateValue
=
float
(
newUpdateVal
)
*
self
.
unitConverter
(
unit
,
unitdict
)
unit
,
unitdict
)
except
(
TypeError
,
ValueError
):
pass
return
updateValue
return
updateValue
def
unitConverter
(
self
,
unit
,
unitdict
):
def
unitConverter
(
self
,
unit
,
unitdict
):
...
@@ -352,8 +422,11 @@ class Container(object):
...
@@ -352,8 +422,11 @@ class Container(object):
newunit
=
newunit
.
replace
(
'
-
'
,
'
*
'
).
replace
(
'
'
,
'
*
'
).
replace
(
'
^
'
,
"
**
"
)
newunit
=
newunit
.
replace
(
'
-
'
,
'
*
'
).
replace
(
'
'
,
'
*
'
).
replace
(
'
^
'
,
"
**
"
)
for
key
,
value
in
unitdict
.
items
():
for
key
,
value
in
unitdict
.
items
():
newunit
=
newunit
.
replace
(
str
(
key
),
str
(
value
))
newunit
=
newunit
.
replace
(
str
(
key
),
str
(
value
))
NOTEXCEPT
.
sub
(
''
,
newunit
)
newunit
=
NOTEXCEPT
.
sub
(
''
,
newunit
)
try
:
return
float
(
eval
(
newunit
))
return
float
(
eval
(
newunit
))
except
(
ValueError
,
TypeError
):
return
None
def
checkTestsDicts
(
self
,
item
,
localdict
):
def
checkTestsDicts
(
self
,
item
,
localdict
):
for
depdict
in
item
[
"
depends
"
]:
for
depdict
in
item
[
"
depends
"
]:
...
@@ -519,13 +592,6 @@ class Container(object):
...
@@ -519,13 +592,6 @@ class Container(object):
newValue
=
itemv
[
"
unitconverter
"
](
self
,
itemv
)
newValue
=
itemv
[
"
unitconverter
"
](
self
,
itemv
)
self
.
Storage
.
__dict__
[
itemk
[
"
val
"
]]
=
newvalue
self
.
Storage
.
__dict__
[
itemk
[
"
val
"
]]
=
newvalue
def
is_number
(
self
,
val
):
try
:
float
(
val
)
return
True
except
ValueError
:
return
False
def
__str__
(
self
,
caller
=
None
,
decorate
=
''
,
color
=
None
,
printactive
=
None
,
onlynames
=
None
):
def
__str__
(
self
,
caller
=
None
,
decorate
=
''
,
color
=
None
,
printactive
=
None
,
onlynames
=
None
):
string
=
''
string
=
''
if
onlynames
is
None
:
if
onlynames
is
None
:
...
...
This diff is collapsed.
Click to expand it.
common/python/nomadcore/smart_parser/SmartParserCommon.py
+
56
−
5
View file @
f3ca80af
...
@@ -6,6 +6,7 @@ from nomadcore.simple_parser import mainFunction
...
@@ -6,6 +6,7 @@ from nomadcore.simple_parser import mainFunction
from
nomadcore.simple_parser
import
SimpleMatcher
as
SM
from
nomadcore.simple_parser
import
SimpleMatcher
as
SM
from
nomadcore.metainfo_storage.MetaInfoStorage
import
COMMON_META_INFO_PATH
,
PUBLIC_META_INFO_PATH
from
nomadcore.metainfo_storage.MetaInfoStorage
import
COMMON_META_INFO_PATH
,
PUBLIC_META_INFO_PATH
import
nomadcore.metainfo_storage.MetaInfoStorage
as
mStore
import
nomadcore.metainfo_storage.MetaInfoStorage
as
mStore
from
nomadcore.metainfo_storage.MetaInfoStorage
import
strcleaner
,
strisinstance
,
literal_eval
from
nomadcore.smart_parser.SmartParserDictionary
import
getDict_MetaStrInDict
,
getList_MetaStrInDict
,
get_unitDict
from
nomadcore.smart_parser.SmartParserDictionary
import
getDict_MetaStrInDict
,
getList_MetaStrInDict
,
get_unitDict
from
contextlib
import
contextmanager
from
contextlib
import
contextmanager
import
numpy
as
np
import
numpy
as
np
...
@@ -150,6 +151,9 @@ class ParserBase(object):
...
@@ -150,6 +151,9 @@ class ParserBase(object):
#r"^\s*%\s*%\s*$",
#r"^\s*%\s*%\s*$",
]
]
self
.
coverageIgnore
=
None
self
.
coverageIgnore
=
None
self
.
strcleaner
=
strcleaner
self
.
strisinstance
=
strisinstance
self
.
literal_eval
=
literal_eval
def
parse
(
self
):
def
parse
(
self
):
self
.
coverageIgnore
=
re
.
compile
(
r
"
^(?:
"
+
r
"
|
"
.
join
(
self
.
coverageIgnoreList
)
+
r
"
)$
"
)
self
.
coverageIgnore
=
re
.
compile
(
r
"
^(?:
"
+
r
"
|
"
.
join
(
self
.
coverageIgnoreList
)
+
r
"
)$
"
)
...
@@ -983,29 +987,76 @@ class ParserBase(object):
...
@@ -983,29 +987,76 @@ class ParserBase(object):
matchWith
=
mNameDict
[
key
].
matchWith
matchWith
=
mNameDict
[
key
].
matchWith
else
:
else
:
matchWith
=
''
matchWith
=
''
if
mNameDict
[
key
].
replaceDict
:
replaceDict
=
mNameDict
[
key
].
replaceDict
else
:
replaceDict
=
None
if
mNameDict
[
key
].
subFunc
:
if
isinstance
(
subFunc
,
str
):
subFunc
=
getattr
(
self
,
mNameDict
[
key
].
subFunc
)
else
:
subFunc
=
mNameDict
[
key
].
subFunc
else
:
subFunc
=
None
if
mNameDict
[
key
].
addAsList
:
addAsList
=
mNameDict
[
key
].
addAsList
else
:
addAsList
=
None
if
mNameDict
[
key
].
appendToList
:
appendToList
=
getattr
(
self
,
mNameDict
[
key
].
appendToList
)
else
:
appendToList
=
None
if
'
EOL
'
in
matchWith
:
if
'
EOL
'
in
matchWith
:
matchThisParsy
=
re
.
compile
(
r
"
(?:%s)\s*(?:=|:)\s*(?:
'
|\")?
"
matchThisParsy
=
re
.
compile
(
r
"
(?:%s)\s*(?:=|:)\s*(?:
'
|\")?
"
"
(?P<%s>.*)(?:
'
|
\"
)?\s*,?
"
"
(?P<%s>.*)(?:
'
|
\"
)?\s*,?
"
%
(
cText
,
key
))
%
(
cText
,
key
))
elif
'
UD
'
in
matchWith
:
elif
'
UD
'
in
matchWith
:
delimeter
=
matchWith
.
replace
(
'
UD
'
,
''
)
delimeter
=
matchWith
.
replace
(
'
UD
'
,
''
)
matchThisParsy
=
re
.
compile
(
r
"
(?:%s)\s*(?:=|:)\s*(?:
'
|\")?
"
matchThisParsy
=
re
.
compile
(
r
"
(?:%s)\s*(?:
\s|
=|:)\s*(?:
'
|\")?
"
"
(?P<%s>[\-+0-9.a-zA-Z:]+)
"
"
(?P<%s>[\-+0-9.a-zA-Z:]+)
"
"
(?:
'
|
\"
)?\s*[%s]
"
"
(?:
'
|
\"
)?\s*[%s]
"
%
(
cText
,
key
,
delimeter
))
%
(
cText
,
key
,
delimeter
))
else
:
# Default matchWith 'NW'
elif
'
BOL
'
in
matchWith
:
matchThisParsy
=
re
.
compile
(
r
"
(?:%s)\s*(?:=|:)\s*(?:
'
|\"|{|\[|\()?
"
matchThisParsy
=
re
.
compile
(
r
"
(?:
'
|\")?(?P<%s>.*)(?:
'
|\")?
"
"
\s*(?:%s)\s*
"
%
(
key
,
cText
))
elif
'
PW
'
in
matchWith
:
matchThisParsy
=
re
.
compile
(
r
"
(?:
'
|\")?(?P<%s>[\-+0-9.a-zA-Z:]+)
"
"
(?:
'
|
\"
)?\s*(?:%s)\s*
"
%
(
key
,
cText
))
elif
'
FD
'
in
matchWith
:
delimeter
=
matchWith
.
replace
(
'
FD
'
,
''
)
matchThisParsy
=
re
.
compile
(
r
"
[%s]\s*(?:
'
|\")?
"
"
(?P<%s>[\-+0-9.a-zA-Z:]+)
"
"
(?P<%s>[\-+0-9.a-zA-Z:]+)
"
"
(?:
'
|
\"
)?\s*(?:%s)\s*
"
%
(
delimeter
,
key
,
cText
))
else
:
# Default matchWith 'NW'
matchThisParsy
=
re
.
compile
(
r
"
(?:%s)\s*(?:\s|=|:)\s*(?:
'
|\"|{|\[|\()?
"
"
(?P<%s>[\S]+)
"
"
(?:
'
|
\"
|}|\]|\))?\s*
"
"
(?:
'
|
\"
|}|\]|\))?\s*
"
%
(
cText
,
key
))
%
(
cText
,
key
))
#"(?P<%s>[\-+0-9.a-zA-Z:_/;]+)"
reDict
=
{
key
:
value
for
value
in
matchThisParsy
.
findall
(
lastLine
)}
reDict
=
{
key
:
value
for
value
in
matchThisParsy
.
findall
(
lastLine
)}
if
reDict
:
if
reDict
:
for
k
,
v
in
reDict
.
items
():
for
k
,
v
in
reDict
.
items
():
if
k
==
key
:
if
k
==
key
:
if
isinstance
(
v
,
str
):
if
isinstance
(
v
,
str
):
v
=
v
.
replace
(
'
{
'
,
'
[
'
).
replace
(
'
}
'
,
'
]
'
)
if
replaceDict
is
not
None
:
for
repK
,
repV
in
replaceDict
.
items
():
v
=
v
.
replace
(
repK
,
repV
)
if
subFunc
is
not
None
:
v
=
subFunc
(
v
)
if
k
in
list
(
parser
.
lastMatch
.
keys
()):
if
k
in
list
(
parser
.
lastMatch
.
keys
()):
parser
.
lastMatch
[
k
]
=
v
parser
.
lastMatch
[
k
]
=
v
else
:
if
addAsList
is
not
None
:
if
mNameDict
[
k
].
value
is
None
:
mNameDict
[
k
].
value
=
[
v
]
else
:
if
isinstance
(
mNameDict
[
k
].
value
,
list
):
mNameDict
[
k
].
value
.
append
(
v
)
else
:
mNameDict
[
k
].
value
=
v
else
:
else
:
mNameDict
[
k
].
value
=
v
mNameDict
[
k
].
value
=
v
mNameDict
[
k
].
activeInfo
=
True
mNameDict
[
k
].
activeInfo
=
True
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment