Skip to main content

Typed, read-only Python client for a tns-mirror TNS catalogue database.

Project description

tns-mirror-client

Typed, read-only Python access to a tns-mirror database — a local mirror of the IAU Transient Name Server public objects catalogue.

Add the library, get results. No hand-written SQL, and no rate-limited API on your hot path.

$ pip install 'tns-mirror-client>=1,<2'
import os
from tns_mirror_client import TnsMirror

with TnsMirror(dsn=os.environ["TNS_RO_DSN"]) as tns:
    hit = tns.nearest(ra=203.1, dec=10.2, radius_arcsec=3.0)
    if hit:
        print(hit.name, hit.type, hit.redshift, f'{hit.separation_arcsec:.2f}"')

    obj = tns.by_name("AT2026abc")
    print(tns.count(), "objects mirrored")

You need a read-only DSN from whoever operates the mirror. This library never writes, and it carries no TNS credentials — those belong to the server.

Why not just write the SQL?

Because a cone search on a sphere has three edges that are easy to get wrong and fail silently:

  • The cosine must be clamped before acos, or an exact positional match — the most common case in a cross-match — raises a domain error.
  • Near a pole the RA bound stops meaning anything: two objects 177° apart in right ascension can be 0.2° apart on the sky.
  • At the 0/360 seam, ra BETWEEN 359.5 AND 0.5 matches nothing at all.

This library handles all three, and keeps the indexed bounding-box prefilter that makes the query fast. The geometry is tested by sampling the rim of the cone at every bearing and asserting the prefilter never excludes a point that is genuinely inside the radius.

API

Method Returns
nearest(ra, dec, radius_arcsec=3.0) TnsObject | None — closest match, ties broken on objid
search(ra, dec, radius_arcsec, limit=None) list[TnsObject] — everything in range, nearest first
by_name(name) TnsObject | None
by_objid(objid) TnsObject | None
by_names(names) dict[str, TnsObject] — many in one round trip
count() int
meta() MirrorMeta — schema version and freshness

TnsObject mirrors the schema columns exactly: objid, name, ra, dec, type, redshift, discoverydate, discoverymag, internal_names, reporting_group, source_snapshot, refreshed_at — plus separation_arcsec, populated only by cone queries.

Everything but objid, name, ra, dec, source_snapshot and refreshed_at is nullable, and a minimal TNS row is common. Do not assume type or redshift is populated; hit.is_classified says whether TNS has published a classification.

Check freshness before you trust it

A row count looks identical whether the mirror synced an hour ago or died three weeks ago:

meta = tns.meta()
if not meta.is_fresh(max_age_hours=26):
    raise RuntimeError(f"TNS mirror is stale: last sync {meta.age} ago")

Versioning

Client 1.x speaks schema v1. The major version tracks the contract, not the project milestone, so pin it:

tns-mirror-client>=1,<2

You are then insulated from additive schema changes — a new nullable column or a new index bumps the minor and never breaks a reader. A breaking change bumps both majors together and is announced in the changelog.

The client reads the mirror's published schema_version on connect and raises SchemaVersionError on a mismatch rather than returning quietly wrong answers. That check needs SELECT on tns_mirror_meta, which the mirror's print-grants output includes; pass check_schema_version=False to skip it.

Connections

TnsMirror(dsn=...) opens lazily and closes with the context manager or .close(). To reuse a connection you already manage — from a pool, say — pass it instead, and the client will not close what it did not open:

tns = TnsMirror(connection=my_conn)

If the mirror was deployed with a non-default TNS_SCHEMA/TNS_TABLE, pass schema= and table=.

The client sets its session read-only as defence in depth, so even a bug in this library cannot write through a privileged role.

Dependencies

One: psycopg. This gets embedded in other people's applications, so results are plain dataclasses rather than a validation framework.

Requires Python 3.11+.

Licence

MIT. The mirrored catalogue itself is not the mirror's to license — follow the TNS data-use terms and cite TNS as they require.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tns_mirror_client-1.0.0.tar.gz (40.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tns_mirror_client-1.0.0-py3-none-any.whl (13.7 kB view details)

Uploaded Python 3

File details

Details for the file tns_mirror_client-1.0.0.tar.gz.

File metadata

  • Download URL: tns_mirror_client-1.0.0.tar.gz
  • Upload date:
  • Size: 40.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for tns_mirror_client-1.0.0.tar.gz
Algorithm Hash digest
SHA256 828f69c33a5da9c810614226d850fafa1633aed18754564a1eef9a974e6ac64c
MD5 f623281a872620e37d53cbf165db22c5
BLAKE2b-256 6c90c097cc04b1beb8a3520766dfa8ec31e45917515f914b4606c0eb0690b44b

See more details on using hashes here.

Provenance

The following attestation bundles were made for tns_mirror_client-1.0.0.tar.gz:

Publisher: release.yml on sarhatabaot/tns-mirror

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file tns_mirror_client-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for tns_mirror_client-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72e2821e25db09c22a6d0ad9dcadee879751707948ce80bdd346423a03312869
MD5 5988c08661c2e8e4da24a6165d4a013e
BLAKE2b-256 1c78431a0e4565145c0e1d17f9621dac6fed628bdf94ab2240aa45545054ff52

See more details on using hashes here.

Provenance

The following attestation bundles were made for tns_mirror_client-1.0.0-py3-none-any.whl:

Publisher: release.yml on sarhatabaot/tns-mirror

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page