Metadata-Version: 2.4
Name: py-lattix
Version: 0.1.0
Summary: A high-performance, hierarchical, thread-safe mapping library for Python.
Project-URL: Homepage, https://github.com/YuHao-Yeh/py-lattix
Project-URL: Documentation, https://github.com/YuHao-Yeh/py-lattix#readme
Project-URL: Repository, https://github.com/YuHao-Yeh/py-lattix
Project-URL: Issues, https://github.com/YuHao-Yeh/py-lattix/issues
Author-email: Yeh Yuhao <yuhao200.ok@gmail.com>
License: BSD 3-Clause License
        
        Copyright (c) 2026, Yeh Yuhao
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
License-File: LICENSE
Keywords: attrdict,config,dot-access,hierarchical,mapping,thread-safe
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.8
Requires-Dist: typing-extensions>=4.0; python_version <= '3.10'
Provides-Extra: check
Requires-Dist: mypy>=1.11; extra == 'check'
Requires-Dist: ruff; extra == 'check'
Requires-Dist: types-pyyaml; extra == 'check'
Provides-Extra: dev
Requires-Dist: black; extra == 'dev'
Requires-Dist: msgpack>=1.0; extra == 'dev'
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: nox; extra == 'dev'
Requires-Dist: numpy; extra == 'dev'
Requires-Dist: orjson>=3.0; extra == 'dev'
Requires-Dist: pandas; extra == 'dev'
Requires-Dist: pre-commit; extra == 'dev'
Requires-Dist: pytest; extra == 'dev'
Requires-Dist: pytest-cov; extra == 'dev'
Requires-Dist: pyyaml>=6.0; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Requires-Dist: types-pyyaml; extra == 'dev'
Requires-Dist: typing-extensions>=4.0; extra == 'dev'
Provides-Extra: full
Requires-Dist: msgpack>=1.0; extra == 'full'
Requires-Dist: numpy; extra == 'full'
Requires-Dist: orjson>=3.0; extra == 'full'
Requires-Dist: pandas; extra == 'full'
Requires-Dist: pyyaml>=6.0; extra == 'full'
Provides-Extra: msgpack
Requires-Dist: msgpack>=1.0; extra == 'msgpack'
Provides-Extra: orjson
Requires-Dist: orjson>=3.0; extra == 'orjson'
Provides-Extra: test
Requires-Dist: msgpack>=1.0; extra == 'test'
Requires-Dist: numpy; extra == 'test'
Requires-Dist: orjson>=3.0; extra == 'test'
Requires-Dist: pandas; extra == 'test'
Requires-Dist: pytest; extra == 'test'
Requires-Dist: pytest-cov; extra == 'test'
Requires-Dist: pyyaml>=6.0; extra == 'test'
Requires-Dist: typing-extensions>=4.0; extra == 'test'
Provides-Extra: torch
Requires-Dist: torch; extra == 'torch'
Provides-Extra: xarray
Requires-Dist: xarray; extra == 'xarray'
Provides-Extra: yaml
Requires-Dist: pyyaml>=6.0; extra == 'yaml'
Description-Content-Type: text/markdown

# Lattix

