Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Spack
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
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
eic_tools
Spack
Commits
780a5736
Commit
780a5736
authored
9 years ago
by
Massimiliano Culpo
Browse files
Options
Downloads
Patches
Plain Diff
version : modified __getitem__ to return either an item or a Version instance + unit tests
parent
32e086f4
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
bin/spack
+1
-1
1 addition, 1 deletion
bin/spack
lib/spack/spack/test/versions.py
+34
-6
34 additions, 6 deletions
lib/spack/spack/test/versions.py
lib/spack/spack/version.py
+16
-1
16 additions, 1 deletion
lib/spack/spack/version.py
with
51 additions
and
8 deletions
bin/spack
+
1
−
1
View file @
780a5736
...
@@ -77,7 +77,7 @@ import llnl.util.tty as tty
...
@@ -77,7 +77,7 @@ import llnl.util.tty as tty
from
llnl.util.tty.color
import
*
from
llnl.util.tty.color
import
*
import
spack
import
spack
from
spack.error
import
SpackError
from
spack.error
import
SpackError
import
argparse
from
external
import
argparse
# Command parsing
# Command parsing
parser
=
argparse
.
ArgumentParser
(
parser
=
argparse
.
ArgumentParser
(
...
...
This diff is collapsed.
Click to expand it.
lib/spack/spack/test/versions.py
+
34
−
6
View file @
780a5736
...
@@ -391,9 +391,37 @@ def test_formatted_strings(self):
...
@@ -391,9 +391,37 @@ def test_formatted_strings(self):
self
.
assertEqual
(
v
.
underscored
,
'
1_2_3
'
)
self
.
assertEqual
(
v
.
underscored
,
'
1_2_3
'
)
def
test_repr_and_str
(
self
):
def
test_repr_and_str
(
self
):
a
=
Version
(
'
1.2.3
'
)
self
.
assertEqual
(
repr
(
a
),
'
Version(
\'
1.2.3
\'
)
'
)
def
check_repr_and_str
(
vrs
):
b
=
eval
(
repr
(
a
))
a
=
Version
(
vrs
)
self
.
assertEqual
(
a
,
b
)
self
.
assertEqual
(
repr
(
a
),
'
Version(
\'
'
+
vrs
+
'
\'
)
'
)
self
.
assertEqual
(
str
(
a
),
'
1.2.3
'
)
b
=
eval
(
repr
(
a
))
self
.
assertEqual
(
str
(
a
),
str
(
b
))
self
.
assertEqual
(
a
,
b
)
self
.
assertEqual
(
str
(
a
),
vrs
)
self
.
assertEqual
(
str
(
a
),
str
(
b
))
check_repr_and_str
(
'
1.2.3
'
)
check_repr_and_str
(
'
R2016a
'
)
check_repr_and_str
(
'
R2016a.2-3_4
'
)
def
test_get_item
(
self
):
a
=
Version
(
'
0.1_2-3
'
)
self
.
assertTrue
(
isinstance
(
a
[
1
],
int
))
# Test slicing
b
=
a
[
0
:
2
]
self
.
assertTrue
(
isinstance
(
b
,
Version
))
self
.
assertEqual
(
b
,
Version
(
'
0.1
'
))
self
.
assertEqual
(
repr
(
b
),
'
Version(
\'
0.1
\'
)
'
)
self
.
assertEqual
(
str
(
b
),
'
0.1
'
)
b
=
a
[
0
:
3
]
self
.
assertTrue
(
isinstance
(
b
,
Version
))
self
.
assertEqual
(
b
,
Version
(
'
0.1_2
'
))
self
.
assertEqual
(
repr
(
b
),
'
Version(
\'
0.1_2
\'
)
'
)
self
.
assertEqual
(
str
(
b
),
'
0.1_2
'
)
b
=
a
[
1
:]
self
.
assertTrue
(
isinstance
(
b
,
Version
))
self
.
assertEqual
(
b
,
Version
(
'
1_2-3
'
))
self
.
assertEqual
(
repr
(
b
),
'
Version(
\'
1_2-3
\'
)
'
)
self
.
assertEqual
(
str
(
b
),
'
1_2-3
'
)
# Raise TypeError on tuples
self
.
assertRaises
(
TypeError
,
b
.
__getitem__
,
1
,
2
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
lib/spack/spack/version.py
+
16
−
1
View file @
780a5736
...
@@ -44,6 +44,7 @@
...
@@ -44,6 +44,7 @@
concrete
concrete
"""
"""
import
re
import
re
import
numbers
from
bisect
import
bisect_left
from
bisect
import
bisect_left
from
functools
import
wraps
from
functools
import
wraps
...
@@ -194,7 +195,21 @@ def __iter__(self):
...
@@ -194,7 +195,21 @@ def __iter__(self):
return
iter
(
self
.
version
)
return
iter
(
self
.
version
)
def
__getitem__
(
self
,
idx
):
def
__getitem__
(
self
,
idx
):
return
tuple
(
self
.
version
[
idx
])
cls
=
type
(
self
)
if
isinstance
(
idx
,
numbers
.
Integral
):
return
self
.
version
[
idx
]
elif
isinstance
(
idx
,
slice
):
# Currently len(self.separators) == len(self.version) - 1
extendend_separators
=
self
.
separators
+
(
''
,)
string_arg
=
[]
for
token
,
separator
in
zip
(
self
.
version
,
extendend_separators
)[
idx
]:
string_arg
.
append
(
str
(
token
))
string_arg
.
append
(
str
(
separator
))
string_arg
.
pop
()
# We don't need the last separator
string_arg
=
''
.
join
(
string_arg
)
return
cls
(
string_arg
)
message
=
'
{cls.__name__} indices must be integers
'
raise
TypeError
(
message
.
format
(
cls
=
cls
))
def
__repr__
(
self
):
def
__repr__
(
self
):
return
'
Version(
'
+
repr
(
self
.
string
)
+
'
)
'
return
'
Version(
'
+
repr
(
self
.
string
)
+
'
)
'
...
...
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