Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Platform
Development
swh-core
Commits
cbabde9b
Commit
cbabde9b
authored
5 years ago
by
Nicolas Dandrimont
Browse files
Options
Downloads
Patches
Plain Diff
logger: only flatten dicts if all keys are strings
parent
cdf60119
No related branches found
No related tags found
1 merge request
!99
logger: only flatten dicts if all keys are strings
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
swh/core/logger.py
+14
-4
14 additions, 4 deletions
swh/core/logger.py
swh/core/tests/test_logger.py
+10
-0
10 additions, 0 deletions
swh/core/tests/test_logger.py
with
24 additions
and
4 deletions
swh/core/logger.py
+
14
−
4
View file @
cbabde9b
...
...
@@ -5,6 +5,7 @@
import
datetime
import
logging
from
typing
import
Any
,
Generator
,
List
,
Tuple
from
systemd.journal
import
JournalHandler
as
_JournalHandler
,
send
...
...
@@ -52,12 +53,21 @@ def get_extra_data(record, task_args=True):
return
extra_data
def
flatten
(
data
,
separator
=
'
_
'
):
def
flatten
(
data
:
Any
,
separator
:
str
=
"
_
"
)
->
Generator
[
Tuple
[
str
,
Any
],
None
,
None
]:
"""
Flatten the data dictionary into a flat structure
"""
def
inner_flatten
(
data
,
prefix
):
def
inner_flatten
(
data
:
Any
,
prefix
:
List
[
str
]
)
->
Generator
[
Tuple
[
List
[
str
],
Any
],
None
,
None
]:
if
isinstance
(
data
,
dict
):
for
key
,
value
in
data
.
items
():
yield
from
inner_flatten
(
value
,
prefix
+
[
key
])
if
all
(
isinstance
(
key
,
str
)
for
key
in
data
):
for
key
,
value
in
data
.
items
():
yield
from
inner_flatten
(
value
,
prefix
+
[
key
])
else
:
yield
prefix
,
str
(
data
)
elif
isinstance
(
data
,
(
list
,
tuple
)):
for
key
,
value
in
enumerate
(
data
):
yield
from
inner_flatten
(
value
,
prefix
+
[
str
(
key
)])
...
...
This diff is collapsed.
Click to expand it.
swh/core/tests/test_logger.py
+
10
−
0
View file @
cbabde9b
...
...
@@ -69,6 +69,16 @@ def test_flatten_dict():
]
def
test_flatten_dict_binary_keys
():
d
=
{
b
"
a
"
:
"
a
"
}
str_d
=
str
(
d
)
assert
list
(
logger
.
flatten
(
d
))
==
[(
""
,
str_d
)]
assert
list
(
logger
.
flatten
({
"
a
"
:
d
}))
==
[(
"
a
"
,
str_d
)]
assert
list
(
logger
.
flatten
({
"
a
"
:
[
d
,
d
]}))
==
[
(
"
a_0
"
,
str_d
),
(
"
a_1
"
,
str_d
)
]
def
test_stringify
():
assert
logger
.
stringify
(
None
)
==
'
None
'
assert
logger
.
stringify
(
123
)
==
'
123
'
...
...
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