Skip to content

Commit 61c7c41

Browse files
committed
Use humanize for integer/bytes abbreviation
1 parent 61161ca commit 61c7c41

File tree

2 files changed

+7
-42
lines changed

2 files changed

+7
-42
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies = [
1212
"croniter",
1313
"duckdb>=0.10.0,!=0.10.3",
1414
"dateparser<=1.2.1",
15+
"humanize",
1516
"hyperscript>=0.1.0",
1617
"importlib-metadata; python_version<'3.12'",
1718
"ipywidgets",

sqlmesh/core/console.py

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import uuid
88
import logging
99
import textwrap
10+
from humanize import metric, naturalsize
1011
from itertools import zip_longest
1112
from pathlib import Path
1213
from hyperscript import h
@@ -4186,15 +4187,14 @@ def _create_evaluation_model_annotation(
41864187
execution_stats_str = ""
41874188
if execution_stats:
41884189
rows_processed = execution_stats.total_rows_processed
4189-
execution_stats_str += (
4190-
f"{_abbreviate_integer_count(rows_processed)} row{'s' if rows_processed > 1 else ''}"
4191-
if rows_processed
4192-
else ""
4193-
)
4190+
if rows_processed:
4191+
# 1.00 and 1.0 to 1
4192+
rows_processed_str = metric(rows_processed).replace(".00", "").replace(".0", "")
4193+
execution_stats_str += f"{rows_processed_str} row{'s' if rows_processed > 1 else ''}"
41944194

41954195
bytes_processed = execution_stats.total_bytes_processed
41964196
execution_stats_str += (
4197-
f"{', ' if execution_stats_str else ''}{_format_bytes(bytes_processed)}"
4197+
f"{', ' if execution_stats_str else ''}{naturalsize(bytes_processed, binary=True)}"
41984198
if bytes_processed
41994199
else ""
42004200
)
@@ -4299,39 +4299,3 @@ def _calculate_annotation_str_len(
42994299
+ execution_stats_len,
43004300
)
43014301
return annotation_str_len
4302-
4303-
4304-
# Convert number of bytes to a human-readable string
4305-
# https://github.com/dbt-labs/dbt-adapters/blob/34fd178539dcb6f82e18e738adc03de7784c032f/dbt-bigquery/src/dbt/adapters/bigquery/connections.py#L165
4306-
def _format_bytes(num_bytes: t.Optional[int]) -> str:
4307-
if num_bytes and num_bytes >= 0:
4308-
if num_bytes < 1024:
4309-
return f"{num_bytes} bytes"
4310-
4311-
num_bytes_float = float(num_bytes) / 1024.0
4312-
for unit in ["KiB", "MiB", "GiB", "TiB", "PiB"]:
4313-
if num_bytes_float < 1024.0:
4314-
return f"{num_bytes_float:3.1f} {unit}"
4315-
num_bytes_float /= 1024.0
4316-
4317-
num_bytes_float *= 1024.0 # undo last division in loop
4318-
return f"{num_bytes_float:3.1f} {unit}"
4319-
return ""
4320-
4321-
4322-
# Abbreviate integer count. Example: 1,000,000,000 -> 1b
4323-
# https://github.com/dbt-labs/dbt-adapters/blob/34fd178539dcb6f82e18e738adc03de7784c032f/dbt-bigquery/src/dbt/adapters/bigquery/connections.py#L178
4324-
def _abbreviate_integer_count(count: t.Optional[int]) -> str:
4325-
if count and count >= 0:
4326-
if count < 1000:
4327-
return str(count)
4328-
4329-
count_float = float(count) / 1000.0
4330-
for unit in ["k", "m", "b", "t"]:
4331-
if count_float < 1000.0:
4332-
return f"{count_float:3.1f}{unit}".strip()
4333-
count_float /= 1000.0
4334-
4335-
count_float *= 1000.0 # undo last division in loop
4336-
return f"{count_float:3.1f}{unit}".strip()
4337-
return ""

0 commit comments

Comments
 (0)