Links Notation Parser for Python
Python implementation of the Links Notation parser.
Installation
pip install links-notation
Quick Start
from links_notation import Parser
parser = Parser()
links = parser.parse("papa (lovesMama: loves mama)")
# Access parsed links
for link in links:
print(link)
Usage
Basic Parsing
from links_notation import Parser, format_links
parser = Parser()
# Parse simple links
links = parser.parse("(papa: loves mama)")
print(links[0].id) # 'papa'
print(len(links[0].values)) # 2
# Format links back to string
output = format_links(links)
print(output) # (papa: loves mama)
Working with Link Objects
from links_notation import Link
# Create links programmatically
link = Link('parent', [Link('child1'), Link('child2')])
print(str(link)) # (parent: child1 child2)
# Access link properties
print(link.id) # 'parent'
print(link.values[0].id) # 'child1'
# Combine links
combined = link.combine(Link('another'))
print(str(combined)) # ((parent: child1 child2) another)
Indented Syntax
parser = Parser()
# Parse indented notation
text = """3:
papa
loves
mama"""
links = parser.parse(text)
# Produces: (3: papa loves mama)
API Reference
Parser
The main parser class for Links Notation.
__init__(..., comments: bool = True): Create a parser; withcomments=Falsea#is an ordinary character instead of the start of a commentparse(input_text: str) -> List[Link]: Parse Links Notation text into Link objects
Link
Represents a link in Links Notation.
__init__(id: Optional[str] = None, values: Optional[List[Link]] = None)format(less_parentheses: bool = False) -> str: Format as stringsimplify() -> Link: Simplify link structurecombine(other: Link) -> Link: Combine with another link
format_links
Format a list of links into Links Notation.
format_links(links: List[Link], less_parentheses: bool = False) -> str
Examples
Doublets (2-tuple)
parser = Parser()
text = """
papa (lovesMama: loves mama)
son lovesMama
daughter lovesMama
"""
links = parser.parse(text)
Triplets (3-tuple)
text = """
papa has car
mama has house
(papa and mama) are happy
"""
links = parser.parse(text)
Quoted References
# References with special characters need quotes
text = '("has space": "value with: colon")'
links = parser.parse(text)
Multi-line Groups
A parenthesized group opens a nested context: its body starts fresh at indentation level zero and follows the same rules as the root document, so a line break inside parentheses is structure rather than decoration.
value (
id "1"
label "one"
)
The document above parses to (value ((id 1) (label one))) - two children, each
a link of its own - rather than to one flat list in which the boundary between
id and label would be lost. A body that stays on a single line still
collapses to a single link, so (a b c) is unchanged.
text = """value (
id "1"
label "one"
)"""
print(format_links(parser.parse(text)))
# (value ((id 1) (label one)))
Comments
A # hides the rest of the line it stands on, so a document can carry prose
about itself:
# the machines this deploys to
deploy: staging # only staging, for now
Both comments are gone by the time the document is read, leaving the single
link (deploy: staging). A # only opens a comment where a reference could
begin, so a # inside a token (issue#1047) and a # inside a delimited
reference ("#") stay ordinary characters.
A formatter keeps the same rule from the other side: a reference that begins
with a # is written quoted ('#tag'), so a document it writes reads back as
itself.
Comments are on by default, and a parser can be told to read # as an ordinary
character again, for documents written before comments existed:
document = "# the machines this deploys to\ndeploy: staging # only staging, for now\n"
print(format_links(Parser().parse(document))) # (deploy: staging)
print(format_links(Parser(comments=False).parse("# a b\n"))) # (# a b)
Development
Running Tests
# Install development dependencies
pip install pytest
# Run tests
pytest
Building
pip install build
python -m build
Maintenance
Linting and Formatting
This project uses Black for code formatting, isort for import sorting, and flake8 for linting.
Install linting tools:
pip install ".[lint]"
# Or install all dev dependencies
pip install ".[dev]"
Format all code files
black .
isort .
Check formatting (without modifying files)
black --check --diff .
isort --check-only --diff .
flake8 --max-line-length=120
These checks are enforced in CI. Pull requests with unformatted code will fail the lint check.
Pre-commit Hooks
This project uses pre-commit hooks that automatically run Black, isort, and flake8 before commits. To set up pre-commit hooks locally:
# From repository root
pip install pre-commit
pre-commit install
License
This project is released into the public domain under the Unlicense.
Links
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file links_notation-0.19.0.tar.gz.
File metadata
- Download URL: links_notation-0.19.0.tar.gz
- Upload date:
- Size: 35.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6627414a532e2c54d5ca32d9399f3e1ebedd96254f452a5e5c590294a0e144a
|
|
| MD5 |
b5ce3d903e810244027f9fe62bb10848
|
|
| BLAKE2b-256 |
f54a9d57775b76e5c941d93d6561b437aae3eeaa813aba7d9e8414fc3f11e2aa
|
File details
Details for the file links_notation-0.19.0-py3-none-any.whl.
File metadata
- Download URL: links_notation-0.19.0-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1388b5241e5808ff6a5102e964c84c7cff8311c0fb4c06c1ff6795a73a429739
|
|
| MD5 |
f7273b4300ad6e893c01e677ef90fc28
|
|
| BLAKE2b-256 |
88accdd112e64032bd50fde13f040da6a31bb33731c30b6ca72f23db0a98c4d6
|