Skip to content

Commit 431b9de

Browse files
authored
Merge pull request astanin#115 from krishvk/master
Support intfmt - Closes astanin#24
2 parents b3c2c2a + 2ec080d commit 431b9de

File tree

4 files changed

+48
-7
lines changed

4 files changed

+48
-7
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,14 @@ column, in which case every column may have different number formatting:
569569
0.1 0.123 0.12345
570570
--- ----- -------
571571

572+
`intfmt` works similarly for integers
573+
574+
>>> print(tabulate([["a",1000],["b",90000]], intfmt=","))
575+
- ------
576+
a 1,000
577+
b 90,000
578+
- ------
579+
572580
### Text formatting
573581

574582
By default, `tabulate` removes leading and trailing whitespace from text
@@ -783,6 +791,7 @@ Usage of the command line utility
783791
-o FILE, --output FILE print table to FILE (default: stdout)
784792
-s REGEXP, --sep REGEXP use a custom column separator (default: whitespace)
785793
-F FPFMT, --float FPFMT floating point number format (default: g)
794+
-I INTFMT, --int INTFMT integer point number format (default: "")
786795
-f FMT, --format FMT set output table format; supported formats:
787796
plain, simple, github, grid, fancy_grid, pipe,
788797
orgtbl, rst, mediawiki, html, latex, latex_raw,

tabulate.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def _is_file(f):
3232
PRESERVE_WHITESPACE = False
3333

3434
_DEFAULT_FLOATFMT = "g"
35+
_DEFAULT_INTFMT = ""
3536
_DEFAULT_MISSINGVAL = ""
3637
# default align will be overwritten by "left", "center" or "decimal"
3738
# depending on the formatter
@@ -1032,7 +1033,7 @@ def _column_type(strings, has_invisible=True, numparse=True):
10321033
return reduce(_more_generic, types, bool)
10331034

10341035

