{"id":26703065,"url":"https://github.com/shigekikarita/mir-pybuffer","last_synced_at":"2025-04-13T12:12:26.015Z","repository":{"id":62579091,"uuid":"127749423","full_name":"ShigekiKarita/mir-pybuffer","owner":"ShigekiKarita","description":"buffer protocol interface for Python and D","archived":false,"fork":false,"pushed_at":"2018-11-14T16:27:00.000Z","size":326,"stargazers_count":9,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-13T12:12:22.581Z","etag":null,"topics":["buffer-protocol","dlang","mir","numpy","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ShigekiKarita.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-02T11:51:12.000Z","updated_at":"2018-11-14T16:27:02.000Z","dependencies_parsed_at":"2022-11-03T21:00:44.984Z","dependency_job_id":null,"html_url":"https://github.com/ShigekiKarita/mir-pybuffer","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShigekiKarita%2Fmir-pybuffer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShigekiKarita%2Fmir-pybuffer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShigekiKarita%2Fmir-pybuffer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ShigekiKarita%2Fmir-pybuffer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ShigekiKarita","download_url":"https://codeload.github.com/ShigekiKarita/mir-pybuffer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248710448,"owners_count":21149191,"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":["buffer-protocol","dlang","mir","numpy","python3"],"created_at":"2025-03-27T03:18:57.069Z","updated_at":"2025-04-13T12:12:25.990Z","avatar_url":"https://github.com/ShigekiKarita.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# [WARNING]\n**this repository is outdated. moved to [mir-pybind](https://github.com/ShigekiKarita/mir-pybind)**\n\n\n# mir-pybuffer\n[![Build Status](https://travis-ci.org/ShigekiKarita/mir-pybuffer.svg?branch=master)](https://travis-ci.org/ShigekiKarita/mir-pybuffer)\n[![pypi](https://img.shields.io/pypi/v/pybuffer.svg)](https://pypi.org/project/pybuffer)\n[![dub](https://img.shields.io/dub/v/mir-pybuffer.svg)](https://code.dlang.org/packages/mir-pybuffer)\n\nmir-pybuffer provides simpler communication interface between C/D-language and python ndarrays (e.g., numpy, PIL) in [Buffer Protocol](https://docs.python.org/3/c-api/buffer.html#buffer-protocol).\n\n## installation\n\n``` console\n$ pip install pybuffer\n$ dub fetch mir-pybuffer # for D (mir) extention\n```\n\nfor C extention, you do not need anything but `Python.h`.\nyou can see a read/write example in [c-bp.c](c-bp.c) and run it by `$ make test-c`.\n\n## usage\n\n### python side\n\nAll you need to do is calling C/D dynamic library with `pybuffer.CDLL`.\n\n``` python\nimport ctypes\nimport numpy\nimport pybuffer\n\n# ndarrays\nx = numpy.array([[0, 1, 2], [3, 4, 5]]).astype(numpy.float64)\ny = numpy.array([0, 1, 2]).astype(numpy.float64)\n\n# load dynamic library written in d or c\nlib = pybuffer.CDLL(\"./libyour-dub-lib.so\")\nerr = lib.func1(x, y, ctypes.c_double(2.0))\nassert err == 0\n```\n\n### D side\n\ncurrently mir-pybuffer only supports ndslice functions that return void.\nTo create dynamic library, you also need to add `\"targetType\": \"dynamicLibrary\"` in [dub.json](test/dub.json).\n\n``` d\nimport mir.ndslice : Slice, Contiguous;\n// NOTE: DO NOT import pybuffer without \": pybuffer, MixinPyBufferWrappers\"\n// because it fails to generate wrappers.\nimport pybuffer : pybuffer, MixinPyBufferWrappers;\n\n@pybuffer\nvoid func1(Slice!(double*, 2) mat, Slice!(double*, 1) vec, double a) {\n  mat[0][] += vec;\n  vec[] *= a;\n}\n\nmixin MixinPyBufferWrappers;\n```\n\nrun this example by `$ make test-mir`.\n\n## detail\n\n`@pybuffer` and `mixin MixinPyBufferWrappers;` will generate wrapper functions as follows:\n\n``` d\npragma(mangle, __traits(identifier, pybuffer_func1))\nextern(C) auto pybuffer_func1( ref Py_buffer a0 , ref Py_buffer a1 , double a2 ) {\n  import mir.ndslice.connect.cpython;\n  import std.stdio : writeln;\n  Slice!(double*, 2) _a0;\n  {\n    auto err = fromPythonBuffer( _a0 , a0 );\n    if (err != PythonBufferErrorCode.success) { writeln(err); return err; }\n  }\n  Slice!(double*, 1) _a1;\n  {\n    auto err = fromPythonBuffer( _a1 , a1 );\n    if (err != PythonBufferErrorCode.success) { writeln(err); return err; }\n  }\n  func1( _a0 , _a1 , a2 );\n  return PythonBufferErrorCode.success;\n}\n```\n\nyou can see the actual generated codes by `lib.print_generated()` in python.\n`pybuffer.CDLL` calls `pybuffer_func1` instead of `func1` with PyBuffer arguments and error code handling.\nsee [mir.ndslice.connect.cpython.PythonBufferErrorCode](http://docs.algorithm.dlang.io/latest/mir_ndslice_connect_cpython.html#.PythonBufferErrorCode) for error code definitions.\n\n\n## known issues\n\n- `import pybuffer` without ` : pybuffer, MixinPyBufferWrappers` causes a empty generated string.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshigekikarita%2Fmir-pybuffer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshigekikarita%2Fmir-pybuffer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshigekikarita%2Fmir-pybuffer/lists"}