Skip to main content

SQLAlchemy Ltree Model Mixins

Mixin classes, utilities, and database functions for working with ltree based trees in sqlalchemy and postgresql.

Synopsis

import ltree_models
import testing.postgresql
from sqlalchemy import (
  Column,
  create_engine,
  Integer,
  Text,
)
from sqlalchemy.ext.declarative import (
    declarative_base
)
from sqlalchemy.orm import (
    Session,
)

Base = declarative_base()
class UnorderedNode(Base, ltree_models.LtreeMixin):
    __tablename__ = 'ltree_nodes'
    id = Column(Integer, primary_key=True)

class OrderedNode(Base, ltree_models.OLtreeMixin):
    __tablename__ = 'oltree_nodes'
    id = Column(Integer, primary_key=True)

# Create a new postgresql database in /tmp
db = testing.postgresql.Postgresql()
engine = create_engine(db.url(), echo=False)
ltree_models.add_ltree_extension(engine)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)

# Build and print an unordered ltree
unordered_builder = ltree_models.LtreeBuilder(engine, UnorderedNode)
unordered_builder.populate(2, 3, unordered_builder.path_chooser_sequential)
with Session(engine, future=True) as s:
    unordered_builder.print_tree(s, with_name_path=True)

# Output:
# UnorderedNode(id=1, node_name='r', path=Ltree('r')) r
# UnorderedNode(id=2, node_name='r.0', path=Ltree('r.0')) r/r.0
# UnorderedNode(id=3, node_name='r.0.0', path=Ltree('r.0.0')) r/r.0/r.0.0
# UnorderedNode(id=4, node_name='r.0.1', path=Ltree('r.0.1')) r/r.0/r.0.1
# UnorderedNode(id=5, node_name='r.0.2', path=Ltree('r.0.2')) r/r.0/r.0.2
# UnorderedNode(id=6, node_name='r.1', path=Ltree('r.1')) r/r.1
# UnorderedNode(id=7, node_name='r.1.0', path=Ltree('r.1.0')) r/r.1/r.1.0
# UnorderedNode(id=8, node_name='r.1.1', path=Ltree('r.1.1')) r/r.1/r.1.1
# UnorderedNode(id=9, node_name='r.1.2', path=Ltree('r.1.2')) r/r.1/r.1.2
# UnorderedNode(id=10, node_name='r.2', path=Ltree('r.2')) r/r.2
# UnorderedNode(id=11, node_name='r.2.0', path=Ltree('r.2.0')) r/r.2/r.2.0
# UnorderedNode(id=12, node_name='r.2.1', path=Ltree('r.2.1')) r/r.2/r.2.1
# UnorderedNode(id=13, node_name='r.2.2', path=Ltree('r.2.2')) r/r.2/r.2.2

# Build and print an ordered ltree
ordered_builder = ltree_models.OLtreeBuilder(engine, OrderedNode)
ordered_builder.populate(2, 3, ordered_builder.path_chooser_balanced)
with Session(engine, future=True) as s:
    ordered_builder.print_tree(s, with_name_path=True)

# Output:
# OrderedNode(id=1, node_name='r', path=Ltree('r')) r
# OrderedNode(id=2, node_name='r.0', path=Ltree('r.2500000000000000')) r/r.0
# OrderedNode(id=3, node_name='r.0.0', path=Ltree('r.2500000000000000.2500000000000000')) r/r.0/r.0.0
# OrderedNode(id=4, node_name='r.0.1', path=Ltree('r.2500000000000000.5000000000000000')) r/r.0/r.0.1
# OrderedNode(id=5, node_name='r.0.2', path=Ltree('r.2500000000000000.7500000000000000')) r/r.0/r.0.2
# OrderedNode(id=6, node_name='r.1', path=Ltree('r.5000000000000000')) r/r.1
# OrderedNode(id=7, node_name='r.1.0', path=Ltree('r.5000000000000000.2500000000000000')) r/r.1/r.1.0
# OrderedNode(id=8, node_name='r.1.1', path=Ltree('r.5000000000000000.5000000000000000')) r/r.1/r.1.1
# OrderedNode(id=9, node_name='r.1.2', path=Ltree('r.5000000000000000.7500000000000000')) r/r.1/r.1.2
# OrderedNode(id=10, node_name='r.2', path=Ltree('r.7500000000000000')) r/r.2
# OrderedNode(id=11, node_name='r.2.0', path=Ltree('r.7500000000000000.2500000000000000')) r/r.2/r.2.0
# OrderedNode(id=12, node_name='r.2.1', path=Ltree('r.7500000000000000.5000000000000000')) r/r.2/r.2.1
# OrderedNode(id=13, node_name='r.2.2', path=Ltree('r.7500000000000000.7500000000000000')) r/r.2/r.2.2

Metadata

Release files for ltree-models 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ltree-models 0.1.0
File Size Uploaded Provenance
ltree_models-0.1.0.tar.gz 8.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ltree-models 0.1.0
File Interpreter ABI Platform
ltree_models-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.4 kB

Release files / ltree_models-0.1.0.tar.gz

Download URL ltree_models-0.1.0.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
95d81bf2f243ff55ebc3fc50caac1138e8c7023c33db13a6a901698b0eb896d5
BLAKE2b-256 checksum
How to use checksums
76366f686774e2ed4dcf5c78dac18e6a3d5c837ff91cf62e6d6193a7dceeeaf9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

Release files / ltree_models-0.1.0-py3-none-any.whl

Download URL ltree_models-0.1.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
32ad97dccd8fd622139f5603e6b8ba6315c331489893ba0f8f116eccf032112e
BLAKE2b-256 checksum
How to use checksums
5e1cba99d336bf2329662f1fc9ce9241b38cafeefd058a54453a9db14cd67948
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.6.3 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.10

Security for ltree-models 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page