Skip to content

Support with statements - #81

Closed
kbairak wants to merge 0 commit into
astanin:masterfrom
kbairak:context_manager
Closed

Support with statements#81
kbairak wants to merge 0 commit into
astanin:masterfrom
kbairak:context_manager

Conversation

@kbairak

@kbairak kbairak commented Sep 2, 2020

Copy link
Copy Markdown

This converts tabulate to a context manager.

If called as before, it will work as before. However, if the first argument (tabular_data) isn't provided, it will act as a context
manager, the "as" value being a function that adds values to the table being formatted:

print(tabulate([[1, 2], [3, 4]]))

should be equivalent to:

with tabulate() as t:
    t(1, 2)
    t(3, 4)

The rest of the arguments should work with the 'with' syntax as well:

with tabulate(headers=['name', 'age'], tablefmt="github") as t:
    t('John', 43)
    t('Mary', 32)

The differences when using the 'with' syntax are:

  1. There isn't a return value, rather the result is printed with print
  2. The rest of the arguments (headers etc) cannot be provided as positional arguments, only as keyword arguments

@kbairak
kbairak force-pushed the context_manager branch 4 times, most recently from f7ab40e to 4bef88e Compare September 3, 2020 07:03

@astanin astanin left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think it's a very cool idea!

But there are some issues I believe have to be addressed:

  1. doc-string of the tabulate function is different, so users of the basic function we'll see docs of the context manager first, and then should follow an indirection to figure out how to use tabulate() function. I suggest that the docstring of the tabulate() function stays the same (or is extended). Also, most users should not care/know about underscored names. The function should be usable by whoever learned Python just yesterday.

  2. Named arguments have been moved to the hidden function _tabulate(). I'm not sure how it will play out with autocomplete in various environments. Named arguments are usually picked up and suggested automatically. Did you test how it works with autocomplete somewhere?

  3. Existing tests have been changed. In particular test_no_data(). This is a breaking change which can be avoided.

If you're still interested, I'll accept this feature if you can address these comlaints: 1) keep tabulate() as the top-level function (feel free to give another name to the context manager, e.g. tabulate_context); 2) keep tabulate docstring and its named arguments; 3) do not change existing tests.

@astanin astanin added this to the v0.9 milestone Feb 17, 2021
@kbairak kbairak closed this Mar 14, 2021
@kbairak kbairak mentioned this pull request Mar 14, 2021
@kbairak

kbairak commented Mar 14, 2021

Copy link
Copy Markdown
Author

@astanin Reopened this as #120

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants