Skip to main content

A dataclass-like interface for ctypes data types.

Project description

ctypes-plus

PyPI - Version PyPI - License

A dataclass-like interface for ctypes data types.

[!WARNING] This package is under development. The API is subject to breaking changes.

Installation

pip install ctypes-plus

Usage

Decorate an annotated class with @structure or @union. Each annotation becomes a _fields_ entry, and the result is a ctypes.Structure / ctypes.Union subclass usable anywhere ctypes is expected.

from ctypes_plus import structure, union
from ctypes_plus.types import CharP, Double, Int32, UInt64

@structure
class LogEvent:
    event_id: Int32
    timestamp: UInt64
    message: CharP

@union
class Number:
    as_int: Int32
    as_double: Double

event = LogEvent(event_id=2, timestamp=1234, message=b"foo")
print(event)  # LogEvent(event_id=2, timestamp=1234, message=b'foo')

Typed field aliases

ctypes_plus.types provides Annotated types that map a native Python type to its ctypes type, so construction and attribute access type-check cleanly.

Plain ctypes types (a: c_int), your own Annotated[..., ctype] aliases and type aliases of either are all accepted too.

Introspection

>>> from ctypes_plus import asdict, fields
>>> fields(LogEvent)
(Field(name='event_id', type=<class 'ctypes.c_int'>), ...)
>>> asdict(LogEvent(event_id=2, timestamp=1234, message=b"foo"))
{'event_id': 2, 'timestamp': 1234, 'message': b'foo'}

fields accepts a class or instance — including hand-written ctypes types — and returns a tuple of Field(name, type) records. asdict takes an instance and returns its fields as a dict.

Licence

MIT

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

ctypes_plus-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

ctypes_plus-0.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file ctypes_plus-0.2.0.tar.gz.

File metadata

  • Download URL: ctypes_plus-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ctypes_plus-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d75ef790347b0a7fbe6a1cdd31a4eaa40d117a796f3e463ccd1f850ce14a8ddf
MD5 b3cf6adb9be751b9d3b279a5114c8875
BLAKE2b-256 359b6e7269b47523bbb8ab36479ad931ff9ef5397ccefcdb69867fbc1964ce8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ctypes_plus-0.2.0.tar.gz:

Publisher: publish.yaml on leyts/ctypes-plus

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ctypes_plus-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ctypes_plus-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ctypes_plus-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d1dc49eefff1d19d2e59c9dcda777b1f642e46ff3041ac25206cf703b9cc7ea
MD5 b3adec5e5ec495bae6961372a19f5c21
BLAKE2b-256 11667a78c7dab14c4102612c47e25db5072ae5817364bd436ed2d9ec6a6a9346

See more details on using hashes here.

Provenance

The following attestation bundles were made for ctypes_plus-0.2.0-py3-none-any.whl:

Publisher: publish.yaml on leyts/ctypes-plus

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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