Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
CaosDB Crawler
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
caosdb
Software
CaosDB Crawler
Commits
1937ca9a
Commit
1937ca9a
authored
Jan 5, 2024
by
Henrik tom Wörden
Browse files
Options
Downloads
Patches
Plain Diff
MAINT: do not use complex dicts internally
parent
a941bfe9
No related branches found
No related tags found
2 merge requests
!160
STY: styling
,
!126
Transformers
Pipeline
#45673
failed
Jan 5, 2024
Stage: info
Stage: setup
Stage: cert
Stage: style
Stage: test
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/caoscrawler/converters.py
+15
-15
15 additions, 15 deletions
src/caoscrawler/converters.py
src/caoscrawler/scanner.py
+8
-9
8 additions, 9 deletions
src/caoscrawler/scanner.py
unittests/test_converters.py
+1
-4
1 addition, 4 deletions
unittests/test_converters.py
with
24 additions
and
28 deletions
src/caoscrawler/converters.py
+
15
−
15
View file @
1937ca9a
...
...
@@ -23,6 +23,14 @@
# ** end header
#
"""
TODO place
- The key is the name of the function to be looked up in the dictionary
of registered transformer functions.
- The value is a dictionary with key, value-assignments which will be
passed to the transformer function.
"""
from
__future__
import
annotations
import
datetime
...
...
@@ -376,8 +384,7 @@ class Converter(object, metaclass=ABCMeta):
raise
RuntimeError
(
"
Condition does not match.
"
)
values
.
update
(
m
)
def
apply_transformers
(
self
,
values
:
GeneralStore
,
transformer_functions
:
dict
):
def
apply_transformers
(
self
,
values
:
GeneralStore
,
transformer_functions
:
dict
):
"""
Check if transformers are defined using the
"
transform
"
keyword.
Then apply the transformers to the variables defined in GeneralStore
"
values
"
.
...
...
@@ -390,6 +397,11 @@ class Converter(object, metaclass=ABCMeta):
transformer_functions: dict
A dictionary of registered functions that can be used within this transformer block.
The keys of the dict are the function keys and the values the callable functions of the
form:
def func(in_value: Any, in_parameters: dict) -> Any:
pass
"""
if
"
transform
"
in
self
.
definition
:
...
...
@@ -421,23 +433,11 @@ class Converter(object, metaclass=ABCMeta):
"
of the function!
"
)
tr_func_key
=
list
(
tr_func_el
.
keys
())[
0
]
tr_func_params
=
tr_func_el
[
tr_func_key
]
# These functions are a list of functions that need to be registered
# in the dictionary of registered transformer_functions.
# Each function is a dictionary:
# - The key is the name of the function to be looked up in the dictionary
# of registered transformer functions.
# - The value is a dictionary with key, value-assignments which will be
# passed to the transformer function.
# The transformer function needs to be of the form:
#
# def func(in_value: Any, in_parameters: dict) -> Any:
# pass
#
if
tr_func_key
not
in
transformer_functions
:
raise
RuntimeError
(
"
Unknown transformer function: {}
"
.
format
(
tr_func_key
))
# Retrieve the function from the dictionary:
tr_func
=
transformer_functions
[
tr_func_key
]
[
"
function
"
]
tr_func
=
transformer_functions
[
tr_func_key
]
# Call the function:
out_value
=
tr_func
(
in_value
,
tr_func_params
)
# The next in_value is the current out_value:
...
...
This diff is collapsed.
Click to expand it.
src/caoscrawler/scanner.py
+
8
−
9
View file @
1937ca9a
...
...
@@ -199,12 +199,13 @@ def create_transformer_registry(definition: dict):
# Defaults for the transformer registry:
with
open
(
str
(
files
(
'
caoscrawler
'
).
joinpath
(
'
default_transformers.yml
'
)),
"
r
"
)
as
f
:
transformer_
registry
:
dict
[
str
,
dict
[
str
,
str
]]
=
yaml
.
safe_load
(
f
)
transformer_
def
:
dict
[
str
,
dict
[
str
,
str
]]
=
yaml
.
safe_load
(
f
)
registry
=
{}
# More transformers from definition file:
if
"
Transformers
"
in
definition
:
for
key
,
entry
in
definition
[
"
Transformers
"
].
items
():
transformer_
registry
[
key
]
=
{
transformer_
def
[
key
]
=
{
"
function
"
:
entry
[
"
function
"
],
"
package
"
:
entry
[
"
package
"
]
}
...
...
@@ -212,8 +213,8 @@ def create_transformer_registry(definition: dict):
# Load modules and associate classes:
for
key
,
value
in
transformer_registry
.
items
():
module
=
importlib
.
import_module
(
value
[
"
package
"
])
value
[
"
function
"
]
=
getattr
(
module
,
value
[
"
function
"
])
return
transformer_
registry
registry
[
key
]
=
getattr
(
module
,
value
[
"
function
"
])
return
registry
def
initialize_converters
(
crawler_definition
:
dict
,
converter_registry
:
dict
):
...
...
@@ -284,9 +285,7 @@ def scanner(items: list[StructureElement],
Each function is a dictionary:
- The key is the name of the function to be looked up in the dictionary
of registered transformer functions.
- The value is a dictionary with key, value-assignments which will be
passed to the transformer function.
The transformer function needs to be of the form:
- The value is the function which needs to be of the form:
def func(in_value: Any, in_parameters: dict) -> Any:
pass
...
...
This diff is collapsed.
Click to expand it.
unittests/test_converters.py
+
1
−
4
View file @
1937ca9a
...
...
@@ -379,10 +379,7 @@ def test_apply_transformers(converter_registry):
values
[
"
a
"
]
=
"
a|b|c
"
# transformer_functions = create_transformer_registry(crawler_definition)
transformer_functions
=
{
"
split
"
:
{
"
function
"
:
split
,
"
package
"
:
"
caoscrawler.transformer_functions
"
}}
transformer_functions
=
{
"
split
"
:
split
}
conv
=
ListElementConverter
(
definition
=
cfood_def
,
name
=
'
test
'
,
converter_registry
=
converter_registry
)
...
...
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