Skip to content

Commit

Permalink
Improve visibility of sigfig import
Browse files Browse the repository at this point in the history
  • Loading branch information
masaccio committed Oct 13, 2024
1 parent 4268dcf commit 22ad9a3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
19 changes: 14 additions & 5 deletions src/numbers_parser/_cat_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import sys

import sigfig
from sigfig import round as sigfig

from numbers_parser import Document, ErrorCell, FileError, FileFormatError, NumberCell, _get_version
from numbers_parser import __name__ as numbers_parser_name
Expand Down Expand Up @@ -49,15 +49,24 @@ def command_line_parser():
help="Dump formatted cells (durations) as they appear in Numbers",
)
parser.add_argument(
"-s", "--sheet", action="append", help="Names of sheet(s) to include in export",
"-s",
"--sheet",
action="append",
help="Names of sheet(s) to include in export",
)
parser.add_argument(
"-t", "--table", action="append", help="Names of table(s) to include in export",
"-t",
"--table",
action="append",
help="Names of table(s) to include in export",
)
parser.add_argument("document", nargs="*", help="Document(s) to export")
parser.add_argument("--debug", default=False, action="store_true", help="Enable debug logging")
parser.add_argument(
"--experimental", default=False, action="store_true", help=argparse.SUPPRESS,
"--experimental",
default=False,
action="store_true",
help=argparse.SUPPRESS,
)
return parser

Expand All @@ -81,7 +90,7 @@ def cell_as_string(args, cell):
elif args.formatting and cell.formatted_value is not None:
return cell.formatted_value
elif isinstance(cell, NumberCell):
return sigfig.round(cell.value, sigfigs=MAX_SIGNIFICANT_DIGITS, warn=False)
return sigfig(cell.value, sigfigs=MAX_SIGNIFICANT_DIGITS, warn=False)
elif cell.value is None:
return ""
else:
Expand Down
18 changes: 8 additions & 10 deletions src/numbers_parser/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
from typing import Any, List, Optional, Tuple, Union
from warnings import warn

import sigfig
from sigfig import round as sigfig

from numbers_parser import __name__ as numbers_parser_name

# from numbers_parser.cell_storage import CellStorage, CellType
from numbers_parser.constants import (
CHECKBOX_FALSE_VALUE,
CHECKBOX_TRUE_VALUE,
Expand Down Expand Up @@ -799,7 +797,7 @@ def _from_value(cls, row: int, col: int, value):
elif isinstance(value, int):
cell = NumberCell(row, col, value)
elif isinstance(value, float):
rounded_value = sigfig.round(value, sigfigs=MAX_SIGNIFICANT_DIGITS, warn=False)
rounded_value = sigfig(value, sigfigs=MAX_SIGNIFICANT_DIGITS, warn=False)
if rounded_value != value:
warn(
f"'{value}' rounded to {MAX_SIGNIFICANT_DIGITS} significant digits",
Expand Down Expand Up @@ -1734,7 +1732,7 @@ def _decode_number_format(format, value, name): # noqa: PLR0912
int_width = num_integers

# value_1 = str(value).split(".")[0]
# value_2 = sigfig.round(str(value).split(".")[1], sigfig=MAX_SIGNIFICANT_DIGITS, warn=False)
# value_2 = sigfig(str(value).split(".")[1], sigfig=MAX_SIGNIFICANT_DIGITS, warn=False)
# int_pad_space_as_zero = (
# num_integers > 0
# and num_decimals > 0
Expand Down Expand Up @@ -1812,16 +1810,16 @@ def _format_decimal(value: float, format, percent: bool = False) -> str:
formatted_value = f"{int(value):{thousands}}"
else:
if format.decimal_places >= DECIMAL_PLACES_AUTO:
formatted_value = str(sigfig.round(value, MAX_SIGNIFICANT_DIGITS, warn=False))
formatted_value = str(sigfig(value, MAX_SIGNIFICANT_DIGITS, warn=False))
else:
formatted_value = sigfig.round(value, MAX_SIGNIFICANT_DIGITS, type=str, warn=False)
formatted_value = sigfig.round(
formatted_value = sigfig(value, MAX_SIGNIFICANT_DIGITS, type=str, warn=False)
formatted_value = sigfig(
formatted_value,
decimals=format.decimal_places,
type=str,
)
if format.show_thousands_separator:
formatted_value = sigfig.round(formatted_value, spacer=",", spacing=3, type=str)
formatted_value = sigfig(formatted_value, spacer=",", spacing=3, type=str)
try:
(integer, decimal) = formatted_value.split(".")
formatted_value = integer + "." + decimal.replace(",", "")
Expand Down Expand Up @@ -1945,7 +1943,7 @@ def _format_fraction(value: float, format) -> str:


def _format_scientific(value: float, format) -> str:
formatted_value = sigfig.round(value, sigfigs=MAX_SIGNIFICANT_DIGITS, warn=False)
formatted_value = sigfig(value, sigfigs=MAX_SIGNIFICANT_DIGITS, warn=False)
return f"{formatted_value:.{format.decimal_places}E}"


Expand Down

0 comments on commit 22ad9a3

Please sign in to comment.