Skip to content
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

singledispatch argumentToString #115

Merged
merged 10 commits into from
Feb 8, 2022
Merged

singledispatch argumentToString #115

merged 10 commits into from
Feb 8, 2022

Conversation

atusy
Copy link
Contributor

@atusy atusy commented Feb 2, 2022

Current implementation requires users to rewrite argToStringFunction totally.

I would suggest singledispatch argumentToString, which allows sort of ad-hoc way to stringify the argument.

This makes codes shorter.
Also, This is very handy especially on Jupyter Notebook because users may add customizations in working cells, instead of going back to the cell which defines custom argToStringFunction in a current manner.

from icecream import ic, argumentToString
import numpy as np

@argumentToString.register(np.ndarray)
def argumentToString_ndarray(obj):
    return f"shape: {obj.shape}, min: {obj.min()}, mean: {obj.mean()}, max: {obj.max()}"

x = np.arange(10).reshape(5, -1)
ic(x)
ic| x: shape: (5, 2), min: 0, mean: 4.5, max: 9
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

@alexmojaki
Copy link
Collaborator

Cool idea! However this doesn't work in Python 2. That doesn't mean you have to drop this PR or implement singledispatch for the sake of Python 2, this could be a Python-3-only feature, just make sure that not everything breaks when from functools import singledispatch fails. A test would also be good.

@atusy
Copy link
Contributor Author

atusy commented Feb 2, 2022

@alexmojaki
Thank you for your review!!
I will try!!

@gruns
Copy link
Owner

gruns commented Feb 2, 2022

neat idea!

one important item: every action needs to have a reciprocal undo action. insofar, there should be a way to unregister() a registered argumentToString() function. additionally useful would be a way to list/enumerate all the registered argumentToString() functions for programmatic usage

i leave it up to you two to figure out the best API, but at minimum argumentToString() should have an unregister() method that unregisters/reverses the effect of register()

@atusy
Copy link
Contributor Author

atusy commented Feb 3, 2022

@gruns
Thank you for the idea.

I overrided singledispatch to add unregister and avoid ImportError Python2.

list/enumerate all the registered argumentToString()

This is already possible

argumentToString.registry
# mappingproxy({object: <function icecream.icecream.argumentToString(obj)>,
#              numpy.ndarray: <function __main__.argumentToString_ndarray(obj)>})

I will also add tests.

@atusy
Copy link
Contributor Author

atusy commented Feb 3, 2022

done with adding test

@gruns
Copy link
Owner

gruns commented Feb 3, 2022

This is already possible

argumentToString.registry

👌

I will also add tests.

👌

tests/test_icecream.py Outdated Show resolved Hide resolved
@alexmojaki
Copy link
Collaborator

Looks good overall, it just also needs some docs next to the existing ones for argToStringFunction.

@atusy
Copy link
Contributor Author

atusy commented Feb 3, 2022

Definitely! I will update README.md tomorrow!

Just for sure, is this the doc you mean?

icecream/README.md

Lines 276 to 293 in 7bf20d4

`argToStringFunction`, if provided, is called with argument values to be
serialized to displayable strings. The default is PrettyPrint's
[pprint.pformat()](https://docs.python.org/3/library/pprint.html#pprint.pformat),
but this can be changed to, for example, handle non-standard datatypes in a
custom fashion.
```pycon
>>> from icecream import ic
>>>
>>> def toString(obj):
>>> if isinstance(obj, str):
>>> return '[!string %r with length %i!]' % (obj, len(obj))
>>> return repr(obj)
>>>
>>> ic.configureOutput(argToStringFunction=toString)
>>> ic(7, 'hello')
ic| 7: 7, 'hello': [!string 'hello' with length 5!]
```

@alexmojaki
Copy link
Collaborator

Yes

@atusy
Copy link
Contributor Author

atusy commented Feb 3, 2022

@alexmojaki Thanks. I added documentation.

README.md Outdated Show resolved Hide resolved
@alexmojaki
Copy link
Collaborator

Thanks, The docs look pretty good. @gruns, any more comments? After this is merged, can you make sure the tests pass with tox (reminder of #98) and then release to PyPI?

Co-authored-by: Alex Hall <[email protected]>
@gruns gruns merged commit 88649d1 into gruns:master Feb 8, 2022
@gruns
Copy link
Owner

gruns commented Feb 8, 2022

no more comments from me. docs look pretty good. thank you for your wonderful work, @atusy!

ill rerun tests and release to pypi this weekend. thx for the reminder, @alexmojaki 🙏

@atusy
Copy link
Contributor Author

atusy commented Feb 9, 2022

Thank you @gruns and @alexmojaki
I'm so proud of contributing to this wonderrful project!!

@gruns
Copy link
Owner

gruns commented Feb 15, 2022

GLOB sdist-make: /mnt/fett/code/icecream/setup.py
✔ OK py36 in 5.046 seconds
✔ OK py35 in 5.181 seconds
✔ OK py27 in 5.731 seconds
✔ OK py39 in 7.227 seconds
✔ OK py37 in 7.361 seconds
✔ OK py38 in 7.397 seconds
✔ OK pypy in 10.359 seconds
✔ OK pypy3 in 11.83 seconds

summary 
  py27: commands succeeded
  py35: commands succeeded
  py36: commands succeeded
  py37: commands succeeded
  py38: commands succeeded
  py39: commands succeeded
  pypy: commands succeeded
  pypy3: commands succeeded
  congratulations :)

all tests pass 🙌

icecream v2.1.2 now out: https://pypi.org/project/icecream/

@atusy: huge thank you for your PR! i added a sincere shoutout to your work in the changelog https://github.com/gruns/icecream/blob/master/changelog.txt#L4

@atusy
Copy link
Contributor Author

atusy commented Feb 15, 2022

@gruns Thank you, too!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants