Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
D
DAN
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Container Registry
Operate
Terraform modules
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Automatic Text Recognition
DAN
Commits
1ffa8321
Verified
Commit
1ffa8321
authored
1 year ago
by
Yoann Schneider
Browse files
Options
Downloads
Patches
Plain Diff
Check tokens configuration
parent
593c9402
No related branches found
No related tags found
1 merge request
!394
Check tokens configuration
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dan/utils.py
+12
-1
12 additions, 1 deletion
dan/utils.py
tests/test_utils.py
+39
-0
39 additions, 0 deletions
tests/test_utils.py
with
51 additions
and
1 deletion
dan/utils.py
+
12
−
1
View file @
1ffa8321
...
...
@@ -2,6 +2,7 @@
import
json
from
argparse
import
ArgumentTypeError
from
itertools
import
islice
from
operator
import
attrgetter
from
pathlib
import
Path
from
typing
import
Dict
,
NamedTuple
...
...
@@ -131,10 +132,20 @@ def list_to_batches(iterable, n):
def
parse_tokens
(
filename
:
str
)
->
Dict
[
str
,
EntityType
]:
return
{
tokens
=
{
name
:
EntityType
(
**
tokens
)
for
name
,
tokens
in
yaml
.
safe_load
(
Path
(
filename
).
read_text
()).
items
()
}
end_tokens
=
map
(
attrgetter
(
"
end
"
),
tokens
.
values
())
# Check that either
if
next
(
end_tokens
):
# - all entities have end token
assert
all
(
end_tokens
),
"
Some entities have no end token
"
else
:
# - no entities have end tokens
assert
not
any
(
end_tokens
),
"
Some entities have end tokens
"
return
tokens
def
read_yaml
(
yaml_path
:
str
)
->
Dict
:
...
...
This diff is collapsed.
Click to expand it.
tests/test_utils.py
0 → 100644
+
39
−
0
View file @
1ffa8321
import
pytest
import
yaml
from
dan.utils
import
parse_tokens
@pytest.mark.parametrize
(
(
"
tokens
"
,
"
error_msg
"
),
[
# All should have no end tokens
(
{
"
name
"
:
{
"
start
"
:
"
A
"
,
"
end
"
:
""
,
},
"
surname
"
:
{
"
start
"
:
"
B
"
,
"
end
"
:
"
C
"
},
},
"
Some entities have end tokens
"
,
),
# All should have end tokens
(
{
"
name
"
:
{
"
start
"
:
"
A
"
,
"
end
"
:
"
C
"
,
},
"
surname
"
:
{
"
start
"
:
"
B
"
,
"
end
"
:
""
},
},
"
Some entities have no end token
"
,
),
],
)
def
test_parse_tokens_errors
(
tmp_path
,
tokens
,
error_msg
):
tokens_path
=
tmp_path
/
"
tokens.yml
"
tokens_path
.
write_text
(
yaml
.
dump
(
tokens
))
with
pytest
.
raises
(
AssertionError
,
match
=
error_msg
):
parse_tokens
(
tokens_path
)
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