Skip to main content

Coeden is a idiomatic library to work with trees in python

Project description

Coeden for python

Coeden is a library to make work with tree data structures ergonimic and ideomatic in python.

Disclaimer

This library is in an early alpha stage, expect major changes in the API in subsequent versions.

Getting started

Requirements

Python >= 3.10.x

Install

You can install it with pip:

python3 -m pip install coeden

Documentation

Create a tree

In coeden there is no tree object, the trees are made of nodes and from every node you can create new leafs (nodes without childs).

Let's create a simple tree with 3 levels of depth:

from coeden import node

color_tree = Node("colors")

and this node can grow new leafs

color_tree["colors"].new_leaf("red")
color_tree["colors"].new_leaf("blue")
color_tree["colors"].new_leaf("green")

then blue can grow a couple of leafs or typing the whole chain

color_tree["colors"]["blue"].new_leaf("marine")
color_tree["colors"]["blue"].new_leaf("celeste")

also, the red node can be stored in a variable and grow some leafs from it

red = color_tree["colors]["red"]
red.new_leaf["dark"]
red.new_leaf["velvet"]

The current tree can be printed like this

color_tree.print_tree()

# Output
colors
  red
    dark
    velvet
  blue
    marine
    celeste
  green

but as all nodes are the starting point of their own tree blue can print its own tree, that happens to be a subtree of colors because blue belong to colors

blue.print_tree()

# Output
blue
  marine
  celeste

Traversing the tree

Trees are traversed using the index operator []. Using the brackets you can traverse the previous tree from the root to the velvet node for example

if color_tree["red"]["velvet"] != None:
    ...    

this is kind of a nested dictionary (and they are internaly nested dictionaries indeed) but it hides some interesting features that normal dicts do not.

Traverse inexistent nodes

Imagine you want to check the existance of "dark gray" in the tree with dictionaries you have to do something like:

if color_tree["gray"] != None and color_tree["gray"]["dark"] != None:
    ...

or at least wrap it in a try/except. But coeden allows to make free test for existance

if color_tree["gray"]["dark"] != None:
    print("Nice color!")
else:
    print("Nah, not a real color")

# Output
Nah, not a real color 

or even better you can create al the inexistent nodes in one call with

color_tree["gray"]["dark"].create_all()
color_tree.print_tree()

# Output
colors
  red
    dark
    velvet
  blue
    marine
    celeste
  green
  gray
    dark

Wildcards

Wilcards allow to consider every node of a level, for instance, we can search for marine node without knowing it is blue.

marine = color_tree["__*__"]["marine"]  # Marine is an iterable set of nodes

but wildcards return sets of nodes and not nodes so there are two options, if you know that there is going to be only one node you can use the special key "__?__" that converts the set to a node again if its length is exactly one. If there are more the sets can be iterated in a loop.

marine = color_tree["__*__"]["marine"]["__?__"]  # Now it is the node 
print("marine parent: " + marine.parent.key)

for node in color_tree["__*__"]["dark"]:
   print("dark parent: " + node.key)

# Output
marine parent: blue
dark parent: red
dark parent: gray 

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

coeden-0.2.3.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

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

coeden-0.2.3-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file coeden-0.2.3.tar.gz.

File metadata

  • Download URL: coeden-0.2.3.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for coeden-0.2.3.tar.gz
Algorithm Hash digest
SHA256 5148ec4dcacf13fe89b98a9045371d80afb952494524a4f5e8a7ab0f5f3c9e3e
MD5 f2be8551fc65211299115de5e17a4d31
BLAKE2b-256 1e467f78da0275a8afe8422fe15a46225bb89ee6aaffb5674afc7b2e79e33bb4

See more details on using hashes here.

File details

Details for the file coeden-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: coeden-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for coeden-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 331f4f2c2e4fb8e781c734f01644cbe2916c14e0e22f0de6eda4ce9b3cd051b1
MD5 2e959f36b7240d8eece522b14bf746b8
BLAKE2b-256 5ffd29b217489b1d826cecfd2c3123d08cbb6d0a53111f7466eb76e42af10371

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