Skip to main content

A simple decorator to create custom sequence types

Project description

from listclasses.build.lib.listclasses import is_listclassfrom listclasses.build.lib.listclasses import is_listclass

listclasses

A Python library providing a @listclass decorator that transforms standard classes into powerful, type-safe, and highly customizable custom sequence types

Features:

  • Type Safety: Enforce strict data types for sequence items
  • Length Constraints: Define fixed-size or dynamic sequences
  • Immutability: Easily create read-only (frozen) collections
  • Rich API: Seamlessly integrates with built-in functions (len(), repr(), iteration, slicing)
  • Copy Support: Native support for shallow and deep copying
  • Pretty Printing: Built-in methods for clean text formatting out of the box
  • Is Listclass Checking : Support for checking if an object or a class is a listclass
  • Installation: Add the listclass decorator code directly to your project
pip install listclasses
from listclasses import listclass

Basic Usage

from listclasses import listclass

@listclass
class Inventory:
    pass

# Create an instance with initial items
items = Inventory("Sword", "Shield", "Potion")

print(items)          # Output: Inventory(Sword, Shield, Potion)
print(items[0])       # Output: Sword
print(len(items))     # Output: 3

Advanced Configuration

The @listclass decorator accepts configuration parameters to restrict container behavior

Enforcing Strict Types

@listclass(type=int)
class IntegerList:
    pass

# Works perfectly
numbers = IntegerList(1, 2, 3)

# Raises TypeError: Incorrect type: expected: int got: str
numbers = IntegerList(1, "two", 3)

Multi-Type Union Support

@listclass(type=int | str)
class MixedList:
    pass

# Both integers and strings are valid
mixed = MixedList(1, "hello", 42, "world")

# Appending a float raises an error
# Raises TypeError: Incorrect type: expected: int | str got: float
mixed.append(3.14)

Setting Fixed Lengths

@listclass(len=2)
class Coordinates:
    pass

# Works perfectly
point = Coordinates(10, 20)

# Raises ValueError: Too many items: expected: 2 got: 3
point = Coordinates(10, 20, 30)

Creating Frozen (Immutable) Lists

@listclass(frozen=True)
class ReadOnlyData:
    pass

data = ReadOnlyData("A", "B")

# Raises TypeError: 'ReadOnlyData' object does not support item assignment
data[0] = "Z"

Built-in Formatting

listclasses comes with utility methods to print your items cleanly.

@listclass
class TodoList:
    pass

todos = TodoList("Buy milk", "Clean room", "Code Python")

# Dotted list format
todos.print_dotted('-')
# Output:
# -  Buy milk
# -  Clean room
# -  Code Python

# Numbered list format
todos.print_numbered()
# Output:
# 1. Buy milk
# 2. Clean room
# 3. Code Python

Checking for listclasses

Checking if a class is a listclass

@listclass
class MyList:
    pass

class Car:
    pass

print(is_listclass(MyList))
# Output:
# True

print(is_listclass(Car))
# Output:
# False

Checking if an object is a listclass

@listclass
class MyList:
    pass

class Car:
    pass

todo = MyList()
volvo = Car()

print(is_listclass(todo))
# Output:
# True

print(is_listclass(volvo))
# Output:
# False

API Reference

Decorator Arguments

Argument Type Default Description
len int 0 Expected length of the collection. 0 means dynamic size.
type type any Restricts elements to a single or multiple explicit python types.
frozen bool False If True, prevents item assignment and mutating operations.

Available Methods

Depending on configuration (frozen and len), instances support standard list operations:

  • Access: __getitem__, __iter__, __contains__, count(), index()
  • Mutation (Dynamic only): append(), pop(), clear(), insert(), __delitem__
  • Ordering (Mutable only): sort(), reverse()
  • Math: __add__, __iadd__, __mul__, __imul__

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

listclasses-1.1.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

listclasses-1.1.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file listclasses-1.1.1.tar.gz.

File metadata

  • Download URL: listclasses-1.1.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for listclasses-1.1.1.tar.gz
Algorithm Hash digest
SHA256 8e2370f1787f314e90d10bfd43ac402db71b850887873edd4a48f57a17e5ee0a
MD5 4c14c5c70254eb5433ce40aba764feb5
BLAKE2b-256 8e0432ae5516aca7d95eff733c014f74e0a68d29996b4580a2a284e8f2ccb90d

See more details on using hashes here.

File details

Details for the file listclasses-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: listclasses-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for listclasses-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2b6a223a5dbe1c8a19fca75987103d0645feefeacac3bf77e3fb104fde186c3
MD5 76ddfcfd27710e481940c13128325ce3
BLAKE2b-256 d7c8e132e0c79c821665f6c6cc1a9faecda17f893aa8a88edc8fd28703c4714b

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