stackvar
========
Dispatch function’s parameters through the callstack omitting arguments
on intermediary functions.
Installing
----------
https://pypi.org/project/stackvar/
::
pip install -U stackvar
Example
-------
.. code:: python
import stackvar
def test_stackvar():
# sending within a context
with stackvar.send(send_email, email='rsanchez@example.com'):
foo()
# Use default value
send_email()
# pass specific value
send_email('jerry@example.com')
def foo():
# intermediary function
bar()
def bar():
# intermediary function
send_email()
@stackvar.receive()
def send_email(email: stackvar.Variable = 'morty@example.com'):
print(f'Sending email to={email}')
if __name__ == '__main__':
test_stackvar()
Will output
::
Sending email to=rsanchez@example.com
Sending email to=morty@example.com
Sending email to=jerry@example.com
Cheatsheet
----------
.. code:: python
import stackvar
import uuid
def cheat_sheet_doc():
# Using namespace (recommended method)
my_namespace = stackvar.Namespace(uuid.uuid4())
@stackvar.receive(my_namespace)
def send_email(email: stackvar.Variable = 'morty@example.com'):
print(f'Sending email to={email}')
with stackvar.send(my_namespace, email='rsanchez@example.com'):
send_email()
# Automatic namespace (solved from function)
@stackvar.receive()
def send_email2(email: stackvar.Variable = 'morty@example.com'):
print(f'Sending email to={email}')
with stackvar.send(send_email2, email='rsanchez@example.com'):
send_email2()
# Without decorator
ns_uuid2 = stackvar.Namespace(uuid.uuid4())
def send_email_nodecorator():
email1 = ns_uuid2.email1
# setting default value for a variable
email2 = getattr(ns_uuid2, 'email2', 'jerry@example.com')
print(f'Sending email1 to={email1} and {email2}')
# another fancier way to set a default
email2 = stackvar.get(ns_uuid2, email2='summer@example.com')
print(f'Sending email1 to={email1} and {email2}')
with stackvar.send(ns_uuid2, email1='rsanchez@example.com'):
send_email_nodecorator()
# No default values
ns_uuid3 = stackvar.Namespace(uuid.uuid4())
@stackvar.receive(ns_uuid3)
def send_no_default(email1: stackvar.Variable, email2: stackvar.Variable):
print(f'Sending={email1} and {email2}')
with stackvar.send(ns_uuid3,
email1='rsanchez@example.com',
email2='summer@example.com'):
send_no_default()
# Using Factory for mutable values
ns_uuid4 = stackvar.Namespace(uuid.uuid4())
@stackvar.receive(ns_uuid4)
def send_factory(email_list: stackvar.Factory = list):
email_list.append('squanchy@example.com')
print(f'Sending to={email_list}')
with stackvar.send(ns_uuid4):
send_factory()
if __name__ == '__main__':
cheat_sheet_doc()
More docs
---------
Check examples at https://gitlab.com/joaduo/stackvar/-/tree/main/tests
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/joaduo/stackvar",
"name": "stackvar",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "stack, callstack, variable, parameter",
"author": "Joaquin Duo",
"author_email": "joaduo@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/c5/e4/c33225d9390bc65fd73ea425bac8720f9ba29efcc3bfb20632e5d87109b0/stackvar-3.1.0.tar.gz",
"platform": null,
"description": "stackvar\n========\n\nDispatch function\u2019s parameters through the callstack omitting arguments\non intermediary functions.\n\nInstalling\n----------\n\nhttps://pypi.org/project/stackvar/\n\n::\n\n pip install -U stackvar\n\nExample\n-------\n\n.. code:: python\n\n import stackvar\n\n def test_stackvar():\n # sending within a context\n with stackvar.send(send_email, email='rsanchez@example.com'):\n foo()\n # Use default value\n send_email()\n # pass specific value\n send_email('jerry@example.com')\n\n def foo():\n # intermediary function\n bar()\n\n def bar():\n # intermediary function\n send_email()\n\n @stackvar.receive()\n def send_email(email: stackvar.Variable = 'morty@example.com'):\n print(f'Sending email to={email}')\n\n if __name__ == '__main__':\n test_stackvar()\n\nWill output\n\n::\n\n Sending email to=rsanchez@example.com\n Sending email to=morty@example.com\n Sending email to=jerry@example.com\n\nCheatsheet\n----------\n\n.. code:: python\n\n import stackvar\n import uuid\n\n def cheat_sheet_doc():\n # Using namespace (recommended method)\n my_namespace = stackvar.Namespace(uuid.uuid4())\n @stackvar.receive(my_namespace)\n def send_email(email: stackvar.Variable = 'morty@example.com'):\n print(f'Sending email to={email}')\n with stackvar.send(my_namespace, email='rsanchez@example.com'):\n send_email()\n \n # Automatic namespace (solved from function)\n @stackvar.receive()\n def send_email2(email: stackvar.Variable = 'morty@example.com'):\n print(f'Sending email to={email}')\n with stackvar.send(send_email2, email='rsanchez@example.com'):\n send_email2()\n \n # Without decorator\n ns_uuid2 = stackvar.Namespace(uuid.uuid4())\n def send_email_nodecorator():\n email1 = ns_uuid2.email1\n # setting default value for a variable\n email2 = getattr(ns_uuid2, 'email2', 'jerry@example.com')\n print(f'Sending email1 to={email1} and {email2}')\n # another fancier way to set a default\n email2 = stackvar.get(ns_uuid2, email2='summer@example.com')\n print(f'Sending email1 to={email1} and {email2}')\n with stackvar.send(ns_uuid2, email1='rsanchez@example.com'):\n send_email_nodecorator()\n \n # No default values\n ns_uuid3 = stackvar.Namespace(uuid.uuid4())\n @stackvar.receive(ns_uuid3)\n def send_no_default(email1: stackvar.Variable, email2: stackvar.Variable):\n print(f'Sending={email1} and {email2}')\n with stackvar.send(ns_uuid3,\n email1='rsanchez@example.com',\n email2='summer@example.com'):\n send_no_default()\n\n # Using Factory for mutable values\n ns_uuid4 = stackvar.Namespace(uuid.uuid4())\n @stackvar.receive(ns_uuid4)\n def send_factory(email_list: stackvar.Factory = list):\n email_list.append('squanchy@example.com')\n print(f'Sending to={email_list}')\n with stackvar.send(ns_uuid4):\n send_factory()\n\n if __name__ == '__main__':\n cheat_sheet_doc()\n\nMore docs\n---------\n\nCheck examples at https://gitlab.com/joaduo/stackvar/-/tree/main/tests\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Dispatch function's parameters through the callstack omitting arguments on intermediary functions. (a.k.a.: stack variable)",
"version": "3.1.0",
"project_urls": {
"Homepage": "https://gitlab.com/joaduo/stackvar"
},
"split_keywords": [
"stack",
" callstack",
" variable",
" parameter"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c5e4c33225d9390bc65fd73ea425bac8720f9ba29efcc3bfb20632e5d87109b0",
"md5": "49dec9a0c019f3e0295b5beadfa69db1",
"sha256": "054a08951e1de0e891c2c99a88f40702a5cf3aed5012f47797b111097ada7cac"
},
"downloads": -1,
"filename": "stackvar-3.1.0.tar.gz",
"has_sig": false,
"md5_digest": "49dec9a0c019f3e0295b5beadfa69db1",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 5909,
"upload_time": "2024-08-04T03:13:13",
"upload_time_iso_8601": "2024-08-04T03:13:13.706975Z",
"url": "https://files.pythonhosted.org/packages/c5/e4/c33225d9390bc65fd73ea425bac8720f9ba29efcc3bfb20632e5d87109b0/stackvar-3.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-04 03:13:13",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "joaduo",
"gitlab_project": "stackvar",
"lcname": "stackvar"
}