Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
snf-ganeti
Commits
f7414041
Commit
f7414041
authored
Nov 20, 2007
by
Michael Hanselmann
Browse files
Add function to return list with unique elements.
Reviewed-by: ultrotter
parent
7d7609a3
Changes
2
Show whitespace changes
Inline
Side-by-side
lib/utils.py
View file @
f7414041
...
@@ -1002,3 +1002,12 @@ def any(seq, pred=bool):
...
@@ -1002,3 +1002,12 @@ def any(seq, pred=bool):
for
elem
in
itertools
.
ifilter
(
pred
,
seq
):
for
elem
in
itertools
.
ifilter
(
pred
,
seq
):
return
True
return
True
return
False
return
False
def
UniqueSequence
(
seq
):
"""Returns a list with unique elements.
Element order is preserved.
"""
seen
=
set
()
return
[
i
for
i
in
seq
if
i
not
in
seen
and
not
seen
.
add
(
i
)]
test/ganeti.utils_unittest.py
View file @
f7414041
...
@@ -608,5 +608,28 @@ class TestNewUUID(unittest.TestCase):
...
@@ -608,5 +608,28 @@ class TestNewUUID(unittest.TestCase):
self
.
failUnless
(
self
.
_re_uuid
.
match
(
utils
.
NewUUID
()))
self
.
failUnless
(
self
.
_re_uuid
.
match
(
utils
.
NewUUID
()))
class
TestUniqueSequence
(
unittest
.
TestCase
):
"""Test case for UniqueSequence"""
def
_test
(
self
,
input
,
expected
):
self
.
assertEqual
(
utils
.
UniqueSequence
(
input
),
expected
)
def
runTest
(
self
):
# Ordered input
self
.
_test
([
1
,
2
,
3
],
[
1
,
2
,
3
])
self
.
_test
([
1
,
1
,
2
,
2
,
3
,
3
],
[
1
,
2
,
3
])
self
.
_test
([
1
,
2
,
2
,
3
],
[
1
,
2
,
3
])
self
.
_test
([
1
,
2
,
3
,
3
],
[
1
,
2
,
3
])
# Unordered input
self
.
_test
([
1
,
2
,
3
,
1
,
2
,
3
],
[
1
,
2
,
3
])
self
.
_test
([
1
,
1
,
2
,
3
,
3
,
1
,
2
],
[
1
,
2
,
3
])
# Strings
self
.
_test
([
"a"
,
"a"
],
[
"a"
])
self
.
_test
([
"a"
,
"b"
],
[
"a"
,
"b"
])
self
.
_test
([
"a"
,
"b"
,
"a"
],
[
"a"
,
"b"
])
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment