# Classno
[![pypi](https://img.shields.io/pypi/v/classno.svg)](https://pypi.org/project/classno/)
[![downloads](https://static.pepy.tech/badge/classno)](https://www.pepy.tech/projects/classno)
[![downloads](https://static.pepy.tech/badge/classno/month)](https://www.pepy.tech/projects/classno)
[![versions](https://img.shields.io/pypi/pyversions/classno.svg)](https://github.com/kuderr/classno)
[![license](https://img.shields.io/github/license/kuderr/classno.svg)](https://github.com/kuderr/classno/blob/master/LICENSE)
Classno is a lightweight and extensible Python library for data modeling, schema definition, and validation. It provides a clean and intuitive way to define data classes with various features like type validation, immutability, private fields, and automatic type casting.
## Key Features
- Type hints validation
- Immutable objects
- Private fields
- Automatic type casting
- Customizable comparison behavior
- Default values and factory functions
- Nested object support
- Slots optimization
- Rich comparison methods
## Installation
```bash
pip install classno
```
## Basic Usage
### Simple Data Class
```python
from classno import Classno, field
class User(Classno):
name: str
age: int = 0
email: str = field(default="")
# Create an instance
user = User(name="John", age=30)
```
### Features Configuration
Features can be enabled by setting the `__features__` class attribute:
```python
from classno import Classno, Features
class Config(Classno):
__features__ = Features.VALIDATION | Features.FROZEN
host: str
port: int = 8080
```
Available features:
- `Features.EQ` - Enable equality comparison
- `Features.ORDER` - Enable ordering operations
- `Features.HASH` - Make instances hashable
- `Features.SLOTS` - Use slots for memory optimization
- `Features.FROZEN` - Make instances immutable
- `Features.PRIVATE` - Enable private field access
- `Features.VALIDATION` - Enable type validation
- `Features.LOSSY_AUTOCAST` - Enable automatic type casting
### Field Configuration
Fields can be configured using the `field()` function:
```python
from classno import Classno, field
from datetime import datetime
class Post(Classno):
title: str
content: str = ""
created_at: datetime = field(default_factory=datetime.now)
metadata: dict = field(default_factory=dict, metadata={"indexed": True})
```
### Type Validation
```python
class ValidatedModel(Classno):
__features__ = Features.VALIDATION
numbers: list[int]
mapping: dict[str, float]
# This will raise TypeError if types don't match
model = ValidatedModel(
numbers=[1, 2, 3],
mapping={"a": 1.0, "b": 2.0}
)
```
### Immutable Objects
```python
class ImmutableConfig(Classno):
__features__ = Features.IMMUTABLE # Combines FROZEN, SLOTS, and HASH
host: str
port: int = 8080
config = ImmutableConfig(host="localhost")
# Attempting to modify will raise an exception
config.port = 9000 # Raises Exception
```
### Private Fields
```python
class PrivateFields(Classno):
__features__ = Features.PRIVATE
name: str
secret: str # Can only be accessed with _secret prefix for rw, secret for ro
obj = PrivateFields(name="public")
obj._secret = "hidden" # OK
obj.secret # OK
obj.secret = "hidden" # Raises Exception
```
### Nested Objects
```python
class Address(Classno):
street: str
city: str
class Person(Classno):
name: str
address: Address
# Create nested structure
person = Person(
name="John",
address=Address(street="123 Main St", city="Boston")
)
```
## Customization
### Custom Comparison Keys
```python
class CustomCompare(Classno):
__hash_keys__ = {"id"} # Keys used for hashing
__eq_keys__ = {"id", "name"} # Keys used for equality comparison
__order_keys__ = {"name"} # Keys used for ordering
id: int
name: str
description: str
```
## Best Practices
1. Use type hints for all fields
2. Enable appropriate features based on your needs
3. Use `field()` for complex field configurations
4. Consider using `Features.SLOTS` for better memory usage
5. Enable validation when type safety is important
## Error Handling
The library raises appropriate exceptions for:
- Type validation errors
- Missing required fields
- Immutability violations
- Invalid field access
- Incorrect feature combinations
# Authors
- Dmitriy Kudryavtsev - author - [kuderr](https://github.com/kuderr)
Raw data
{
"_id": null,
"home_page": "https://github.com/kuderr/classno",
"name": "classno",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": "library",
"author": "kuderr",
"author_email": "dakudryavcev@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/71/54/bc504d2b1e28d6f0b1786b76d655bf73a26af762330452cdac5e5414b6ba/classno-0.0.5.tar.gz",
"platform": null,
"description": "# Classno\n\n[![pypi](https://img.shields.io/pypi/v/classno.svg)](https://pypi.org/project/classno/)\n[![downloads](https://static.pepy.tech/badge/classno)](https://www.pepy.tech/projects/classno)\n[![downloads](https://static.pepy.tech/badge/classno/month)](https://www.pepy.tech/projects/classno)\n[![versions](https://img.shields.io/pypi/pyversions/classno.svg)](https://github.com/kuderr/classno)\n[![license](https://img.shields.io/github/license/kuderr/classno.svg)](https://github.com/kuderr/classno/blob/master/LICENSE)\n\nClassno is a lightweight and extensible Python library for data modeling, schema definition, and validation. It provides a clean and intuitive way to define data classes with various features like type validation, immutability, private fields, and automatic type casting.\n\n## Key Features\n\n- Type hints validation\n- Immutable objects\n- Private fields\n- Automatic type casting\n- Customizable comparison behavior\n- Default values and factory functions\n- Nested object support\n- Slots optimization\n- Rich comparison methods\n\n## Installation\n\n```bash\npip install classno\n```\n\n## Basic Usage\n\n### Simple Data Class\n\n```python\nfrom classno import Classno, field\n\nclass User(Classno):\n name: str\n age: int = 0\n email: str = field(default=\"\")\n\n# Create an instance\nuser = User(name=\"John\", age=30)\n```\n\n### Features Configuration\n\nFeatures can be enabled by setting the `__features__` class attribute:\n\n```python\nfrom classno import Classno, Features\n\nclass Config(Classno):\n __features__ = Features.VALIDATION | Features.FROZEN\n\n host: str\n port: int = 8080\n```\n\nAvailable features:\n\n- `Features.EQ` - Enable equality comparison\n- `Features.ORDER` - Enable ordering operations\n- `Features.HASH` - Make instances hashable\n- `Features.SLOTS` - Use slots for memory optimization\n- `Features.FROZEN` - Make instances immutable\n- `Features.PRIVATE` - Enable private field access\n- `Features.VALIDATION` - Enable type validation\n- `Features.LOSSY_AUTOCAST` - Enable automatic type casting\n\n### Field Configuration\n\nFields can be configured using the `field()` function:\n\n```python\nfrom classno import Classno, field\nfrom datetime import datetime\n\nclass Post(Classno):\n title: str\n content: str = \"\"\n created_at: datetime = field(default_factory=datetime.now)\n metadata: dict = field(default_factory=dict, metadata={\"indexed\": True})\n```\n\n### Type Validation\n\n```python\nclass ValidatedModel(Classno):\n __features__ = Features.VALIDATION\n\n numbers: list[int]\n mapping: dict[str, float]\n\n# This will raise TypeError if types don't match\nmodel = ValidatedModel(\n numbers=[1, 2, 3],\n mapping={\"a\": 1.0, \"b\": 2.0}\n)\n```\n\n### Immutable Objects\n\n```python\nclass ImmutableConfig(Classno):\n __features__ = Features.IMMUTABLE # Combines FROZEN, SLOTS, and HASH\n\n host: str\n port: int = 8080\n\nconfig = ImmutableConfig(host=\"localhost\")\n# Attempting to modify will raise an exception\nconfig.port = 9000 # Raises Exception\n```\n\n### Private Fields\n\n```python\nclass PrivateFields(Classno):\n __features__ = Features.PRIVATE\n\n name: str\n secret: str # Can only be accessed with _secret prefix for rw, secret for ro\n\nobj = PrivateFields(name=\"public\")\nobj._secret = \"hidden\" # OK\nobj.secret # OK\nobj.secret = \"hidden\" # Raises Exception\n```\n\n### Nested Objects\n\n```python\nclass Address(Classno):\n street: str\n city: str\n\nclass Person(Classno):\n name: str\n address: Address\n\n# Create nested structure\nperson = Person(\n name=\"John\",\n address=Address(street=\"123 Main St\", city=\"Boston\")\n)\n```\n\n## Customization\n\n### Custom Comparison Keys\n\n```python\nclass CustomCompare(Classno):\n __hash_keys__ = {\"id\"} # Keys used for hashing\n __eq_keys__ = {\"id\", \"name\"} # Keys used for equality comparison\n __order_keys__ = {\"name\"} # Keys used for ordering\n\n id: int\n name: str\n description: str\n```\n\n## Best Practices\n\n1. Use type hints for all fields\n2. Enable appropriate features based on your needs\n3. Use `field()` for complex field configurations\n4. Consider using `Features.SLOTS` for better memory usage\n5. Enable validation when type safety is important\n\n## Error Handling\n\nThe library raises appropriate exceptions for:\n\n- Type validation errors\n- Missing required fields\n- Immutability violations\n- Invalid field access\n- Incorrect feature combinations\n\n# Authors\n\n- Dmitriy Kudryavtsev - author - [kuderr](https://github.com/kuderr)\n",
"bugtrack_url": null,
"license": "X11-distribute-modifications-variant",
"summary": null,
"version": "0.0.5",
"project_urls": {
"Bug Tracker": "https://github.com/kuderr/classno/issues",
"Homepage": "https://github.com/kuderr/classno",
"Repository": "https://github.com/kuderr/classno"
},
"split_keywords": [
"library"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "adc1d16cd0a39d02a07503acc00ecdce7ccca06dba082036cb271e75efb88147",
"md5": "83e14c1100f326db22f8391bd6ab1b15",
"sha256": "8c4a69f6e3aa0e40603dc35be69bc7ae5932bba3769c69e1a26efc2c041d0e65"
},
"downloads": -1,
"filename": "classno-0.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "83e14c1100f326db22f8391bd6ab1b15",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 9344,
"upload_time": "2024-11-11T19:40:30",
"upload_time_iso_8601": "2024-11-11T19:40:30.519267Z",
"url": "https://files.pythonhosted.org/packages/ad/c1/d16cd0a39d02a07503acc00ecdce7ccca06dba082036cb271e75efb88147/classno-0.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7154bc504d2b1e28d6f0b1786b76d655bf73a26af762330452cdac5e5414b6ba",
"md5": "70cc052d32cb2bdcfdd23e99392090e8",
"sha256": "b5fd673fad44351601ecc70481c9b4ac58b275918d79c6928b9dbfc29a53c26a"
},
"downloads": -1,
"filename": "classno-0.0.5.tar.gz",
"has_sig": false,
"md5_digest": "70cc052d32cb2bdcfdd23e99392090e8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 8525,
"upload_time": "2024-11-11T19:40:32",
"upload_time_iso_8601": "2024-11-11T19:40:32.650266Z",
"url": "https://files.pythonhosted.org/packages/71/54/bc504d2b1e28d6f0b1786b76d655bf73a26af762330452cdac5e5414b6ba/classno-0.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-11 19:40:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "kuderr",
"github_project": "classno",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "classno"
}