{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Union Collection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from byteclasses.print import byteclass_info, byteclass_inspect\n", "from byteclasses.types.collections import ByteclassCollection, String, member, union\n", "from byteclasses.types.primitives.integers import Int8, Int16, UInt8, UInt16, UInt32, UInt64" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basic Union" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@union\n", "class BasicUnion:\n", " \"\"\"A basic union byteclass.\"\"\"\n", "\n", " var1: UInt8\n", " var2: UInt16" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "bu = BasicUnion()\n", "byteclass_info(bu)\n", "byteclass_inspect(bu)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Large Union Byteclass" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "@union(byte_order=b\">\")\n", "class LargeUnion:\n", " \"\"\"A large union class.\"\"\"\n", "\n", " uint64: UInt64\n", " uint32: UInt32\n", " uint16: UInt16\n", " int16: Int16\n", " uint8: UInt8\n", " int8: Int8\n", " string: String = member(\n", " factory=lambda byte_order: String(64, value=\"The quick brown fox jumped over the lazy dog.\")\n", " )" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lu: ByteclassCollection = LargeUnion()\n", "lu.uint64 = 2\n", "lu.uint32 = 4\n", "lu.uint16 = 8\n", "lu.int16 = 16\n", "lu.uint8 = 32\n", "lu.int8 = 64\n", "byteclass_info(lu)\n", "byteclass_inspect(lu)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lu.uint64 = 0xFFFFFFFF00" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "byteclass_inspect(lu)" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 2 }