Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
K
km3io
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
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
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
km3py
km3io
Commits
b8136331
Commit
b8136331
authored
5 years ago
by
Tamas Gal
Browse files
Options
Downloads
Patches
Plain Diff
Simplify header
parent
bda30b67
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!27
Refactor offline I/O
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
km3io/offline.py
+11
-9
11 additions, 9 deletions
km3io/offline.py
tests/test_offline.py
+42
-5
42 additions, 5 deletions
tests/test_offline.py
with
53 additions
and
14 deletions
km3io/offline.py
+
11
−
9
View file @
b8136331
...
...
@@ -226,23 +226,25 @@ class Header:
"""
The header
"""
def
__init__
(
self
,
header
):
self
.
_data
=
{}
self
.
_missing_keys
=
set
(
header
.
keys
())
-
set
(
mc_header
.
keys
())
for
attribute
,
fields
in
mc_header
.
items
():
values
=
header
.
get
(
attribute
,
''
).
split
()
for
attribute
,
fields
in
header
.
items
():
values
=
fields
.
split
()
fields
=
mc_header
.
get
(
attribute
,
[])
n_values
=
len
(
values
)
n_fields
=
len
(
fields
)
n_max
=
max
(
n_values
,
n_fields
)
values
+=
[
None
]
*
(
n_max
-
n_values
)
fields
+=
[
"
field_{}
"
.
format
(
i
)
for
i
in
range
(
n_fields
,
n_max
)]
if
not
values
:
continue
Constructor
=
namedtuple
(
attribute
,
fields
)
if
len
(
values
)
<
len
(
fields
):
values
+=
[
None
]
*
(
len
(
fields
)
-
len
(
values
))
self
.
_data
[
attribute
]
=
Constructor
(
**
{
f
:
_to_num
(
v
)
for
(
f
,
v
)
in
zip
(
fields
,
values
)})
# quick fix while waiting for additional definitions in mc_header
for
key
in
self
.
_missing_keys
:
self
.
_data
[
key
]
=
_to_num
(
header
[
key
])
for
attribute
,
value
in
self
.
_data
.
items
():
setattr
(
self
,
attribute
,
value
)
...
...
This diff is collapsed.
Click to expand it.
tests/test_offline.py
+
42
−
5
View file @
b8136331
...
...
@@ -36,18 +36,55 @@ class TestHeader(unittest.TestCase):
with
self
.
assertWarns
(
UserWarning
):
OFFLINE_FILE
.
header
def
test_header
_wrapper
(
self
):
def
test_header
(
self
):
head
=
{
'
DAQ
'
:
'
394
'
,
'
PDF
'
:
'
4
'
,
'
XSecFile
'
:
'
'
,
'
can
'
:
'
0 1027 888
.4
'
'
can
'
:
'
0 1027 888.4
'
,
'
undefined
'
:
'
1 2 test 3
.4
'
}
header
=
Header
(
head
)
self
.
assertEqual
(
len
(
header
.
_data
),
len
(
head
))
self
.
assertIsNone
(
header
.
_data
[
"
PDF
"
].
i2
)
assert
394
==
header
.
DAQ
.
livetime
assert
4
==
header
.
PDF
.
i1
assert
header
.
PDF
.
i2
is
None
assert
0
==
header
.
can
.
zmin
assert
1027
==
header
.
can
.
zmax
assert
888.4
==
header
.
can
.
r
assert
1
==
header
.
undefined
.
field_0
assert
2
==
header
.
undefined
.
field_1
assert
"
test
"
==
header
.
undefined
.
field_2
assert
3.4
==
header
.
undefined
.
field_3
def
test_missing_key_definitions
(
self
):
head
=
{
'
a
'
:
'
1 2 3
'
,
'
b
'
:
'
4
'
}
header
=
Header
(
head
)
assert
1
==
header
.
a
.
field_0
assert
2
==
header
.
a
.
field_1
assert
3
==
header
.
a
.
field_2
assert
4
==
header
.
b
.
field_0
def
test_missing_values
(
self
):
head
=
{
'
can
'
:
'
1
'
}
header
=
Header
(
head
)
assert
1
==
header
.
can
.
zmin
assert
header
.
can
.
zmax
is
None
assert
header
.
can
.
r
is
None
def
test_additional_values_compared_to_definition
(
self
):
head
=
{
'
can
'
:
'
1 2 3 4
'
}
header
=
Header
(
head
)
assert
1
==
header
.
can
.
zmin
assert
2
==
header
.
can
.
zmax
assert
3
==
header
.
can
.
r
assert
4
==
header
.
can
.
field_3
class
TestOfflineEvents
(
unittest
.
TestCase
):
...
...
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