Python wrapper for advanced C++ low-level operations
Project description
PyCppLink
A Python library that provides direct access to advanced C++ functionality including memory pools, data processing, and error handling.
Features
- Memory Pool: Efficient stack-based memory management with zero-copy operations
- Advanced Processor: High-performance data processing with C++ backend
- Pointer Operations: Comprehensive pointer manipulation for low-level memory access
- Error Handling: Comprehensive error reporting with detailed messages
- Cross-platform: Works on Windows, Linux, and macOS
Installation
From Source
cd pycpplink
pip install .
Development Mode
cd pycpplink
pip install -e .
Usage
Basic Example
from pycpplink import MemoryPool, AdvancedProcessor, PyCPPError
try:
# Create a memory pool
pool = MemoryPool()
print(f"Memory pool created with {pool.get_remaining()} bytes remaining")
# Create a processor
processor = AdvancedProcessor(pool)
# Initialize data
processor.init_data(10)
print("Data initialized with 10 elements")
# Process data
processor.process_data(0, 100)
processor.process_data(5, 500)
# Get data
value = processor.get_data(0)
print(f"Value at index 0: {value}")
value = processor.get_data(5)
print(f"Value at index 5: {value}")
# Reset memory pool
pool.reset()
print("Memory pool reset")
except PyCPPError as e:
print(f"Error occurred: {e}")
Advanced Example
from pycpplink import MemoryPool, AdvancedProcessor, PyCPPError
def process_large_dataset():
pool = MemoryPool()
processor = AdvancedProcessor(pool)
try:
# Initialize with maximum allowed size
processor.init_data(100)
# Process all elements
for i in range(100):
processor.process_data(i, i * 10)
# Verify data
for i in range(100):
value = processor.get_data(i)
assert value == i * 10, f"Expected {i * 10}, got {value}"
print("All data processed successfully!")
### Pointer Operations Example
```python
from pycpplink import MemoryPool, Pointer, PyCPPError
def pointer_operations():
pool = MemoryPool()
try:
# Allocate memory
ptr = pool.allocate(64)
print(f"Allocated {ptr.size} bytes")
# Write various data types
ptr.write_byte(0, 0x01)
ptr.write_short(1, 0x0203)
ptr.write_int(3, 0x04050607)
ptr.write_long(7, 0x08090A0B0C0D0E0F)
ptr.write_float(15, 3.14159)
ptr.write_double(19, 2.71828)
# Read back
print(f"Byte: {hex(ptr.read_byte(0))}")
print(f"Short: {hex(ptr.read_short(1))}")
print(f"Int: {hex(ptr.read_int(3))}")
print(f"Long: {hex(ptr.read_long(7))}")
print(f"Float: {ptr.read_float(15)}")
print(f"Double: {ptr.read_double(19)}")
# Pointer offset
offset_ptr = ptr.offset(10)
print(f"Read from offset: {hex(offset_ptr.read_byte(0))}")
# Memory copy
ptr2 = pool.allocate(64)
ptr.copy(ptr2, 32)
print(f"Copied value: {hex(ptr2.read_int(3))}")
# Memory fill
ptr.fill(0xFF, 20)
print(f"Filled value: {hex(ptr.read_byte(5))}")
# Memory zero
ptr.zero()
print(f"Zeroed value: {hex(ptr.read_byte(0))}")
except PyCPPError as e:
print(f"Error occurred: {e}")
if __name__ == "__main__":
pointer_operations()
API Reference
MemoryPool
Methods
allocate(size: int) -> Pointer: Allocate memory from the poolreset(): Reset the memory poolget_remaining() -> int: Get remaining bytes in the pool
AdvancedProcessor
Methods
init_data(length: int): Initialize data array (1-100 elements)process_data(index: int, value: int): Set value at indexget_data(index: int) -> int: Get value at indexget_last_error() -> Tuple[int, str]: Get last error code and message
Pointer
Methods
Write Operations:
write_byte(offset: int, value: int): Write a byte (8-bit) at the specified offsetwrite_short(offset: int, value: int): Write a short (16-bit) at the specified offsetwrite_int(offset: int, value: int): Write an int (32-bit) at the specified offsetwrite_long(offset: int, value: int): Write a long (64-bit) at the specified offsetwrite_float(offset: int, value: float): Write a float (32-bit) at the specified offsetwrite_double(offset: int, value: float): Write a double (64-bit) at the specified offset
Read Operations:
read_byte(offset: int) -> int: Read a byte (8-bit) from the specified offsetread_short(offset: int) -> int: Read a short (16-bit) from the specified offsetread_int(offset: int) -> int: Read an int (32-bit) from the specified offsetread_long(offset: int) -> int: Read a long (64-bit) from the specified offsetread_float(offset: int) -> float: Read a float (32-bit) from the specified offsetread_double(offset: int) -> float: Read a double (64-bit) from the specified offset
Memory Operations:
offset(offset: int) -> Pointer: Create a new pointer with the specified byte offsetcopy(dest: Pointer, size: int): Copy memory to another pointercompare(other: Pointer, size: int) -> int: Compare memory with another pointer (returns -1, 0, or 1)fill(value: int, size: int): Fill memory with a byte valuezero(): Zero all memory in the pointer
Properties:
address -> int: Get the memory address of the pointersize -> int: Get the size of the allocated memory
PyCPPError
Exception raised when C++ operations fail.
Attributes
code: Error code (int)message: Error message (str)
Error Codes
0: Success1: Memory allocation failed2: Invalid parameter3: Out of range4: Internal error
Building from Source
The library automatically compiles the C++ code during installation. No pre-compiled binaries are required.
Requirements
- Python 3.7+
- C++ compiler (MSVC on Windows, GCC on Linux, Clang on macOS)
Build Commands
# Build wheel
python setup.py bdist_wheel
# Install from wheel
pip install dist/pycpp-0.1.0-*.whl
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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 pycpplink-0.1.0.tar.gz.
File metadata
- Download URL: pycpplink-0.1.0.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
efb9d84cea386d6ff448b8d94d5383b261f357ac3abbe226d36e8e75a2bb15f9
|
|
| MD5 |
78ee26b51e39bbccd504e8cce3479a0e
|
|
| BLAKE2b-256 |
1177bdb80078c7a5d08948636a225523251e48d6e03b13bdff69c65efa42e3a9
|
File details
Details for the file pycpplink-0.1.0-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: pycpplink-0.1.0-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 17.8 kB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5ee88117c7c6bb605726b62487b9da47fe8bd3ac7e039df53d17196e16953e6
|
|
| MD5 |
66e4003b8d40615361459efe18a7f191
|
|
| BLAKE2b-256 |
03bab728208910f119abf56c93c94ff1f4d2587b35ef86c762da80d367704279
|