Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0aeac107
Commit
0aeac107
authored
Jul 05, 2004
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
* Add __eq__ and __ne__ so that things like list.index() work properly
for lists of mixed types. * Test that sort works.
parent
10959b1c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
15 deletions
+26
-15
Lib/decimal.py
Lib/decimal.py
+10
-4
Lib/test/test_decimal.py
Lib/test/test_decimal.py
+16
-11
No files found.
Lib/decimal.py
View file @
0aeac107
...
...
@@ -8,10 +8,6 @@
# and Tim Peters
# Todo:
# Add rich comparisons for equality testing with other types
"""
This is a Py2.3 implementation of decimal floating point arithmetic based on
the General Decimal Arithmetic Specification:
...
...
@@ -644,6 +640,16 @@ class Decimal(object):
return
-
1
return
1
def
__eq__
(
self
,
other
):
if
not
isinstance
(
other
,
(
Decimal
,
int
,
long
)):
return
False
return
self
.
__cmp__
(
other
)
==
0
def
__ne__
(
self
,
other
):
if
not
isinstance
(
other
,
(
Decimal
,
int
,
long
)):
return
True
return
self
.
__cmp__
(
other
)
!=
0
def
compare
(
self
,
other
,
context
=
None
):
"""Compares one to another.
...
...
Lib/test/test_decimal.py
View file @
0aeac107
...
...
@@ -33,6 +33,7 @@ import pickle, copy
from
decimal
import
*
from
test.test_support
import
TestSkipped
,
run_unittest
,
run_doctest
,
is_resource_enabled
import
threading
import
random
# Tests are built around these assumed context defaults
DefaultContext
.
prec
=
9
...
...
@@ -841,17 +842,17 @@ class DecimalUsabilityTest(unittest.TestCase):
self
.
assertEqual
(
cmp
(
dc
,
45
),
0
)
#a Decimal and uncomparable
try
:
da
==
'ugly'
except
TypeError
:
pass
else
:
self
.
fail
(
'Did not raised an error!'
)
try
:
da
==
'32.7'
except
TypeError
:
pass
else
:
self
.
fail
(
'Did not raised an error!'
)
try
:
da
==
object
except
TypeError
:
pass
else
:
self
.
fail
(
'Did not raised an error!'
)
self
.
assertNotEqual
(
da
,
'ugly'
)
self
.
assertNotEqual
(
da
,
32.7
)
self
.
assertNotEqual
(
da
,
object
()
)
self
.
assertNotEqual
(
da
,
object
)
# sortable
a
=
map
(
Decimal
,
xrange
(
100
)
)
b
=
a
[:]
random
.
shuffle
(
a
)
a
.
sort
()
self
.
assertEqual
(
a
,
b
)
def
test_copy_and_deepcopy_methods
(
self
):
d
=
Decimal
(
'43.24'
)
...
...
@@ -1078,6 +1079,10 @@ class ContextAPItests(unittest.TestCase):
v2
=
vars
(
e
)[
k
]
self
.
assertEqual
(
v1
,
v2
)
def
test_equality_with_other_types
(
self
):
self
.
assert_
(
Decimal
(
10
)
in
[
'a'
,
1.0
,
Decimal
(
10
),
(
1
,
2
),
{}])
self
.
assert_
(
Decimal
(
10
)
not
in
[
'a'
,
1.0
,
(
1
,
2
),
{}])
def
test_main
(
arith
=
False
,
verbose
=
None
):
""" Execute the tests.
...
...
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