Giter VIP home page Giter VIP logo

py-ltree's Introduction

Python ltree implementation

ltree is a sequence of labels that can be used to represent a hierarchical tree-like structure. The PostgreSQL database provides an ltree data type with very powerful indexing functionalities, making it a practical way to store tree-like information in a relational database, often more flexible or performing than implementing an adjacency list or nested set.

This extension module contains objects to help manipulating ltree in Python and interacting with the PostgreSQL data types using psycopg

Ltree object

Ltree objects can be created from a string containing a dot-separated sequence of labels, a python sequence of labels or multiple arguments. Valid labels are sequences of alphanumeric ascii characters and underscore. Empty strings and None are skipped.:

>>> from ltree import Ltree

>>> Ltree('a.b.c')
Ltree('a.b.c')

>>> Ltree(['a', '', 'b', 0, None, 'c'])
Ltree('a.b.0.c')

>>> Ltree('a', '', 'b', 0, None, 'c')
Ltree('a.b.0.c')

Ltree objects can also be concatenated to other ltree or other objects representing valid labels. They can be sliced as a normal Python sequence: slicing will return a new Ltree object; accessing by index will return the label as a string:

>>> 'first' + Ltree('a.b') + Ltree('c') + 42
Ltree('first.a.b.c.42')

>>> Ltree('a.b.c.d.e.f.g')[:2]
Ltree('a.b')

>>> Ltree('a.b.c.d.e.f.g')[5:]
Ltree('f.g')

>>> Ltree('a.b.c.d.e.f.g')[1:3]
Ltree('b.c')

>>> Ltree('a.b.c.d.e.f.g')[2]
'c'

Lquery object

The Lquery object works similarly to Ltree but also supports star symbols. Sequence of stars get merged together (because PostgreSQL lquery not always does the right thing with two stars in a row):

>>> from ltree import Lquery

>>> Lquery('a.*.b')
Lquery('a.*.b')

>>> Lquery('a.*{1}') + Lquery('*{2}.b')
Lquery('a.*{3}.b')

>>> Lquery('a.*{1}') + Lquery('*.b')
Lquery('a.*{1,}.b')

Using with psycopg2

In order to pass Ltree and Lquery objects to psycopg2 you can register the ltree adapters using the ltree.pg.register_ltree() function. Because the ltree type doesn't have a fixed OID, the function takes a connection or cursor as argument to look it up:

>>> import psycopg2
>>> cnn = psycopg2.connect('')

>>> import ltree.pg
>>> ltree.pg.register_ltree(cnn)

Once the adaptation bits are in place shuttling Ltree back and forth the database is a breeze:

>>> cur = cnn.cursor()
>>> cur.execute('select %s::ltree', [Ltree('a.b.c')])
>>> cur.fetchone()[0]
Ltree('a.b.c')

>>> cur.execute(
...     "select %s::ltree ~ %s::lquery",
...     [Ltree('a.b.c'), Lquery('a.*')])
>>> cur.fetchone()[0]
True

Using with Django

The ltree.django module contains some Django helper. Importing it will registers the lqmatch lookup, which can be used to filter a model for lquery matching (the ~ operator):

objs = MyModel.objects.filter(code__lqmatch=Lquery('a.b.*'))  #doctest: +SKIP

py-ltree's People

Contributors

dvarrazzo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.