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.5matches 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
828f69c33a5da9c810614226d850fafa1633aed18754564a1eef9a974e6ac64c
|
|
| MD5 |
f623281a872620e37d53cbf165db22c5
|
|
| BLAKE2b-256 |
6c90c097cc04b1beb8a3520766dfa8ec31e45917515f914b4606c0eb0690b44b
|
Provenance
The following attestation bundles were made for tns_mirror_client-1.0.0.tar.gz:
Publisher:
release.yml on sarhatabaot/tns-mirror
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tns_mirror_client-1.0.0.tar.gz -
Subject digest:
828f69c33a5da9c810614226d850fafa1633aed18754564a1eef9a974e6ac64c - Sigstore transparency entry: 2256120732
- Sigstore integration time:
-
Permalink:
sarhatabaot/tns-mirror@ab0e0277cc6419d2e22e2df7e72258b19349d18d -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/sarhatabaot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ab0e0277cc6419d2e22e2df7e72258b19349d18d -
Trigger Event:
release
-
Statement type:
File details
Details for the file tns_mirror_client-1.0.0-py3-none-any.whl.
File metadata
- Download URL: tns_mirror_client-1.0.0-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72e2821e25db09c22a6d0ad9dcadee879751707948ce80bdd346423a03312869
|
|
| MD5 |
5988c08661c2e8e4da24a6165d4a013e
|
|
| BLAKE2b-256 |
1c78431a0e4565145c0e1d17f9621dac6fed628bdf94ab2240aa45545054ff52
|
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
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
tns_mirror_client-1.0.0-py3-none-any.whl -
Subject digest:
72e2821e25db09c22a6d0ad9dcadee879751707948ce80bdd346423a03312869 - Sigstore transparency entry: 2256120738
- Sigstore integration time:
-
Permalink:
sarhatabaot/tns-mirror@ab0e0277cc6419d2e22e2df7e72258b19349d18d -
Branch / Tag:
refs/tags/1.0.0 - Owner: https://github.com/sarhatabaot
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@ab0e0277cc6419d2e22e2df7e72258b19349d18d -
Trigger Event:
release
-
Statement type: