Skip to main content

A dataclass-like interface for ctypes data types.

Project description

ctypes-plus

A dataclass-like interface for ctypes data types.

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

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.1.0.tar.gz (4.8 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.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ctypes_plus-0.1.0.tar.gz
  • Upload date:
  • Size: 4.8 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.1.0.tar.gz
Algorithm Hash digest
SHA256 a401c4c0266adfe5ca3bae82fcb63ce3c857199a24002f89728164db46bd544a
MD5 765c24187bf09e38d799d59dd2936b31
BLAKE2b-256 de041479ceab9c5fe0f41eb61fe3c33d4f0130c9feb374d5af48e355eab05d13

See more details on using hashes here.

Provenance

The following attestation bundles were made for ctypes_plus-0.1.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.1.0-py3-none-any.whl.

File metadata

  • Download URL: ctypes_plus-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5acc4166a8b092097c39612ded45ef66a61591af36745fa23bf57b21f6a07211
MD5 82e42742af2bb1d865af77dd40f65ef0
BLAKE2b-256 e3641bbe393b8f5a00dc5be59e50a2134eadab79ecd630d75cca6842a42fac62

See more details on using hashes here.

Provenance

The following attestation bundles were made for ctypes_plus-0.1.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