sqlalchemy models for ltree.
Project description
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
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
ltree_models-0.1.0.tar.gz
(8.8 kB
view details)
Built Distribution
File details
Details for the file ltree_models-0.1.0.tar.gz
.
File metadata
- Download URL: ltree_models-0.1.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 95d81bf2f243ff55ebc3fc50caac1138e8c7023c33db13a6a901698b0eb896d5 |
|
MD5 | 83eab5cc3b062c739be5193affe68ba4 |
|
BLAKE2b-256 | 76366f686774e2ed4dcf5c78dac18e6a3d5c837ff91cf62e6d6193a7dceeeaf9 |
File details
Details for the file ltree_models-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: ltree_models-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32ad97dccd8fd622139f5603e6b8ba6315c331489893ba0f8f116eccf032112e |
|
MD5 | b746bffbaffa434ec475245c2addb783 |
|
BLAKE2b-256 | 5e1cba99d336bf2329662f1fc9ce9241b38cafeefd058a54453a9db14cd67948 |