Quickstart¶
Installation¶
Assure you have at least python >= 3.7.
Using pip:
$ pip install unihan-db
Inside a project managed by uv:
$ uv add unihan-db
Run code without installing by using uvx:
$ uvx --from unihan-db python -c "import unihan_db.bootstrap as bootstrap; print(bootstrap.TABLE_NAME)"
Install into an isolated environment with pipx (exposes the unihan-etl CLI from dependencies):
$ pipx install 'unihan-db' --include-deps
You can upgrade to the latest release with:
$ pip install --upgrade unihan-db
Developmental releases¶
New versions of unihan-db are published to PyPI as alpha, beta, or release candidates. In their
versions you will see notification like a1, b1, and rc1, respectively. 1.10.0b4 would mean
the 4th beta release of 1.10.0 before general availability.
pip:
$ pip install --upgrade --pre unihan-db
pipx:
$ pipx install --suffix=@next 'unihan-db' --pip-args '\--pre' --include-deps --force // Provides the unihan-etl CLI from the dependency set.
uv:
$ uv add unihan-db --prerelease allow
uvx:
$ uvx --from 'unihan-db' --prerelease allow python -c "import unihan_db.bootstrap as bootstrap; print(bootstrap.TABLE_NAME)"
via trunk (can break easily):
Usage¶
#!/usr/bin/env python
"""Example for bootstrapping UNIHAN DB."""
from __future__ import annotations
import logging
import pprint
from sqlalchemy.sql.expression import func
from unihan_db import bootstrap
from unihan_db.tables import Unhn
log = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO, format="%(message)s")
def run(unihan_options: dict[str, object] | None = None) -> None:
"""Initialize Unihan DB via ``bootstrap_unihan()``."""
session = bootstrap.get_session()
bootstrap.bootstrap_unihan(session)
random_row_query = session.query(Unhn).order_by(func.random()).limit(1)
assert random_row_query is not None
random_row = random_row_query.first()
log.info(pprint.pformat(bootstrap.to_dict(random_row)))
assert random_row is not None
log.info(pprint.pformat(random_row.to_dict())) # type:ignore
if __name__ == "__main__":
run()
Pythonics¶
See also