Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
utils
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ammsml
utils
Commits
686a8a4f
Commit
686a8a4f
authored
5 years ago
by
Christof Schulze
Browse files
Options
Downloads
Patches
Plain Diff
added path.split_path
parent
452f7cf9
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
path.py
+26
-0
26 additions, 0 deletions
path.py
tests/test_path/test_path_split.py
+26
-0
26 additions, 0 deletions
tests/test_path/test_path_split.py
with
52 additions
and
0 deletions
path.py
+
26
−
0
View file @
686a8a4f
...
...
@@ -134,3 +134,29 @@ def cleanup_tmp_file(path, warn=False):
display
.
display
(
'
Unable to remove temporary file {0}
'
.
format
(
to_text
(
e
)))
except
Exception
:
pass
def
split_path
(
path
:
str
)
->
list
:
"""
Returns all elements of a path separated in its elements.
:param path:
:return: list of elements of a path.
"""
split_path
.
parts
=
[]
def
_split
(
path
:
str
):
rough_split
=
os
.
path
.
split
(
path
)
cleared_split
=
list
(
filter
(
None
,
rough_split
))
if
len
(
rough_split
)
!=
len
(
cleared_split
):
if
len
(
cleared_split
[
0
])
==
len
(
path
):
split_path
.
parts
.
append
(
cleared_split
[
0
])
else
:
_split
(
cleared_split
[
0
])
else
:
split_path
.
parts
.
append
(
cleared_split
[
1
])
_split
(
cleared_split
[
0
])
_split
(
path
)
return
list
(
reversed
(
split_path
.
parts
))
This diff is collapsed.
Click to expand it.
tests/test_path/test_path_split.py
0 → 100644
+
26
−
0
View file @
686a8a4f
import
unittest
from
ammsml.utils.path
import
split_path
class
SimpleSplitTest
(
unittest
.
TestCase
):
def
test_single
(
self
):
test_list1
=
[
'
/tmp/test
'
]
for
i
in
test_list1
:
self
.
assertEqual
(
split_path
(
i
),
[
'
/
'
,
'
tmp
'
,
'
test
'
])
def
test_single_end
(
self
):
test_list2
=
[
'
/tmp/test/
'
]
for
i
in
test_list2
:
self
.
assertEqual
(
split_path
(
i
),
[
'
/
'
,
'
tmp
'
,
'
test
'
])
def
test_single_end
(
self
):
test_list3
=
[
'
/tmp/test
'
,
'
raw
'
,
'
test2
'
,
'
test3/test31
'
,
'
test3/test32/test321
'
]
self
.
assertEqual
(
split_path
(
test_list3
[
0
]),
[
'
/
'
,
'
tmp
'
,
'
test
'
])
self
.
assertEqual
(
split_path
(
test_list3
[
1
]),
[
'
raw
'
])
self
.
assertEqual
(
split_path
(
test_list3
[
2
]),
[
'
test2
'
])
self
.
assertEqual
(
split_path
(
test_list3
[
3
]),
[
'
test3
'
,
'
test31
'
])
self
.
assertEqual
(
split_path
(
test_list3
[
4
]),
[
'
test3
'
,
'
test32
'
,
'
test321
'
])
if
__name__
==
'
__main__
'
:
unittest
.
main
()
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