Skip to main content

Glottolog languoid tree as SQLite database

Project description

Latest PyPI Version License Supported Python Versions Monthly downloads

Build

This tool loads the content of the languoids/tree directory from the Glottolog master repo into a normalized SQLite database.

Each file under in that directory contains the definition of one Glottolog languoid. Loading their content into a relational database allows to perform some advanced consistency checks (example) and in general to execute queries that inspect the languoid tree relations in a compact and performant way (e.g. without repeatedly traversing the directory tree).

See pyglottolog for the more general official Python API to work with the repo without a mandatory initial loading step (also provides programmatic access to the references and a convenient command-line interface).

The database can be exported into a ZIP file containing one CSV file for each database table, or written into a single denormalized CSV file with one row per languoid (via a provided SQL query).

As sqlite is the most widely used database, the database file itself (e.g. treedb.sqlite3) can be queried directly from most programming environments. It can also be examined using graphical interfaces such as DBeaver, or via the sqlite3 cli.

Python users can also use the provided SQLAlchemy models to build queries or additional abstractions programmatically using SQLAlchemy core or the ORM (as more maintainable alternative to hand-written SQL queries).

Quickstart

Install treedb (and dependencies):

$ pip install treedb

Clone the Glottolog master repo :

$ git clone https://github.com/glottolog/glottolog.git

Note: treedb expects to find it under ./glottolog/ by default (i.e. under the current directory), use treedb.set_root() to point it to a different path.

Load ./glottolog/languoids/tree/**/md.ini into an in-memory sqlite3 database. Write the denormalized example query into treedb.query.csv:

$ python -c "import treedb; treedb.load(); treedb.write_csv()"

Usage from Python

Start a Python shell:

$ python

Import the package:

>>> import treedb

Use treedb.iterlanguoids() to iterate over languoids as (<path>, dict) pairs:

>>> next(treedb.iterlanguoids())
(('abin1243',), {'id': 'abin1243', 'parent_id': None, 'level': 'language', ...

Note: This is a low-level interface, which does not require loading.

Load the database into treedb.sqlite3 (and set the default engine):

>>> treedb.load('treedb.sqlite3')
...
<treedb._proxies.SqliteEngineProxy filename='treedb.sqlite3' ...>

Run consistency checks:

>>> treedb.check()
...
True

Export into a ZIP file containing one CSV file per database table:

>>> treedb.csv_zipfile()
...Path('treedb.zip')

Execute the example query and write it into a CSV file with one row per languoid:

>>> treedb.write_csv()
...Path('treedb.query.csv')

Rebuild the database (e.g. after an update):

>>> treedb.load(rebuild=True)
...
<treedb._proxies.SqliteEngineProxy filename='treedb.sqlite3' ...>

Execute a simple query with sqlalchemy core and write it to a CSV file:

>>> import sqlalchemy as sa
>>> treedb.write_csv(sa.select(treedb.Languoid), filename='languoids.csv')
...Path('languoids.csv')

Get one row from the languoid table via sqlalchemy core (in Glottocode order):

>>> next(treedb.iterrows(sa.select(treedb.Languoid)))
('3adt1234', '3Ad-Tekles', 'dialect', 'nort3292', None, None, None, None)

Get one Languoid model instance via sqlalchemy orm (in Glottocode order):

>>> session = treedb.Session()
>>> session.query(treedb.Languoid).first()
<Languoid id='3adt1234' level='dialect' name='3Ad-Tekles'>
>>> session.close()

See also

License

This tool is distributed under the MIT license.

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

treedb-2.7.1.tar.gz (460.5 kB view details)

Uploaded Source

Built Distribution

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

treedb-2.7.1-py3-none-any.whl (70.4 kB view details)

Uploaded Python 3

File details

Details for the file treedb-2.7.1.tar.gz.

File metadata

  • Download URL: treedb-2.7.1.tar.gz
  • Upload date:
  • Size: 460.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for treedb-2.7.1.tar.gz
Algorithm Hash digest
SHA256 fbdc63c13a1b2e632133fa6b6964066a71bb1fb50d68c79736893e5d07be6e6c
MD5 3f3a575278e0ef55ba66a07805fb3a72
BLAKE2b-256 565c11f232e87bf190cb22a990010756f29ca6c8f7ab66210f438e24611defe5

See more details on using hashes here.

File details

Details for the file treedb-2.7.1-py3-none-any.whl.

File metadata

  • Download URL: treedb-2.7.1-py3-none-any.whl
  • Upload date:
  • Size: 70.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for treedb-2.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7219994a9c7f30babdeedbf3709599dc6a785df764b50d8a450dacde869388e
MD5 dac5f7b417081390854638bad0c70b82
BLAKE2b-256 7a08d5a1d6ea848a62d1e899c069f487cd4358c9d5d17d5dac9762cf2e5bc99b

See more details on using hashes here.

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