Skip to main content

A simple decorator to make custom sequence types

Project description

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 · Installation: Add the listclass decorator code directly to your project

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)

Enforcing more than one type

@listclass(type=int | None)
class List:
    pass

# Works perfectly
numbers_and_None = List(1, 2, 3, None)

# Raises TypeError: Incorrect type: expected: int | None got: str
numbers_and_None = List(1, "two", 3)

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

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 (+=)

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.0.0.tar.gz (4.3 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.0.0-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for listclasses-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8ac8a71cd3bd035b5e44ac4086508e98bec515c361c7b78500cb670078a9871e
MD5 657098332759de45f086a62d23c7dc08
BLAKE2b-256 97904b29d92d56bb3bb8c139bb1ede7229e7c179052a5b55cb2b816893307e6f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: listclasses-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.6 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.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 052556bf76738af429b6cfb8150a0596e006e39ab6d35fc82e2de9c51d0be99e
MD5 6abb7b23cdd942e7ee339e4f7b82f034
BLAKE2b-256 03b05361f54110d3208a798ad8d24c7147db9ecacda27b2c91d47c5ce1fb4383

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