Skip to main content

Hierarchical path representation and manipulation for MoaT

Project description

Hierarchical Paths

% start main % start synopsis

MoaT Paths are immutable sequences that can be represented as dot-separated or slash-separated strings, with special encoding for various data types.

MoaT paths are typically entered and displayed with dots as separators and colon as "special" markers. For example, ext:1.set is a three-element path consisting of two strings with an integer in between.

There's a secondary representation that uses slashes (ext/:1/set) for interfacing with the file system or MQTT.

See pydoc moat.lib.path.Path for details.

The MoaT ecosystem uses Path objects as hierarchical accessors for objects and data.

% end synopsis

Usage

Creating paths

from moat.lib.path import Path, P

# Create paths from elements
path = Path()  # empty path

path = P("foo.bar.baz")  # dot notation
assert path == Path.build(("foo", "bar", "baz"))  # assemble from tuples, lists etc.

# Create from various types
path = Path.build(("config", 42, True))  # Mixed types
assert path == P("config:x2a:t")  # config, 0x2a (42), True

Path representation

Paths can be represented in two forms:

Dot notation: Elements separated by dots

path = P("foo.bar.baz")
str(path)  # "foo.bar.baz"

Slash notation: Elements separated by slashes

path = P("foo.bar.baz")
path.slash  # "foo/bar/baz"

Special codes

MoaT paths support non-string path elements. The most common are:

# Inline escapes (within an element):
#   ::  escapes : colon
#   :.  escapes . dot
#   :_  escapes   space
#   :|  escapes / slash

# Type prefixes (start a new element):
#   :t   True
#   :f   False
#   :e   empty string
#   :n   None
#   :42  Simple integer
#   :xAB Hex integer (0xAB)
#   :b01 Binary integer (0b01)
#   :yAB Bytestring, hex encoding
#   :sAB Bytestring, base64 encoding

path = P("config:x2a:t")  # Represents ("config", 42, True)

Path operations

from moat.lib.path import Path

path1 = P("foo.bar")
path2 = P("baz.qux")

# Concatenation
combined = path1 + path2  # Path("foo", "bar", "baz", "qux")

# Element access
path1[0]  # "foo"
path1[-1]  # "bar"

# Slicing
path = Path("a.b.c.d")
path[1:3]  # ("b", "c")

# Length
len(path)  # 4

# Iteration
for elem in path:
    print(elem)

# Appending single element
path = path / "new"  # a.b.c.d.new

% end main

Serialization

MoaT paths integrate with serialization formats:

  • YAML: Use a !P prefix for Path objects (dot notation)
  • CBOR: List marked with tag 39 ("Identifier")
  • Msgpack: extension 3 encapsulates path elements

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

moat_lib_path-0.3.2.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

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

moat_lib_path-0.3.2-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file moat_lib_path-0.3.2.tar.gz.

File metadata

  • Download URL: moat_lib_path-0.3.2.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for moat_lib_path-0.3.2.tar.gz
Algorithm Hash digest
SHA256 bc4374f7d4f5eae09f06ccaf6eb3843bc477450b27fb06abc5eb14715d62d095
MD5 af22584d0ef02bc311ae4a6c874a7f94
BLAKE2b-256 a76ee4497bca9c9c3654e867680c977dd3aa49ef7478c8dde56560434003024a

See more details on using hashes here.

File details

Details for the file moat_lib_path-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: moat_lib_path-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for moat_lib_path-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49d73777ad8794a8ab7759bd6b0764e30d04780407971d4cced1254f19e998f5
MD5 f224af7fc6aa50fe1c4aec6e8dc142e0
BLAKE2b-256 3475d2cefb937320fe6a30c52e952facb29b4e6639d6a61a8aa8e1da7b95b697

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