Package: treedb
This tool loads the content of thelanguoids/treedirectory from theGlottologmaster repointo a normalizedSQLitedatabase.Each file under in that directory contains the definition of one Glottologlanguoid. 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).Seepyglottologfor the more general official Python API to work with the repo
without a mandatory initial loading step (also provides programmatic access to
thereferencesand 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 providedSQL query).Assqliteis themost widely useddatabase, 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 asDBeaver, or via thesqlite3 cli.Python users can also use the providedSQLAlchemymodelsto build queries or
additional abstractions programmatically usingSQLAlchemy coreor theORM(as more maintainable alternative to hand-written SQL queries).LinksGitHub:https://github.com/glottolog/treedbPyPI:https://pypi.org/project/treedb/Example:https://nbviewer.jupyter.org/github/glottolog/treedb/blob/master/Stats.ipynbChangelog:https://github.com/glottolog/treedb/blob/master/CHANGES.rstIssue Tracker:https://github.com/glottolog/treedb/issuesDownload:https://pypi.org/project/treedb/#filesQuickstartInstalltreedb(and dependencies):$pipinstalltreedbClone the Glottologmaster repo:$gitclonehttps://github.com/glottolog/glottolog.gitNote:treedbexpects to find it under./glottolog/by default (i.e. under
the current directory), usetreedb.set_root()to point it to a different
path.Load./glottolog/languoids/tree/**/md.iniinto an in-memorysqlite3database.
Write the denormalized example query intotreedb.query.csv:$python-c"import treedb; treedb.load(); treedb.write_csv()"Usage from PythonStart a Python shell:$pythonImport the package:>>>importtreedbUsetreedb.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 intotreedb.sqlite3(and set the defaultengine):>>>treedb.load('treedb.sqlite3')...<treedb._proxies.SqliteEngineProxyfilename='treedb.sqlite3'...>Run consistency checks:>>>treedb.check()...TrueExport 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.SqliteEngineProxyfilename='treedb.sqlite3'...>Execute a simple query withsqlalchemycore and write it to a CSV file:>>>importsqlalchemyassa>>>treedb.write_csv(sa.select(treedb.Languoid),filename='languoids.csv')...Path('languoids.csv')Get one row from thelanguoidtable viasqlalchemycore (in Glottocode order):>>>next(treedb.iterrows(sa.select(treedb.Languoid)))('3adt1234','3Ad-Tekles','dialect','nort3292',None,None,None,None)Get oneLanguoidmodel instance viasqlalchemyorm (in Glottocode order):>>>session=treedb.Session()>>>session.query(treedb.Languoid).first()<Languoidid='3adt1234'level='dialect'name='3Ad-Tekles'>>>>session.close()See alsopyglottolog– official Python API to accesshttps://github.com/glottolog/glottologLicenseThis tool is distributed under theMIT license.