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
34346f84
Commit
34346f84
authored
4 years ago
by
Tamas Gal
Browse files
Options
Downloads
Patches
Plain Diff
Add unfold indices routine
parent
b0bae2f3
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/tools.py
+12
-0
12 additions, 0 deletions
km3io/tools.py
tests/test_tools.py
+18
-1
18 additions, 1 deletion
tests/test_tools.py
with
30 additions
and
1 deletion
km3io/tools.py
+
12
−
0
View file @
34346f84
...
...
@@ -19,6 +19,18 @@ class cached_property:
return
prop
def
_unfold_indices
(
obj
,
indices
):
"""
Unfolds an index chain and returns the corresponding item
"""
for
depth
,
idx
in
enumerate
(
indices
):
try
:
obj
=
obj
[
idx
]
except
IndexError
:
print
(
"
IndexError while accessing item
'
{}
'
at depth {} ({}) of
"
"
the index chain {}
"
.
format
(
repr
(
obj
),
depth
,
idx
,
indices
))
raise
return
obj
BranchMapper
=
namedtuple
(
"
BranchMapper
"
,
[
'
name
'
,
'
key
'
,
'
extra
'
,
'
exclude
'
,
'
update
'
,
'
attrparser
'
,
'
flat
'
])
...
...
This diff is collapsed.
Click to expand it.
tests/test_tools.py
+
18
−
1
View file @
34346f84
#!/usr/bin/env python3
import
unittest
from
km3io.tools
import
_to_num
,
cached_property
from
km3io.tools
import
_to_num
,
cached_property
,
_unfold_indices
class
TestToNum
(
unittest
.
TestCase
):
...
...
@@ -20,3 +20,20 @@ class TestCachedProperty(unittest.TestCase):
pass
self
.
assertTrue
(
isinstance
(
Test
.
prop
,
cached_property
))
class
TestUnfoldIndices
(
unittest
.
TestCase
):
def
test_unfold_indices
(
self
):
data
=
range
(
10
)
indices
=
[
slice
(
2
,
5
),
0
]
assert
data
[
indices
[
0
]][
indices
[
1
]]
==
_unfold_indices
(
data
,
indices
)
indices
=
[
slice
(
1
,
9
,
2
),
slice
(
1
,
4
),
2
]
assert
data
[
indices
[
0
]][
indices
[
1
]][
indices
[
2
]]
==
_unfold_indices
(
data
,
indices
)
def
test_unfold_indices_raises_index_error
(
self
):
data
=
range
(
10
)
indices
=
[
slice
(
2
,
5
),
99
]
with
self
.
assertRaises
(
IndexError
):
_unfold_indices
(
data
,
indices
)
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