{"id":16654662,"url":"https://github.com/heman/binmap","last_synced_at":"2025-07-26T23:33:24.370Z","repository":{"id":54202674,"uuid":"238802475","full_name":"HeMan/binmap","owner":"HeMan","description":"Python base class for creating binary parsing and packing classes","archived":false,"fork":false,"pushed_at":"2024-11-11T17:22:42.000Z","size":126,"stargazers_count":17,"open_issues_count":9,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-01T09:53:46.548Z","etag":null,"topics":["binary-data","dataclass","python"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HeMan.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-06T23:02:16.000Z","updated_at":"2024-11-14T16:57:16.000Z","dependencies_parsed_at":"2023-12-05T15:26:49.443Z","dependency_job_id":"a33309b5-9f78-4f44-9800-0f7730c0e3f6","html_url":"https://github.com/HeMan/binmap","commit_stats":{"total_commits":98,"total_committers":3,"mean_commits":"32.666666666666664","dds":0.1428571428571429,"last_synced_commit":"8fd20d4844165a9b524bf2a15290ce3d19ea9adb"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HeMan%2Fbinmap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HeMan%2Fbinmap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HeMan%2Fbinmap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HeMan%2Fbinmap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HeMan","download_url":"https://codeload.github.com/HeMan/binmap/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229089142,"owners_count":18018391,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["binary-data","dataclass","python"],"created_at":"2024-10-12T09:50:36.081Z","updated_at":"2024-12-10T16:15:06.613Z","avatar_url":"https://github.com/HeMan.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Dataclass for go to and from binary data\n\n\nIt follows dataclass pattern with typehinting as the binary format.\nTemperature with one unsigned byte:\n\n.. code-block:: python\n\n   \u003e\u003e\u003e class Temperature(BinmapDataclass):\n   ...     temp: unsignedchar = 0\n   ...\n   \u003e\u003e\u003e t = Temperature()\n   \u003e\u003e\u003e t.temp = 22\n   \u003e\u003e\u003e print(bytes(t))\n   b'\\x16'\n\n   \u003e\u003e\u003e t2 = Temperature(b'\\x20')\n   \u003e\u003e\u003e print(t2.temp)\n   32\n\nTemperature and humidity consisting of one signed byte for temperature and\none unsiged byte for humidity:\n\n.. code-block:: python\n\n   \u003e\u003e\u003e class TempHum(BinmapDataclass):\n   ...     temp: signedchar = 0\n   ...     hum: unsignedchar = 0\n   ...\n   \u003e\u003e\u003e th = TempHum()\n   \u003e\u003e\u003e th.temp = -10\n   \u003e\u003e\u003e th.humidity = 60\n   \u003e\u003e\u003e print(bytes(th))\n   b'\\xfc\u003c'\n\n   \u003e\u003e\u003e th2 = TempHum(b'\\xea\\x41')\n   \u003e\u003e\u003e print(th2.temp)\n   -22\n   \u003e\u003e\u003e print(th2.hum)\n   65\n\n\n\nDatatypes\n---------\nBinmap supports all datatypes that standard library `struct \u003chttps://docs.python.org/3/library/struct.html\u003e`_ has.\nThe types works as typehints in the dataclass. When giving an attribute a\ntypehinted datatype it will be added to the binary output from the class.\n\n\nPadding\n-------\nPadding is a field type and datatype that is `length` bytes long, and will not show up as\nattributes in the dataclass. Trying to assign it a value will be silently\nignored and reading from will raise the `AttributeException` exception.\n\n.. code-block:: python\n\n   \u003e\u003e\u003e class PaddedData(BinmapDataclass):\n   ...     temp: signedchar = 0\n   ...     pad1: pad = padding(length = 5)\n   ...\n   \u003e\u003e\u003e pd = PaddedData()\n   \u003e\u003e\u003e pd.temp = 14\n   \u003e\u003e\u003e print(bytes(pd))\n   b'\\x0e\\x00\\x00\\x00\\x00\\x00'\n\nConstant\n--------\nConstant is fieldtype that always is the given value. Constant could be of any\ndatatype.\n\n.. code-block:: python\n\n   \u003e\u003e\u003e class Constant(BinmapDataclass):\n   ...     signature: unsignedshort = constant(0x1313)\n   ...     temp: unsignedchar = 0\n   ...\n   \u003e\u003e\u003e c = Constant()\n   \u003e\u003e\u003e c.temp = 18\n   \u003e\u003e\u003e print(bytes(c))\n   b'\\x13\\x13\\x12'\n\n   \u003e\u003e\u003e print(c.signature)\n   4883\n   \u003e\u003e\u003e print(c.temp)\n   18\n\n   \u003e\u003e\u003e c.signature = 10\n   AttributeError: signature is a constant\n\nEnums\n-----\nEnumfield maps agaings IntEnum or IntFlag so that you could set the value\neither as the enum or as the numeric value.\n\n.. code-block:: python\n\n   \u003e\u003e\u003e class WindEnum(IntEnum):\n   ...     North = 0\n   ...     East = 1\n   ...     South = 2\n   ...     West = 3\n   ...\n   \u003e\u003e\u003e class Wind(BinmapDataclass):\n   ...     speed: unsignedchar = 0\n   ...     direction: unsignedchar = enumfield(WindEnum, default=WindEnum.East)\n   ...\n   \u003e\u003e\u003e w = Wind()\n   \u003e\u003e\u003e print(w)\n   Wind(speed=0, direction=\u003cWindEnum.East: 1\u003e)\n   \u003e\u003e\u003e w.direction = WindEnum.West\n   \u003e\u003e\u003e print(w.direction)\n   \u003cWindEnum.West: 3\u003e\n   \u003e\u003e\u003e w.direction = 2\n   \u003e\u003e\u003e print(w.direction)\n   \u003cWindEnum.South: 2\u003e\n\n\nAutolength\n----------\nAutolenght field types counts number of bytes in the output, including the\nautolength field it self. You can't set an autolenght field. Autolength can be\noffseted, for example to ignore it's own length.\n\n.. code-block:: python\n\n   \u003e\u003e\u003e class MyBinStruct(BinmapDataclass):\n   ...     length: unsignedchar = autolength()\n   ...     temp: signedchar = 0\n   ...\n   \u003e\u003e\u003e mb = MyBinStruct()\n   \u003e\u003e\u003e print(mb)\n   MyBinStruct(length=2, temp=0)\n   \u003e\u003e\u003e mb.length = 10\n   AttributeError: length is a constant\n\nCalculated fields\n-----------------\nCalculated fields calls a function when data is converted to binary value. The\nfunction must be declared when the field is added.\n\n.. code-block:: python\n\n   \u003e\u003e\u003e class WithChecksum(BinmapDataclass):\n   ...     temp: signedchar = 0\n   ...     hum: unsignedchar = 0\n   ...     def chk(self) -\u003e unsignedchar:\n   ...         return (self.temp + self.hum) \u0026 0xFF\n   ...     checksum: unsignedchar = calculatedfield(chk)\n   ...\n   \u003e\u003e\u003e wc = WithChecksum()\n   \u003e\u003e\u003e wc.temp = -20\n   \u003e\u003e\u003e wc.hum = 10\n   \u003e\u003e\u003e print(wc)\n   WithChecksum(temp=-20, hum=10, checksum=246)\n   \u003e\u003e\u003e print(bytes(wc))\n   b'\\xec\\n\\xf6'\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheman%2Fbinmap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fheman%2Fbinmap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fheman%2Fbinmap/lists"}