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

Preserve Float Subclass #361

Open
deckar01 opened this issue Mar 1, 2025 · 3 comments
Open

Preserve Float Subclass #361

deckar01 opened this issue Mar 1, 2025 · 3 comments

Comments

@deckar01
Copy link

deckar01 commented Mar 1, 2025

Numbers are cast to float before format()ing, which prevents custom formatting logic provided by subclasses. tabulate/__init__.py#L1350

Example

from tabulate import tabulate
from prefixed import Float

tabulate([[Float(1)]], floatfmt='h')

ValueError: Unknown format code 'h' for object of type 'float'

Background

Proposals to extend the built-in python formats have been rejected, because subclasses are intended to satisfy this use case.

https://discuss.python.org/t/new-format-specifiers-for-string-formatting-of-floats-with-si-and-iec-prefixes/26914/6

This regression was introduced in version 0.2. 5115cea/tabulate.py#L154

Related to #297.

Proposal

Only apply the float cast in _format() to strings.

Edit: And decimal align any string by . or \d([^d]) in _afterpoint().

@deckar01
Copy link
Author

deckar01 commented Mar 2, 2025

After patching that I also found that the default decimal alignment does not behave as expected. _afterpoint() requires the string to pass _isnumber(), which can fail for custom formats. I would propose removing this restriction and allowing arbitrary strings to be aligned by decimal.

@deckar01
Copy link
Author

deckar01 commented Mar 4, 2025

I also had to generalize the e position logic in _afterpoint() to get custom suffixes aligned correctly without decimal points. Aligning on the rightmost digit of the string regardless of the next character aligns prefixes, suffixes, and sandwiches 🥪 (like e).

  f0     Q    gain    Av    BW    C1     R1    R2      R3
----  ----  ------  ----  ----  ----  -----  ----  ------
 700  2      10     3.16   350  100n  1.44k   940   9.09k
 701  2       9.64  3.03   350  100n  1.5k    910   9.1k
 728  2.52   13.2   4.58   289  100n  1.2k    680  11k
 719  2.71   12     4      265  100n  1.5k    560  12k

@deckar01
Copy link
Author

deckar01 commented Mar 4, 2025

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

No branches or pull requests

1 participant