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

re-write columnize, with intermediate step. #1875

Merged
merged 2 commits into from Jun 10, 2012

Conversation

Carreau
Copy link
Member

@Carreau Carreau commented Jun 7, 2012

fix test that where wrong, add some others.

fix #1860

Maybe not the fastest implementation ever, but allow some granularity, and intermediate state i'll need for #1851. More over, it does more in less lines (docstrings and test excluded :-) ).
Nice thing with this granularity is that we could add stretching separators in one line if necessary.

Right now, it finds the right number of columns by bruteforcing,
we could try to bisect if we start hitting high number of items, but i'm not sure it is necessary.

Output of the new and old function were identical on around 20000 random test case, except when the old columnize was wrong (wider output than displaywidth).

I added a random test to hopefully catch edges cases (fail with a few percent probability on old columnize, haven't failed with new in thousand of sample) .

As it is a random test, I wasn't sure on how to keep information about it, so I added print statements and assert False is there a better way ?

@takluyver
Copy link
Member

Is there no way to test deterministically? A random test that may pass even with incorrect code doesn't sound ideal.

@Carreau
Copy link
Member Author

Carreau commented Jun 7, 2012

There are already some derminalistic test, the random one is ment to hopefully catch some edge case we missed (if there is some), then we could fix, and add this specific case as a deterministic test...

otherwise we should test for all combinaison of length...

@takluyver
Copy link
Member

OK, that makes sense - there's a deterministic test for this problem, and a random test to flag up potential unknown problems. Thanks,

def _find_optimal(rlist , sepsize=2 , displaywidth=80):
"""Calculate optimal info to columnize a list of string"""
for nrow in range(1, len(rlist)+1) :
chk = [max(l) for l in _chunks(rlist, nrow) ]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, when there's no extra logic, map is still clearer than a listcomp (it's also faster):

chk = map(max, _chunks(rlist, nrow))

@fperez
Copy link
Member

fperez commented Jun 10, 2012

A few small points, but overall looks great, thanks!

@Carreau
Copy link
Member Author

Carreau commented Jun 10, 2012

ok, thanks, fixed, also in #1851

@fperez fperez merged commit 4659fca into ipython:master Jun 10, 2012
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.

IPython.utils.columnize sometime wrong...
3 participants