Skip to main content

CELL (Coordinate Encoding for Layered Locations) implementation for Python

Project description

cell.py

PyPI Python License

CELL (Coordinate Encoding for Layered Locations) implementation for Python.

Overview

This library implements the CELL Specification v1.0.0.

Implementation Constraints

Constraint Value Rationale
Max dimensions 3 Sufficient for 1D, 2D, 3D boards
Max index value 255 Covers 256×256×256 boards
Max string length 7 "iv256IV" (max for all dimensions at 255)

These constraints enable bounded memory usage, safe parsing, and protection against malicious input in networked applications.

Installation

pip install sashite-cell

Or with uv:

uv add sashite-cell

Usage

Parsing (String → Coordinate)

Convert a CELL string into a Coordinate object.

from sashite_cell import Coordinate

# Standard parsing (raises on error)
coord = Coordinate.parse("e4")
coord.indices     # => (4, 3)
coord.dimensions  # => 2

# 3D coordinate
coord = Coordinate.parse("a1A")
coord.indices  # => (0, 0, 0)

# Invalid input raises ValueError
Coordinate.parse("a0")  # => raises ValueError

Formatting (Coordinate → String)

Convert a Coordinate back to a CELL string.

# From Coordinate object
coord = Coordinate(4, 3)
str(coord)  # => "e4"

# Direct formatting (convenience)
Coordinate.format(2, 2, 2)  # => "c3C"

Validation

from sashite_cell import Coordinate

# Boolean check
Coordinate.is_valid("e4")  # => True
Coordinate.is_valid("a0")  # => False

# Detailed error
Coordinate.validate("a0")  # => raises ValueError("leading zero")

Accessing Coordinate Data

coord = Coordinate.parse("e4")

# Get dimensions count
coord.dimensions  # => 2

# Get indices as tuple
coord.indices  # => (4, 3)

# Access individual index
coord.indices[0]  # => 4
coord.indices[1]  # => 3

# Round-trip conversion
str(Coordinate.parse("e4"))  # => "e4"

API Reference

Types

class Coordinate:
    """
    Represents a parsed CELL coordinate with up to 3 dimensions.

    Attributes:
        indices: Tuple of 0-indexed integers (0-255).
        dimensions: Number of dimensions (1, 2, or 3).
    """

    def __init__(self, *indices: int) -> None:
        """
        Creates a Coordinate from 1 to 3 indices.

        Args:
            *indices: 0-indexed coordinate values (0-255).

        Raises:
            ValueError: If no indices provided, more than 3, or out of range.
        """

    @property
    def dimensions(self) -> int:
        """Returns the number of dimensions (1, 2, or 3)."""

    @property
    def indices(self) -> tuple[int, ...]:
        """Returns the coordinate indices as a tuple."""

    def __str__(self) -> str:
        """Returns the CELL string representation."""

    def __repr__(self) -> str:
        """Returns a debug representation."""

    def __eq__(self, other: object) -> bool:
        """Compares two coordinates for equality."""

    def __hash__(self) -> int:
        """Returns hash for use in sets and dicts."""

Constants

Coordinate.MAX_DIMENSIONS: int = 3
Coordinate.MAX_INDEX_VALUE: int = 255
Coordinate.MAX_STRING_LENGTH: int = 7

Parsing

@classmethod
def parse(cls, string: str) -> "Coordinate":
    """
    Parses a CELL string into a Coordinate.

    Args:
        string: CELL coordinate string.

    Returns:
        Coordinate object.

    Raises:
        ValueError: If the string is not valid.
    """

Formatting

@classmethod
def format(cls, *indices: int) -> str:
    """
    Formats indices into a CELL string.

    Convenience method equivalent to str(Coordinate(*indices)).

    Args:
        *indices: 0-indexed coordinate values.

    Returns:
        CELL string representation.

    Raises:
        ValueError: If indices are invalid.
    """

Validation

@classmethod
def validate(cls, string: str) -> None:
    """
    Validates a CELL string.

    Args:
        string: CELL coordinate string.

    Raises:
        ValueError: With descriptive message if invalid.
    """

@classmethod
def is_valid(cls, string: str) -> bool:
    """
    Reports whether string is a valid CELL coordinate.

    Args:
        string: CELL coordinate string.

    Returns:
        True if valid, False otherwise.
    """

Errors

All parsing and validation errors raise ValueError with descriptive messages:

Message Cause
"empty input" String length is 0
"input exceeds 7 characters" String too long
"must start with lowercase letter" Invalid first character
"unexpected character" Character violates the cyclic sequence
"leading zero" Numeric part starts with '0'
"exceeds 3 dimensions" More than 3 dimensions
"index exceeds 255" Decoded value out of range

Design Principles

  • Bounded values: Index validation (0-255) prevents overflow and DoS attacks
  • Input length limits: Maximum 7 characters protects against malicious input
  • Object-oriented: Coordinate class enables methods and encapsulation
  • Python idioms: is_valid() predicate, __str__ conversion, ValueError for invalid input
  • Immutable coordinates: Tuple indices prevent mutation
  • Hashable: Coordinates can be used in sets and as dict keys
  • Type hints: Full type annotations for IDE support and static analysis
  • No dependencies: Pure Python standard library only

Related Specifications

License

Available as open source under the Apache License 2.0.

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

sashite_cell-2.0.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

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

sashite_cell-2.0.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file sashite_cell-2.0.0.tar.gz.

File metadata

  • Download URL: sashite_cell-2.0.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sashite_cell-2.0.0.tar.gz
Algorithm Hash digest
SHA256 94d359329a8f3c644914428b32db81b0199b2518cc8edd9d85c314b85b6b815d
MD5 07417c6b41e8d6cdd434fbb0b8a3a8e8
BLAKE2b-256 055d854d813554b29dddc597411f277e8ba4f578c24ccc79d31dc47852474ead

See more details on using hashes here.

File details

Details for the file sashite_cell-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: sashite_cell-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for sashite_cell-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9399c3991db44d2026586c9ee8a55c6b016741acbaa042b8c8d110e1cdc5618
MD5 7763167d524b6da600c2430c2ae7b849
BLAKE2b-256 c046fd94b3d590409cb9085ea491c8b3c8083ecff61452f5025a603fb40e7648

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