-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: Implement ndarray.__format__ for 0d arrays #9883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7013,6 +7013,37 @@ def test_null_inside_ustring_array_is_truthy(self): | |
assert_(a) | ||
|
||
|
||
class TestFormat(object): | ||
|
||
def test_0d(self): | ||
a = np.array(np.pi) | ||
assert_equal('{:0.3g}'.format(a), '3.14') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, because then it shows the two values if they differ. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In pytest you can just
Output is
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency, we should probably use the same form we use in most of the test suite, which gives:
AFAIK, the switch to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are still using nose, th 6DB6 e pytest conversion is still in progress and I don't think I will get back to it until 1.15. |
||
assert_equal('{:0.3g}'.format(a[()]), '3.14') | ||
|
||
def test_1d_no_format(self): | ||
a = np.array([np.pi]) | ||
assert_equal('{}'.format(a), str(a)) | ||
|
||
def test_1d_format(self): | ||
# until gh-5543, ensure that the behaviour matches what it used to be | ||
a = np.array([np.pi]) | ||
|
||
def ret_and_exc(f, *args, **kwargs): | ||
try: | ||
return f(*args, **kwargs), None | ||
except Exception as e: | ||
# exceptions don't compare equal, so return type and args | ||
# which do | ||
return None, (type(e), e.args) | ||
|
||
# Could switch on python version here, but all we care about is | ||
# that the behaviour hasn't changed | ||
assert_equal( | ||
ret_and_exc(object.__format__, a, '30'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose we should silence warnings in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just figure out what the result should be and use that? Eventually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, and I think that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it went away in Python 3.4.
Should ours work if an empty string is passed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed this by making the test Python version dependent. |
||
ret_and_exc('{:30}'.format, a) | ||
) | ||
|
||
|
||
class TestCTypes(object): | ||
|
||
def test_ctypes_is_available(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does the builtin do here?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It keeps the arbitrary and python-dependent behavior that we had before this patch.
Most importantly it handles
'{}'.format
in the typical way. On python 3, it throws an exception if a format string is given, while on python 2 it evaluatesformat(str(arr), 'theformatspec')
.