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.
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d75ef790347b0a7fbe6a1cdd31a4eaa40d117a796f3e463ccd1f850ce14a8ddf
|
|
| MD5 |
b3cf6adb9be751b9d3b279a5114c8875
|
|
| BLAKE2b-256 |
359b6e7269b47523bbb8ab36479ad931ff9ef5397ccefcdb69867fbc1964ce8d
|
Provenance
The following attestation bundles were made for ctypes_plus-0.2.0.tar.gz:
Publisher:
publish.yaml on leyts/ctypes-plus
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ctypes_plus-0.2.0.tar.gz -
Subject digest:
d75ef790347b0a7fbe6a1cdd31a4eaa40d117a796f3e463ccd1f850ce14a8ddf - Sigstore transparency entry: 1672055091
- Sigstore integration time:
-
Permalink:
leyts/ctypes-plus@d7022472f4301af6e1f2be6bec7622997ee734fd -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/leyts
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@d7022472f4301af6e1f2be6bec7622997ee734fd -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d1dc49eefff1d19d2e59c9dcda777b1f642e46ff3041ac25206cf703b9cc7ea
|
|
| MD5 |
b3adec5e5ec495bae6961372a19f5c21
|
|
| BLAKE2b-256 |
11667a78c7dab14c4102612c47e25db5072ae5817364bd436ed2d9ec6a6a9346
|
Provenance
The following attestation bundles were made for ctypes_plus-0.2.0-py3-none-any.whl:
Publisher:
publish.yaml on leyts/ctypes-plus
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ctypes_plus-0.2.0-py3-none-any.whl -
Subject digest:
9d1dc49eefff1d19d2e59c9dcda777b1f642e46ff3041ac25206cf703b9cc7ea - Sigstore transparency entry: 1672055158
- Sigstore integration time:
-
Permalink:
leyts/ctypes-plus@d7022472f4301af6e1f2be6bec7622997ee734fd -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/leyts
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yaml@d7022472f4301af6e1f2be6bec7622997ee734fd -
Trigger Event:
push
-
Statement type: