perucompras-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- perucompras_cli-0.1.0.dist-info/METADATA +429 -0
- perucompras_cli-0.1.0.dist-info/RECORD +50 -0
- perucompras_cli-0.1.0.dist-info/WHEEL +4 -0
- perucompras_cli-0.1.0.dist-info/entry_points.txt +2 -0
- perucompras_cli-0.1.0.dist-info/licenses/LICENSE +16 -0
- piloto_ai_cli/__init__.py +51 -0
- piloto_ai_cli/__main__.py +5 -0
- piloto_ai_cli/api.py +394 -0
- piloto_ai_cli/attribute_text.py +106 -0
- piloto_ai_cli/attributes_service.py +250 -0
- piloto_ai_cli/auto_tag.py +65 -0
- piloto_ai_cli/backoff.py +25 -0
- piloto_ai_cli/cards.py +871 -0
- piloto_ai_cli/cli.py +602 -0
- piloto_ai_cli/connectors.py +106 -0
- piloto_ai_cli/database.py +491 -0
- piloto_ai_cli/enrichment.py +557 -0
- piloto_ai_cli/errors.py +92 -0
- piloto_ai_cli/excel.py +186 -0
- piloto_ai_cli/extraction.py +1316 -0
- piloto_ai_cli/health.py +398 -0
- piloto_ai_cli/health_workflow.py +1060 -0
- piloto_ai_cli/http_client.py +610 -0
- piloto_ai_cli/llm_client.py +259 -0
- piloto_ai_cli/llm_extraction.py +444 -0
- piloto_ai_cli/llm_output.py +90 -0
- piloto_ai_cli/pane.py +303 -0
- piloto_ai_cli/parsers.py +477 -0
- piloto_ai_cli/payload_store.py +227 -0
- piloto_ai_cli/pdf_parser.py +12 -0
- piloto_ai_cli/py.typed +0 -0
- piloto_ai_cli/query_compiler.py +1112 -0
- piloto_ai_cli/query_result.py +175 -0
- piloto_ai_cli/query_service.py +199 -0
- piloto_ai_cli/reextract.py +129 -0
- piloto_ai_cli/review.py +217 -0
- piloto_ai_cli/rules.py +67 -0
- piloto_ai_cli/run_log.py +230 -0
- piloto_ai_cli/schema/__init__.py +25 -0
- piloto_ai_cli/schema/contract-v1.schema.json +261 -0
- piloto_ai_cli/schema/contract.py +257 -0
- piloto_ai_cli/schema/migration_filters.py +59 -0
- piloto_ai_cli/schema/models.py +747 -0
- piloto_ai_cli/schema/snowflake_autogen.py +134 -0
- piloto_ai_cli/search_form.py +82 -0
- piloto_ai_cli/settings.py +174 -0
- piloto_ai_cli/snowflake_url.py +90 -0
- piloto_ai_cli/stamp_service.py +53 -0
- piloto_ai_cli/sync.py +1157 -0
- piloto_ai_cli/unit_converter.py +81 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Canonicalize quantities using accepted unit conversion rules."""
|
|
2
|
+
|
|
3
|
+
from collections.abc import Iterable
|
|
4
|
+
from decimal import ROUND_HALF_EVEN, Decimal, localcontext
|
|
5
|
+
|
|
6
|
+
from piloto_ai_cli.errors import ContractError, ContractErrorCode
|
|
7
|
+
from piloto_ai_cli.schema.models import UnitRule
|
|
8
|
+
|
|
9
|
+
STORED_PRECISION = 24
|
|
10
|
+
STORED_SCALE_DIGITS = 6
|
|
11
|
+
STORED_SCALE = Decimal(1).scaleb(-STORED_SCALE_DIGITS)
|
|
12
|
+
MAX_STORED_VALUE = Decimal(10) ** (STORED_PRECISION - STORED_SCALE_DIGITS) - STORED_SCALE
|
|
13
|
+
WILDCARD_CATEGORY = "*"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class UnitConverter:
|
|
17
|
+
"""Convert raw quantities to an attribute's canonical unit and stored scale."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, unit_rules: Iterable[UnitRule]) -> None:
|
|
20
|
+
self._rules = {
|
|
21
|
+
(rule.raw_unit, rule.category): rule for rule in unit_rules if rule.status == "accepted"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
def convert(
|
|
25
|
+
self,
|
|
26
|
+
value: Decimal,
|
|
27
|
+
raw_unit: str,
|
|
28
|
+
canonical_unit: str,
|
|
29
|
+
category: str,
|
|
30
|
+
*,
|
|
31
|
+
location: int | str = "unit",
|
|
32
|
+
) -> Decimal:
|
|
33
|
+
"""Return a rounded canonical value, or refuse an unsupported conversion."""
|
|
34
|
+
if not value.is_finite():
|
|
35
|
+
raise ContractError(
|
|
36
|
+
ContractErrorCode.SCHEMA_VIOLATION,
|
|
37
|
+
location,
|
|
38
|
+
"Quantity must be finite.",
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
rule = None
|
|
42
|
+
if raw_unit != canonical_unit:
|
|
43
|
+
rule = self._rules.get((raw_unit, category))
|
|
44
|
+
if rule is None:
|
|
45
|
+
rule = self._rules.get((raw_unit, WILDCARD_CATEGORY))
|
|
46
|
+
if rule is None or rule.canonical_unit != canonical_unit:
|
|
47
|
+
raise ContractError(
|
|
48
|
+
ContractErrorCode.UNIT_NOT_CONVERTIBLE,
|
|
49
|
+
location,
|
|
50
|
+
f"Unit {raw_unit!r} cannot be converted to {canonical_unit!r} "
|
|
51
|
+
f"for category {category!r}.",
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
factor = rule.factor if rule is not None else Decimal(1)
|
|
55
|
+
with localcontext() as context:
|
|
56
|
+
context.prec = max(50, len(value.as_tuple().digits) + len(factor.as_tuple().digits) + 2)
|
|
57
|
+
try:
|
|
58
|
+
converted = value * factor
|
|
59
|
+
except ArithmeticError as error:
|
|
60
|
+
# An exponent beyond the decimal context (e.g. 1e1000000) overflows
|
|
61
|
+
# before the range check below can see it.
|
|
62
|
+
raise ContractError(
|
|
63
|
+
ContractErrorCode.SCHEMA_VIOLATION,
|
|
64
|
+
location,
|
|
65
|
+
"Converted quantity exceeds the stored numeric range.",
|
|
66
|
+
) from error
|
|
67
|
+
# adjusted() is the exponent for zero, so zero (0E+18 included) is in range.
|
|
68
|
+
if converted and converted.adjusted() >= STORED_PRECISION - STORED_SCALE_DIGITS:
|
|
69
|
+
raise ContractError(
|
|
70
|
+
ContractErrorCode.SCHEMA_VIOLATION,
|
|
71
|
+
location,
|
|
72
|
+
"Converted quantity exceeds the stored numeric range.",
|
|
73
|
+
)
|
|
74
|
+
rounded = converted.quantize(STORED_SCALE, rounding=ROUND_HALF_EVEN)
|
|
75
|
+
if abs(rounded) > MAX_STORED_VALUE:
|
|
76
|
+
raise ContractError(
|
|
77
|
+
ContractErrorCode.SCHEMA_VIOLATION,
|
|
78
|
+
location,
|
|
79
|
+
"Converted quantity exceeds the stored numeric range.",
|
|
80
|
+
)
|
|
81
|
+
return rounded
|