Skip to main content

Library to help create meaningful structures from yaml or json.

Project description

Propertree

Map Python data structures to classes and create tree of resolvable objects.

Overview

This library provides a set of classes that can be used to take Python data structures and map them to a set of properties organised in a tree-like structure. The basic idea is that a Python object with one or more attributes can be represented by a unique key in a dictionary whereby the the value of that key represents the internal makeup of the object i.e. attributes etc.

The basic design is as follows:

A PTreeSection object takes Python data structures dict/list/literal after having been converted from YAML or JSON. This input is treated like a tree of properties whereby each level of a structure equates to a branch containing a set of "property overrides" which are identified by a unique root key that maps to an implementation of PTreeOverrideBase. Descendant lists, dictionaries or unknown literals are treated new branches.

Once the whole tree has been mapped, objects can be retreived by either iterating over the root object or accessing them directly as attributes.

For example lets say we have the following YAML:

config:
  fire:
    danger:
      level: high
  banana:
    danger:
      level: low

And the following accompanying Python code:

from propertree.propertree2 import PTreeOverrideBase, PTreeSection

class Config(PTreeOverrideBase):
  override_keys = ['config']


root = PTreeSection(MYYAML)

We can then access the config as follows:

print(root.config.fire.danger.level)
print(root.config.banana.danger.level)

Property Inheritance

Property inheritance is supported by passing down all properties identified at a branch level to all descendent branches. This allows property objects to access any property within it's call chain although it would be the most recently overriden version if one exists. For example:

input: /etc/foo
checks:
  chk1:
    input: /etc/bar
    condition: C1
  chk2:
    condition: C2

In the above, checks.chk1.input would be "/etc/bar" whereas checks.chk2.input would be "/etc/foo".

Mapped Properties

It is possible to compose complex properties made up of one or more "member" properties. These are called mapped properties and are provided by the PTreeMappedOverrideBase class. A mapped property has two parts; a primary and its members. A property can only be a member of one mapped property (i.e. be associated with a single primary). These properties also have the special feature that allow them to be defined either explicitly using their full construct i.e. primary and members or implicitly using just their members. If the latter form is used, the primary is implicitly created such that when the members are accessed it is always done through the primary.

For example, here is some code to define a mapped property:

from propertree.propertree2 import PTreeOverrideBase, PTreeMappedOverrideBase, PTreeSection

class MapPrimary(PTreeMappedOverrideBase):
  override_keys = ['mapprimary']
  override_members = ['member1']


class Member1(PTreeOverrideBase):
  override_keys = ['member1']

...

the following will both behave the same when accessed:

The explicit declaration:

mapprimary:
  member1:
    attr1:

And the counterpart implicit declaration:

member1:
  attr1:

Are both accessed as follows:

mapprimary.member1.attr1

Logical Groupings

Sometimes it might be useful to represent properties or content within a property as a logical function. To achieve this, propertree has builtin logical operator properties which map to the PTreeLogicalGrouping class. This class provides a default implementation of logical operations that can be used by any property. Grouped items are expected to have a result attribute that returns a boolean result such that the respective logical operator of that grouping is then applied to the set of all results.

In the following example we have a top-level operator with two items, each of which is itself an operator, the first having two property items and second having one. This equates to AND(OR(P1, P2), NOT(P3)).

and:
  or: [P1, P2]
  not: P3

Example code for this would look like:

from propertree.propertree2 import PTreeSection

root = PTreeSection(MYYAML)
result = root.and.result

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

propertree-1.1.0.post10.tar.gz (45.4 kB view details)

Uploaded Source

Built Distribution

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

propertree-1.1.0.post10-py3-none-any.whl (51.0 kB view details)

Uploaded Python 3

File details

Details for the file propertree-1.1.0.post10.tar.gz.

File metadata

  • Download URL: propertree-1.1.0.post10.tar.gz
  • Upload date:
  • Size: 45.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for propertree-1.1.0.post10.tar.gz
Algorithm Hash digest
SHA256 455525e27a7d4686dbf32f0f731754e3ecb090c35f09e483bf52e238e39b6e0c
MD5 dd54d86497c297cd5689c7fec6077be1
BLAKE2b-256 836c2f8f9fcf23115ef960e4aa37da44e802ae78d24e98935751f93647e7880c

See more details on using hashes here.

File details

Details for the file propertree-1.1.0.post10-py3-none-any.whl.

File metadata

File hashes

Hashes for propertree-1.1.0.post10-py3-none-any.whl
Algorithm Hash digest
SHA256 e73a7ba9c41ff8a29b40c463fdd68f0195e6caea1dad57d6124cd8044ad5f285
MD5 9e1aec3652514698131e791451e67396
BLAKE2b-256 9c55f34f157ac71b12f088aa00b078147ef34a8a40e16d2d6c077870ef8077dd

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