-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: allow use of custom formatter in array2string. Closes #1218. #72
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
Conversation
This also allows for easy implementation of array inputs to binary_repr and base_repr, for example:
With current master "np.binary_repr(np.arange(5))" just gives a very uninformative error. |
Two things:
|
Re: 2) I think the formatter itself could handle the conversion of the different types using a dictionary if the user desired such. |
Added formatter func to set_printoptions in rgommers@8a1b057 (warning: I rebased the branch) Re: 2), this is not very convenient to do with a dict in the formatter itself, because you would have to define formatters for all types even if you care about only one of them, and do type checks. On the other hand, I'm not sure what the best interface is. We could allow both a callable and a dict. In case of a dict, the user could pass in a dict with only the formatters he's interested in. Does that make sense? I'm not sure the dict thing will be used a lot, but while I'm at it why not get it right... |
Hi Ralph, I've been thinking it would be nice if custom formatters could be set to only handle certain types. I'm not sure of the best way to handle that, however. A dictionary would be one method, another would be to let the function return None instead of a string, in which case array2print would use it's default formatting. Thoughts? |
Functions returning None doesn't work well, because the user would have to duplicate the dtype checks in _array2string. This is a pain to get right and should be avoided. This interface is the most flexible I can think of (although it may be slightly confusing):
Another small issue is this:
Does anyone know where this comes from? I can't find anything that has a _format attribute. |
How about only using a dictionary and adding the key 'all'? That would simplify the interface a bit and I suspect avoid testing for a callable. If you go that way, it probably isn't worth checking if the callables work correctly. But I suspect folks using this option will be sophisticated enough to figure out what is going on. |
That would work better I think. I'll implement this. Also, I want to deprecate the _format thing with the request that if it's used to email the list. Because I suspect it's not used at all, and it looks very ugly. |
The ._format method approach has been there since Numeric days, but like Ralph, I have never seen it used. I am +1 on deprecating it. |
I see that you are already thinking about implementing the dictionary of separate formatting functions. This would be nice. I like Chuck's suggestion of always a dictionary interface as well. |
This allows to set formatting functions for specific types or kinds only. Also works with set_printoptions.
Dictionary formatter implemented: rgommers/numpy@master...ticket-1218-array2string There is a minor increase in overhead because all formatter classes need to be initialized. My unscientific measure of impact (running time of test suite) says this is not more that ~5%. I don't think this is a big issue. |
That should address all your comments Charles. |
Hello, Actually, I'm new here. I want to know how to contribute to numpy. Where is the mailing list ? And is this the main numpy repository ? |
Hi, this is the main repo. Mailing lists at http://scipy.org/Mailing_Lists |
Thanks. I'll join the mailing list. I'm a student of Indian Institute of On Sun, Apr 24, 2011 at 10:20 PM, rgommers <
|
Sure, and welcome. Have a look at http://docs.scipy.org/doc/dev/ which explains the development process. It's pretty much the same for Numpy and Scipy. Depending on your interest you can start by trying to fix some bugs or implement open wishes, or contribute to the docs. Bug tracker is here: http://projects.scipy.org/numpy/ For new functionality it's always good to discuss on the mailing list first. Do keep in mind that Numpy is quite mature, so changes have to be backwards compatible. Scipy is a bit less mature and covers a more broad spectrum of functionality, so if you see something there that particularly interests you then that could also be a good option. |
I squashed the commits into a single one and pushed it in d8de71d |
Thanks. |
Merge in numpy-hpy from ss/array_ctor to labs-hpy-port * commit 'de390fb2539634dd54059b6124642b24431218d7': Fix explicit HPy context lookups More partial migration of array ctor Partial migration of array ctor
feat: Add vcombine_s8
I've found several similar requests to #1218 on the mailing list, the only solution offered was set_printoptions which is quite limited compared to this simple enhancement.