Metadata-Version: 2.4
Name: limen-common
Version: 0.1.1
Summary: Shared building blocks for the Limen libraries: one retry policy, one data folder, one point and box, one way to take a quantile, one fire danger table, one reliability type, one open-data record and one answer checker
Keywords: limen,open data,retry,geo,reliability,fire weather index,json schema,llms.txt
Author: Giuseppe Zileni
Author-email: Giuseppe Zileni <giuseppe.zileni@gmail.com>
License-Expression: Apache-2.0
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Dist: httpx>=0.27 ; extra == 'http'
Requires-Dist: griffe>=1.5 ; extra == 'llms'
Requires-Dist: jsonschema>=4.23 ; extra == 'record'
Requires-Python: >=3.12
Project-URL: Homepage, https://github.com/agent-engineering-studio/limen-common
Project-URL: Issues, https://github.com/agent-engineering-studio/limen-common/issues
Project-URL: Changelog, https://github.com/agent-engineering-studio/limen-common/blob/main/CHANGELOG.md
Provides-Extra: http
Provides-Extra: llms
Provides-Extra: record
Description-Content-Type: text/markdown

# limen-common

Shared building blocks for the Limen libraries.

Every library of the Limen family wrote its own retry loop, data folder,
bounding box, quantile and reliability score, each a little different: six
retry loops that disagree on whether a 500 is worth another try, six bounding
boxes, three ways to take a percentile, four scales for "how far to trust
this". `limen-common` holds one of each, so the libraries agree.

| Module | What | Replaces |
|---|---|---|
| `limen_common.http` (extra `[http]`) | `RetryPolicy`, `send` (async), `send_sync`: 429/5xx and transport errors retried with exponential backoff, `Retry-After` honoured, everything else returned as it is | the retry loops of dpc-radar, egms-client, ingv-shakemap, copernicus-ems, firms-client, imageserver-export; none in effis-client |
| `limen_common.paths` | `data_dir(lib)`: the argument, then `<LIB>_DATA_DIR`, then `$DATA_ROOT/<lib>`, then `./data/<lib>`; `atomic_write` through `.part` and a rename | each library's own folder rule and `.part` writes |
| `limen_common.geo` | `Point` and `BBox`, longitude first and range-checked; `ITALY`; `haversine_m`; `BBox.around` | six `BBox` types, `(lon, lat)` tuples and `Node`s, three haversines |
| `limen_common.stats` | `quantile`, `quantiles`, `rank`: linear interpolation (numpy/R type 7) by default, nearest rank by name; `None` and `NaN` left out | nearest rank (effis-client), linear (fwi-chain), the ad hoc ones |
| `limen_common.danger` | the EFFIS FWI classes and `effis_class` | the copies in fwi-chain and effis-client ("very high" vs "Very High") |
| `limen_common.reliability` | `Reliability(measured, score, reasons)`, its `level` (none, poor, fair, good) and `weakest(*parts)` | dpc-radar's score, glofas-ewds' word, rainfall-thresholds' interval, egms-client's thresholds |
| `limen_common.record` (extra `[record]` to check) | the open-data record `opendata-record/v2`: typed `Observation`, `Fact`, `Event`, `Layer`, the JSON Schema, `problems`, `validate`, `read`, `from_csv` | the schema kept in egms-client; the record built by hand in rainfall-thresholds |
| `limen_common.check` | one answer checker: `check_text(text, facts, *, lang, question, rules) -> list[Misquote]`, `check_signs`, `check_claims`, `fill`; units and direction words as data in `Rules` | the checkers of rainfall-thresholds, egms-client, fwi-chain, effis-client, ingv-shakemap, firms-client, with diverging signatures |
| `limen_common.facts` | `stamp`, `rounded`, the shared `ANSWER_RULES` and `answer_rules(about, *rules)` | each library's `stamp`, `_round` and the rules repeated in every `ANSWER_RULES` |
| `limen_common.llms` (extra `[llms]` for API pages) | `Site` and `main`: `llms.txt`, `llms-full.txt` and the agent skill from `zensical.toml` and the docstrings | the nine copies of `tools/build_llms.py` |

## The convention

**`None` means "not measured".** It is never zero, never `NaN`, never a
neutral 0.5. A radar outage, a cell with no InSAR pixel, a region with no
dated landslide are not dry weather, still ground or a safe slope: a function
that cannot answer says so, and `Reliability.not_measured(...)` says why.

**Nothing reads the clock.** A function that needs "today" takes it.

**Longitude first**, as GeoJSON: `Point(lon, lat)`, `BBox(west, south, east, north)`.
`Point.from_lat_lon` and `BBox.lat_lon()` are there for the services that put
latitude first.

## Install

```bash
uv add "limen-common[http]"      # with the retry policy (httpx)
uv add "limen-common[record]"    # checking records (jsonschema)
uv add "limen-common[llms]"      # API pages in llms-full.txt (griffe)
uv add limen-common              # the rest has no dependency
```

## Examples

```python
import httpx
from limen_common import Point, Reliability, data_dir, quantile, weakest
from limen_common.http import RetryPolicy, send

folder = data_dir("dpc-radar")  # $DATA_ROOT/dpc-radar
sarno = Point.of(14.62, 40.81)

async with httpx.AsyncClient() as client:
    response = await send(client, "GET", url, policy=RetryPolicy(retries=4))

p95 = quantile([12.0, None, 30.5, 8.1], 0.95)  # None is left out

answer = weakest(
    Reliability.of(0.8, "radar visible from two sites"),
    Reliability.not_measured("no rain gauge within 20 km"),
)
answer.measured  # False: one part was not measured
```

A record, checked, and a text checked against its facts:

```python
from datetime import UTC, datetime
from limen_common import Point, Reliability
from limen_common.check import check_text
from limen_common.record import MadeBy, Observation, Source, Time, Trust

record = Observation(
    source=Source(name="DPC radar", licence="CC BY-SA 4.0"),
    made_by=MadeBy("dpc-radar", "1.2.0"),
    time=Time.at(datetime(2026, 10, 10, 12, tzinfo=UTC)),
    where=Point(11.25, 43.77),
    reliability=Trust.of(Reliability.of(0.8, "two radars see it"), "radar visibility"),
    property="rainfall",
    value=12.5,
    unit="mm",
).check()  # a dict valid against opendata-record/v2, or RecordError with every problem

facts = {"total_mm": 165.7, "needed_mm": 30.9}
check_text("165.7 mm fell; the threshold asks 30.9 mm/h", facts)
# [Misquote(said='30.9 mm/h', fault='unit', unit='mm/h', key='needed_mm', fact=30.9)]
```

The checker's faults are `invented`, `unit`, `rounded`, `sign` and `swapped`.
What is a library's own (a unit like `decades`, a word like *sinks*) goes in
`Rules`: `RULES.with_units({"decades": "decades"}, keys=[("_decades", "decades")])`,
`RULES.with_directions(down={"en": ["sinks?"]})`.

## Moving a library onto it

One library at a time, behaviour first: replace its own helper with the one
here, keep its public names as thin aliases for one minor release, and note any
change of behaviour (a status now retried, a quantile method) in its CHANGELOG.
The issues "Adopt limen-common" in each repository track it.

## Licence

Apache-2.0.
