{"id":26579921,"url":"https://github.com/obriencj/python-mapbind","last_synced_at":"2025-03-23T06:29:31.008Z","repository":{"id":57439545,"uuid":"103565497","full_name":"obriencj/python-mapbind","owner":"obriencj","description":"mapbind, a simple utility for binding vars from a map","archived":false,"fork":false,"pushed_at":"2024-09-04T12:46:41.000Z","size":42,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T23:18:38.684Z","etag":null,"topics":["python","python2","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/obriencj.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-14T18:07:46.000Z","updated_at":"2024-09-04T12:46:42.000Z","dependencies_parsed_at":"2024-09-05T14:43:29.019Z","dependency_job_id":null,"html_url":"https://github.com/obriencj/python-mapbind","commit_stats":{"total_commits":44,"total_committers":1,"mean_commits":44.0,"dds":0.0,"last_synced_commit":"48b9f90e7b5ae1a171acc30c92a63b9279ee0147"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obriencj%2Fpython-mapbind","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obriencj%2Fpython-mapbind/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obriencj%2Fpython-mapbind/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/obriencj%2Fpython-mapbind/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/obriencj","download_url":"https://codeload.github.com/obriencj/python-mapbind/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245066295,"owners_count":20555398,"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":["python","python2","python3"],"created_at":"2025-03-23T06:29:30.556Z","updated_at":"2025-03-23T06:29:30.994Z","avatar_url":"https://github.com/obriencj.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Overview of python-mapbind\n\nFunctions that inspect their calling frame to figure out how you were\ngoing to bind their results, and returns values appropriate for the\nbindings.\n\n\n## Wut?\n\nA friend gave me the idea, and I wanted to see if it was possible.\nSpoilers: it totally was.\n\n\n## Usage\n\nConsider the following\n```python\n# if you find yourself having to write code like this\na = data[\"a\"]\nb = data[\"b\"]\nc = data[\"c\"]\n\n# or like this\na, b, c = map(data.get, (\"a\", \"b\", \"c\"))\n\n# then stop it. Use mapbind instead:\nfrom mapbind import mapbind\na, b, c = mapbind(data)\n```\n\nThe `mapbind` function looks at its calling frame's bytecode, figures\nout that the result is going to be used in an unpacking assignment,\nfinds the names of the variables the assignment would bind to, and\nreturns an iterator for the values of the given items in data with\nmatching keys. Supercool.\n\nAlso included are `objbind` which will find attributes on an object,\n`funbind` which will call a function for each binding name, and\n`takebind` which will return the right amount of values from a\nsequence to fulfill the count of bindings.\n\n```python\nfrom mapbind import objbind\n\n# objbind will notice that its assignment is to the variables named \"a\",\n# \"b\", and \"c\" and will subsequently fetch attributes with those names\n# from some_object and return a 3-tuple of their values.\n\na, b, c = objbind(some_object)\n\nassert a == some_object.a\nassert b == some_object.b\nassert c == some_object.c\n```\n\n```python\nfrom mapbind import funbind\n\naccu = []\ndef accumulator(name):\n    accu.append(name)\n    return \"|%s|\" % name\n\n# funbind will notice that its assignment is to the variables named \"a\",\n# \"b\", and \"c\" and will subsequently invoke accumulator with those strings\n# in turn, returning a 3-tuple of their values.\n\na, b, c = funbind(accumulator)\n\nassert accu == [\"a\", \"b\", \"c\"]\nassert a == \"|a|\"\nassert b == \"|b|\"\nassert c == \"|c|\"\n```\n\n```python\nfrom mapbind import takebind\n\n# Even though a range is huge, takebind will snag exactly as many items\n# from it as you're going to assign to.\n\nseq = range(0, 100)\na, b = takebind(seq)\n\nassert a == 0\nassert b == 1\n\n# And if you have too small of a range, you can tell takebind how you'd\n# like it to pad out any missing assignments\n\nseq = range(0, 2)\na, b, c, d, e, f, g = takebind(seq, 9001)\n\nassert a == 0\nassert b == 1\nassert [c, d, e, f, g] == [9001] * 5\n```\n\n\n## But...\n\n\"Can't I just do `locals().update(data)`?\"\n\nThat only works at the global/module scope. Once you're inside of a\nfunction, `locals()` is nothing but a snapshot of the underlying fast,\nfree, and cell variables in a call frame.\n\n\n## What Doesn't It Do\n\nMapbind's purpose is specific to unpacking for variable assignment.\nNot for item assignment, not for member assignment. All of the\nfollowing will result in an exception being raised.\n\n```python\n# You only want one member? Then just get it directly, no need for\n# mapbind. This is an error.\na = mapbind(my_data)\n\n# this kind of nesting makes zero sense in the context of mapbind, so\n# don't do it. If you do, it's an error.\na, (b, c) = mapbind(my_data)\n\n# copying from one map to another is something you can do already, no\n# need for mapbind, so this is an error.\nx[\"a\"], x[\"b\"], x[\"c\"] = mapbind(my_data)\n\n# we don't support this because the assignment operations are\n# interleaved with expressions to load their owning object.\nx.a, y.b, z.c = mapbind(my_data)\n```\n\nIt is trivial to write a function that will update a map or an\nobject's attributes from a map given a list of keys. There's no reason\nfor mapbind to support that use case. Mapbind is specifically for\nsituations where the list of keys can be determined entirely from the\nstorage variable names.\n\n\n## Supported Versions\n\nThis has been tested as working on the following versions and\nimplementations of Python\n\n* Python 2.6, 2.7\n* Python 3.3, 3.4, 3.5, 3.6, 3.7\n* PyPy, PyPy3\n\nIt is implemented entirely in Python (no native extensions). It has no\nruntime dependencies outside of itself and those provided as part of\nthe standard Python environment, though to run the unit tests you'll\nneed setuptools.\n\n\n## Contact\n\nauthor: Christopher O'Brien  \u003cobriencj@gmail.com\u003e\n\noriginal git repository: \u003chttps://github.com/obriencj/python-mapbind\u003e\n\n\n## License\n\nThis library is free software; you can redistribute it and/or modify\nit under the terms of the GNU Lesser General Public License as\npublished by the Free Software Foundation; either version 3 of the\nLicense, or (at your option) any later version.\n\nThis library is distributed in the hope that it will be useful, but\nWITHOUT ANY WARRANTY; without even the implied warranty of\nMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\nLesser General Public License for more details.\n\nYou should have received a copy of the GNU Lesser General Public\nLicense along with this library; if not, see\n\u003chttp://www.gnu.org/licenses/\u003e.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fobriencj%2Fpython-mapbind","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fobriencj%2Fpython-mapbind","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fobriencj%2Fpython-mapbind/lists"}