{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Integer and Float Primitives" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Importing fixed length primitive types." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from itertools import chain\n", "\n", "from byteclasses.print import byteclass_info, byteclass_inspect\n", "from byteclasses.types.primitives.floats import Double, Float, Float16, Float32, Float64, Half\n", "from byteclasses.types.primitives.integers import (\n", " Int,\n", " Int8,\n", " Int16,\n", " Int32,\n", " Int64,\n", " Long,\n", " LongLong,\n", " Ptr16,\n", " Ptr32,\n", " Ptr64,\n", " Short,\n", " UInt,\n", " UInt8,\n", " UInt16,\n", " UInt32,\n", " UInt64,\n", " ULong,\n", " ULongLong,\n", " UnderflowError,\n", " UShort,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Fixed Size Primitive Instantiation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fixed_integer_types = [Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, Long, ULong]\n", "fixed_integer_type_aliases = [Ptr16, Ptr32, Ptr64, Short, UShort, Int, UInt, LongLong, ULongLong]\n", "fixed_float_types = [Float16, Float32, Float64]\n", "fixed_float_type_aliases = [Half, Float, Double]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i, type_cls in enumerate(\n", " chain(fixed_integer_types, fixed_integer_type_aliases, fixed_float_types, fixed_float_type_aliases)\n", "):\n", " var = type_cls(i)\n", " byteclass_info(var)\n", " byteclass_inspect(var)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numeric byteclasses can be used in math operations just like normal numbers" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var1 = UInt8(1)\n", "var2 = UInt8(2)\n", "print(var1, var2, var1 + var2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var1 = UInt8(1)\n", "var2 = 2\n", "print(var1, var2, var1 + var2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "var1 = 1\n", "var2 = UInt8(2)\n", "print(var1, var2, var1 + var2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each primitive class has built-in bounds checks and will raise an `OverflowError` or `UnderflowError` as appropriate." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "try:\n", " _ = Int8(128)\n", "except OverflowError as err:\n", " print(err)\n", "\n", "try:\n", " _ = UInt8(-1)\n", "except UnderflowError as err:\n", " print(err)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Override Overflow Protection" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "overflow_var = Int8(128, allow_overflow=True)\n", "byteclass_inspect(overflow_var)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "underflow_var = UInt8(-1, allow_overflow=True)\n", "byteclass_inspect(underflow_var)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Attaching to external data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_data = bytearray(b\"\\x00\\x01\\x02\\x03\")\n", "mv = memoryview(my_data)\n", "my_var1 = Int32()\n", "my_var2 = UInt32()\n", "print(my_data, my_var1, my_var2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Any byteclass instance can be attached to a memoryview of equal size" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_var1.attach(mv)\n", "my_var2.attach(mv)\n", "print(my_data, my_var1, my_var2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Changes to data are also represented in any attached byteclass instances" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mv[:] = b\"\\x04\\x05\\x06\\x07\"\n", "print(my_data, my_var1, my_var2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Changes to a primitive's value or data attribute are also applied to the attached data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_var1.value = my_var1.max\n", "print(my_data, my_var1, my_var2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Changes to a primitive's value or data attribute are also applied to the attached data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_var2.value = my_var2.max\n", "print(my_data, my_var1, my_var2)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10.5 ('.venv': poetry)", "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" }, "vscode": { "interpreter": { "hash": "557d07237cea0a23e1b53fe2d85e0da343a1f271fee6abb399e030b4f4b9db4a" } } }, "nbformat": 4, "nbformat_minor": 2 }