FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Change repr(float) if float(int(f)) == f · Pull Request #104 · go-python/gpython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (1) .py  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions py/float.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ func FloatNew(metatype *Type, args Tuple, kwargs StringDict) (Object, error) {
}

func (a Float) M__str__() (Object, error) {
if i := int64(a); Float(i) == a {
return String(fmt.Sprintf("%d.0", i)), nil
}
return String(fmt.Sprintf("%g", a)), nil
}

Expand Down
20 changes: 20 additions & 0 deletions py/tests/float.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,24 @@
assert float(" -1E400") == float("-inf")
assertRaises(ValueError, float, "1 E200")

doc="repr"
assert repr(float("1.0")) == "1.0"
assert repr(float("1.")) == "1.0"
assert repr(float("1.1")) == "1.1"
assert repr(float("1.11")) == "1.11"
assert repr(float("-1.0")) == "-1.0"
assert repr(float("1.00101")) == "1.00101"
assert repr(float("1.00")) == "1.0"
assert repr(float("2.010")) == "2.01"

doc="str"
assert str(float("1.0")) == "1.0"
assert str(float("1.")) == "1.0"
assert str(float("1.1")) == "1.1"
assert str(float("1.11")) == "1.11"
assert str(float("-1.0")) == "-1.0"
assert str(float("1.00101")) == "1.00101"
assert str(float("1.00")) == "1.0"
assert str(float("2.010")) == "2.01"

doc="finished"
2 changes: 2 additions & 0 deletions py/tests/list.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
assert str([1,2,3]) == "[1, 2, 3]"
assert str([1,[2,3],4]) == "[1, [2, 3], 4]"
assert str(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]"
assert str([1, 1.0]) == "[1, 1.0]"

doc="repr"
assert repr([]) == "[]"
assert repr([1,2,3]) == "[1, 2, 3]"
assert repr([1,[2,3],4]) == "[1, [2, 3], 4]"
assert repr(["1",[2.5,17,[]]]) == "['1', [2.5, 17, []]]"
assert repr([1, 1.0]) == "[1, 1.0]"

doc="enumerate"
a = [e for e in enumerate([3,4,5,6,7], 4)]
Expand Down
2 changes: 2 additions & 0 deletions py/tests/tuple.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
assert str((1,2,3)) == "(1, 2, 3)"
assert str((1,(2,3),4)) == "(1, (2, 3), 4)"
assert str(("1",(2.5,17,()))) == "('1', (2.5, 17, ()))"
assert str((1, 1.0)) == "(1, 1.0)"

doc="repr"
assert repr(()) == "()"
assert repr((1,2,3)) == "(1, 2, 3)"
assert repr((1,(2,3),4)) == "(1, (2, 3), 4)"
assert repr(("1",(2.5,17,()))) == "('1', (2.5, 17, ()))"
assert repr((1, 1.0)) == "(1, 1.0)"

doc="mul"
a = (1, 2, 3)
Expand Down

Back | FazBrowse Home | New Git URL