1035-
def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
1036+
def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
10361037
"""Format a value according to its type.
10371038
10381039
Unicode is supported:
@@ -1047,8 +1048,10 @@ def _format(val, valtype, floatfmt, missingval="", has_invisible=True):
10471048
if val is None:
10481049
return missingval
10491050

1050-
if valtype in [int, str]:
1051+
if valtype is str:
10511052
return f"{val}"
1053+
elif valtype is int:
1054+
return format(val, intfmt)
10521055
elif valtype is bytes:
10531056
try:
10541057
return str(val, "ascii")
@@ -1324,6 +1327,7 @@ def tabulate(
13241327
headers=(),
13251328
tablefmt="simple",
13261329
floatfmt=_DEFAULT_FLOATFMT,
1330+
intfmt=_DEFAULT_INTFMT,
13271331
numalign=_DEFAULT_ALIGN,
13281332
stralign=_DEFAULT_ALIGN,
13291333
missingval=_DEFAULT_MISSINGVAL,
@@ -1398,6 +1402,10 @@ def tabulate(
13981402
Table formats
13991403
-------------
14001404
1405+
`intfmt` is a format specification used for columns which
1406+
contain numeric data without a decimal point. This can also be
1407+
a list or tuple of format strings, one per column.
1408+
14011409
`floatfmt` is a format specification used for columns which
14021410
contain numeric data with a decimal point. This can also be
14031411
a list or tuple of format strings, one per column.
@@ -1843,15 +1851,25 @@ def tabulate(
18431851
float_formats = list(floatfmt)
18441852
if len(float_formats) < len(cols):
18451853
float_formats.extend((len(cols) - len(float_formats)) * [_DEFAULT_FLOATFMT])
1854+
if isinstance(intfmt, str): # old version
1855+
int_formats = len(cols) * [
1856+
intfmt
1857+
] # just duplicate the string to use in each column
1858+
else: # if intfmt is list, tuple etc we have one per column
1859+
int_formats = list(intfmt)
1860+
if len(int_formats) < len(cols):
1861+
int_formats.extend((len(cols) - len(int_formats)) * [_DEFAULT_INTFMT])
18461862
if isinstance(missingval, str):
18471863
missing_vals = len(cols) * [missingval]
18481864
else:
18491865
missing_vals = list(missingval)
18501866
if len(missing_vals) < len(cols):
18511867
missing_vals.extend((len(cols) - len(missing_vals)) * [_DEFAULT_MISSINGVAL])
18521868
cols = [
1853-
[_format(v, ct, fl_fmt, miss_v, has_invisible) for v in c]
1854-
for c, ct, fl_fmt, miss_v in zip(cols, coltypes, float_formats, missing_vals)
1869+
[_format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c]
1870+
for c, ct, fl_fmt, int_fmt, miss_v in zip(
1871+
cols, coltypes, float_formats, int_formats, missing_vals
1872+
)
18551873
]
18561874

18571875
# align columns
@@ -2266,6 +2284,7 @@ def _main():
22662284
-o FILE, --output FILE print table to FILE (default: stdout)
22672285
-s REGEXP, --sep REGEXP use a custom column separator (default: whitespace)
22682286
-F FPFMT, --float FPFMT floating point number format (default: g)
2287+
-I INTFMT, --int INTFMT integer point number format (default: "")
22692288
-f FMT, --format FMT set output table format; supported formats:
22702289
plain, simple, grid, fancy_grid, pipe, orgtbl,
22712290
rst, mediawiki, html, latex, latex_raw,
@@ -2281,14 +2300,15 @@ def _main():
22812300
opts, args = getopt.getopt(
22822301
sys.argv[1:],
22832302
"h1o:s:F:A:f:",
2284-
["help", "header", "output", "sep=", "float=", "align=", "format="],
2303+
["help", "header", "output", "sep=", "float=", "int=", "align=", "format="],
22852304
)
22862305
except getopt.GetoptError as e:
22872306
print(e)
22882307
print(usage)
22892308
sys.exit(2)
22902309
headers = []
22912310
floatfmt = _DEFAULT_FLOATFMT
2311+
intfmt = _DEFAULT_INTFMT
22922312
colalign = None
22932313
tablefmt = "simple"
22942314
sep = r"\s+"
@@ -2300,6 +2320,8 @@ def _main():
23002320
outfile = value
23012321
elif opt in ["-F", "--float"]:
23022322
floatfmt = value
2323+
elif opt in ["-I", "--int"]:
2324+
intfmt = value
23032325
elif opt in ["-C", "--colalign"]:
23042326
colalign = value.split()
23052327
elif opt in ["-f", "--format"]:
@@ -2325,6 +2347,7 @@ def _main():
23252347
tablefmt=tablefmt,
23262348
sep=sep,
23272349
floatfmt=floatfmt,
2350+
intfmt=intfmt,
23282351
file=out,
23292352
colalign=colalign,
23302353
)
@@ -2336,16 +2359,17 @@ def _main():
23362359
tablefmt=tablefmt,
23372360
sep=sep,
23382361
floatfmt=floatfmt,
2362+
intfmt=intfmt,
23392363
file=out,
23402364
colalign=colalign,
23412365
)
23422366

23432367

2344-
def _pprint_file(fobject, headers, tablefmt, sep, floatfmt, file, colalign):
2368+
def _pprint_file(fobject, headers, tablefmt, sep, floatfmt, intfmt, file, colalign):
23452369
rows = fobject.readlines()
23462370
table = [re.split(sep, r.rstrip()) for r in rows if r.strip()]
23472371
print(
2348-
tabulate(table, headers, tablefmt, floatfmt=floatfmt, colalign=colalign),
2372+
tabulate(table, headers, tablefmt, floatfmt=floatfmt, intfmt=intfmt, colalign=colalign),
23492373
file=file,
23502374
)
23512375

test/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def test_tabulate_signature():
4242
("headers", ()),
4343
("tablefmt", "simple"),
4444
("floatfmt", "g"),
45+
("intfmt", ""),
4546
("numalign", "default"),
4647
("stralign", "default"),
4748
("missingval", ""),

test/test_output.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,6 +2110,13 @@ def test_empty_data_without_headers():
21102110
assert_equal(expected, result)
21112111

21122112

2113+
def test_intfmt():
2114+
"Output: integer format"
2115+
result = tabulate([[10000], [10]], intfmt=",", tablefmt="plain")
2116+
expected = "10,000\n 10"
2117+
assert_equal(expected, result)
2118+
2119+
21132120
def test_empty_data_with_headers():
21142121
"Output: table with empty data and headers as firstrow"
21152122
expected = ""

0 commit comments

Comments
 (0)