<!-- GitHub & CI Status -->
[![Tests](https://github.com/YuHao-Yeh/py-lattix/actions/workflows/test.yml/badge.svg)](https://github.com/YuHao-Yeh/py-lattix/actions)
[![Coverage Status](https://coveralls.io/repos/github/YuHao-Yeh/py-lattix/badge.svg)](https://coveralls.io/github/YuHao-Yeh/py-lattix)
[![Python Version](https://img.shields.io/badge/python-3.8%2B-blue.svg)](https://www.python.org/downloads/)
[![License: BSD](https://img.shields.io/badge/License-BSD%203--Clause-blue.svg)](https://opensource.org/licenses/BSD-3-Clause)

<!-- PyPI Metadata -->
[![PyPI Version](https://img.shields.io/pypi/v/py-lattix.svg)](https://pypi.org/project/py-lattix/)
[![PyPI Python Versions](https://img.shields.io/pypi/pyversions/py-lattix.svg)](https://pypi.org/project/py-lattix/)

<!-- Quality & Tooling -->
![mypy](https://img.shields.io/badge/mypy-strict-blue)
![ruff](https://img.shields.io/badge/lint-ruff-red)
![black](https://img.shields.io/badge/code%20style-black-000000)


**Lattix** is a high-performance, hierarchical mapping library designed for complex data pipelines and multi-threaded environments. It combines the flexibility of a dictionary with the power of tree-like structures, offering dot-access, path-traversal, and a unique **inherited locking mechanism** for atomic subtree operations.

```python
from lattix import Lattix

conf = Lattix(lazy_create=True)

conf.database.credentials.user = "admin"
conf["database/credentials/port"] = 5432

conf.database.credentials.to_dict()
# {'user': 'admin', 'port': 5432}
```
---

## Key Features

- **Hierarchical Access**: Use dot-notation (`d.user.profile.id`) or path-strings (`d["user/profile/id"]`) with configurable separators.
- **Lazy Creation**: Automatically build nested structures on the fly with `lazy_create=True`.
- **Thread-Safe Inheritance**: Advanced lock-sharing where children nodes inherit their parent's `RLock`, ensuring consistent synchronization across entire subtrees.
- **Immutability (Freeze)**: Protect your data from accidental changes in production using `d.freeze()`.
- **Set-Like Logic**: Perform deep merges, intersections, and differences using standard operators: `&`, `|`, `-`, and `^`.
- **Data-Science Ready**: Built-in, lazy-loading adapters for **NumPy**, **Pandas**, **PyTorch**, and **Xarray**. No hard dependencies required.
- **Enhanced Serialization**: Native support for high-fidelity YAML (preserving `Path`, `Decimal`, `datetime`), JSON, Msgpack, and Orjson.
- **First-class Typing**: Fully typed with Python generics and `.pyi` stubs for perfect autocompletion in VS Code and PyCharm.

---

## Installation

### 1. Install via PyPI (Recommended)
```bash
# Basic
pip install py-lattix

# With all adapters (NumPy, Pandas, etc.)
pip install "py-lattix[full]"
```
### 2. Install via Github:

```bash
# Basic
pip install git+https://github.com/YuHao-Yeh/py-lattix.git

# With all adapters (NumPy, Pandas, YAML support, etc.)
pip install "py-lattix[full] @ git+https://github.com/YuHao-Yeh/py-lattix.git"
```

### 3. Install from Source

```bash
# 1. Clone the repository
$ git clone https://github.com/YuHao-Yeh/py-lattix
$ cd py-lattix

# 2. Install in editable mode
pip install -e

# 3. (Optional) Install testing dependencies
pip install -e ".[test,full]"
```

---

## Quick Start

### Basic Usage & Path Access
```python
from lattix import Lattix

# Initialize with data or kwargs
conf = Lattix(meta={"version": "1.0"}, lazy_create=True, sep=":")

# Path-style access
conf["app:settings:theme"] = "dark"

# Dot-style access (even for paths created above)
print(conf.app.settings.theme)  # Output: "dark"

# Lazy creation
conf.database.connection.timeout = 30

# Convert entire tree back to a plain serializable dict
conf.to_dict()
# {'meta': {'version': '1.0'}, 'app': {'settings': {'theme': 'dark'}}, 'database': {'connection': {'timeout': 30}}}
```

## Inherited Thread Safety

Lattix solves the "Subtree Locking" problem. When a node is locked, all its children (present or future) share the same lock instance.

```python
import threading

tree = Lattix(enable_lock=True, lazy_create=True)

def update_config():
    with tree: # Acquires global lock for the whole tree
        tree.server.status = "upgrading"
        tree.server.port = 9000
        tree.server.last_check = "2026-01-10"
        # No other thread can modify 'tree' or any of its children 
        # until this block finishes.

threading.Thread(target=update_config).start()
```

### Production Safety: Freezing
Prevent accidental modifications to your configuration once it is loaded.

```python
conf = Lattix({"api": {"key": "secret"}})
conf.freeze()

conf.api.key = "new-key" 
# Raises: ModificationDeniedError
```

---

## Advanced Operations

### Logical Operations (Deep Merging)
```python
base = Lattix({"api": {"host": "localhost", "port": 8080}})
user = Lattix({"api": {"port": 9000}, "debug": True})

# Deep Union (Merge)
final = base | user
# Result: api.host=localhost (preserved), api.port=9000 (overwritten), debug=True

# Intersection (Common Keys)
common = base & user
# Result: api.port=9000 (overwritten)
```

### SQL-style Joins
```python
d1 = Lattix({"a": 1, "b": 2})
d2 = Lattix({"b": 20, "c": 30})

# Inner join: keys existing in both
res = d1.join(d2, how="inner")
# Result: Lattix({'b': (2, 20)})
```

### Data Science Integrations
Lattix recognizes complex types and handles them automatically during serialization.

```python
import numpy as np
import pandas as pd
import torch

d = Lattix(lazy_create=True)
d.array = np.array([1, 2, 3])
d.df = pd.DataFrame({"A": [1, 2], "B": [3, 4]})
d.tensor = torch.randn(3, 3)

# Serialization handles NumPy/Pandas/Torch -> Python conversion automatically
print(d.json())
```

### Enhanced YAML
Lattix preserves complex Python types in YAML that standard loaders usually break.

```python
from decimal import Decimal
from pathlib import Path

d = Lattix({"price": Decimal("19.99"), "path": Path("/usr/bin")})

# Export with custom YAML tags
yaml_out = d.yaml(enhanced=True)
# Output:
# price: !decimal '19.99'
# path: !path '/usr/bin'
```

---

## Diagnostics & Testing

Verify your environment and adapter availability via the CLI:

```bash
python -m lattix
```

Run internal doctests to verify library integrity:

```bash
python -m lattix --test
```

---

## Similar Projects

- [addict](https://github.com/mewwts/addict): Lightweight recursive dictionary with dot-access.
- [Easydict](https://github.com/XuehaiPan/TreeDict): A fast and full-featured dict-like tree container.
- [python-box](https://github.com/cdgriffith/Box): Robust dictionary wrapper with path and dot-access support.

## License

Lattix is released under the **BSD License**. See the [LICENSE](LICENSE) for details.

## Contributing
Contributions are welcome! Please feel free to submit a Pull Request or open an issue on GitHub.

We maintain a high test coverage. To run the suite:
1. Clone the repo: `git clone https://github.com/YuHao-Yeh/py-lattix`
2. Install dev dependencies: `pip install -e ".[test]"`
3. Run tests: `pytest`
4. Ensure typing is correct: `mypy src/lattix`
