Skip to main content

No project description provided

Project description

Classno

pypi downloads downloads versions 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

pip install classno

Basic Usage

Simple Data Class

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:

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:

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

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

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

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

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

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

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

classno-0.0.4.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

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

classno-0.0.4-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file classno-0.0.4.tar.gz.

File metadata

  • Download URL: classno-0.0.4.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/22.6.0

File hashes

Hashes for classno-0.0.4.tar.gz
Algorithm Hash digest
SHA256 602e4c861a6cadbf2da569ad1331df67ad5bde5287f8d56c99d7a521f92e222a
MD5 e766aae0a309f2149ddf45d2cf98c429
BLAKE2b-256 92e8c2084b54df61986e71712367296141fcb22982e210881e4aa68dfc5b106f

See more details on using hashes here.

File details

Details for the file classno-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: classno-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/22.6.0

File hashes

Hashes for classno-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4fda257716ce1167c756fedd6b533bdb506e6d036469f3d75a30423ae6f12a1d
MD5 3069ffa84a3f74a56c46c15bb038918b
BLAKE2b-256 bde0f2fbe65633e057d80f7ebc2eebc34987627bd1cb25750a7761b6ce99435c

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