mda-xdrlib
mda-xdrlib is a stand-alone Python module that provides the functionality of the `xdrlib` module, as it existed in CPython 3.10.8. It enables conversion between Python data types and the XDR (External Data Representation) format, which is useful for platform-independent binary data exchange. Currently at version 0.2.0, its releases are infrequent and primarily driven by the need to maintain XDR compatibility for projects like MDAnalysis after the standard library's `xdrlib` module was deprecated.
Warnings
- deprecated The `xdrlib` module in the Python standard library is deprecated in Python 3.11 and will be removed entirely in Python 3.13. `mda-xdrlib` serves as a drop-in replacement, and users on Python 3.11+ must use this standalone package or similar solutions to retain `xdrlib` functionality.
- breaking For projects previously using the standard `xdrlib` module (especially those developed with Python versions prior to 3.11), migrating to `mda-xdrlib` requires updating all import statements to `from mda_xdrlib import ...` instead of `from xdrlib import ...`.
- gotcha The XDR format and its Python API (`xdrlib`/`mda-xdrlib`) can be unintuitive for developers accustomed to more modern serialization formats. Incorrect usage of packing/unpacking methods (e.g., mismatching types or order) or improper buffer management (e.g., `get_position`, `set_position`) can lead to `mda_xdrlib.Error` or `mda_xdrlib.ConversionError`.
- gotcha When `mda-xdrlib` is used by other libraries (e.g., MDAnalysis for reading GROMACS XDR files), issues can arise from external factors such as corrupted auxiliary files (e.g., offset files) or concurrent access in multiprocessing environments. These can manifest as `ValueError` (e.g., for pickled data) or other file I/O errors.
Install
-
pip install mda-xdrlib
Imports
- Packer
from xdrlib import Packer
from mda_xdrlib import Packer
- Unpacker
from xdrlib import Unpacker
from mda_xdrlib import Unpacker
Quickstart
import mda_xdrlib
# 1. Pack data
packer = mda_xdrlib.Packer()
packer.pack_int(123)
packer.pack_string(b"hello xdr")
packer.pack_double(3.14159)
packed_data = packer.get_buffer()
print(f"Packed data (hex): {packed_data.hex()}")
# 2. Unpack data
unpacker = mda_xdrlib.Unpacker(packed_data)
unpacked_int = unpacker.unpack_int()
unpacked_string = unpacker.unpack_string()
unpacked_double = unpacker.unpack_double()
unpacker.done() # Verify all data has been unpacked
print(f"Unpacked int: {unpacked_int}")
print(f"Unpacked string: {unpacked_string.decode('utf-8')}")
print(f"Unpacked double: {unpacked_double}")