{ "info": { "author": "Michael Kent", "author_email": "mrmakent@gmail.com", "bugtrack_url": null, "classifiers": [ "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "What is it ?\r\n============\r\n\r\nThis is pyswitch, a simple yet powerful 'switch'-like dispatcher for\r\nPython. It's inspired by the C language switch statement, but with more\r\nflexibility; case values are not limited to integers like in C, but can\r\nbe any value usable with the '==' equality operator, or any string usable\r\non the left-hand side of the 'in' operator, or any regular expression.\r\nIterables of these types can also be use as case values.\r\n\r\nNew in Release 1.2:\r\n-------------------\r\nIt's no longer necessary to call the switch method of the Switch class to\r\ndispatch a value, as the __call__ method is now a synonym. So instead, you\r\ncan use::\r\n \r\n mySwitch = Switch()\r\n \r\n ... register your cases ...\r\n \r\n returnValue = mySwitch(value_to_switch_on)\r\n\r\nBut the switch method is still there for backwards compatibility.\r\n\r\nExample\r\n=======\r\n\r\nHere's a small example of its use::\r\n \r\n >>> from pyswitch import Switch, SwitchError\r\n \r\n # Instantiate a Switch object.\r\n >>> mySwitch = Switch()\r\n \r\n # Register some cases and case handlers, using the handy-dandy\r\n # decorators provided by the Switch object.\r\n \r\n # A default handler. A default handler is optional. If defined,\r\n # it will be called for all switch values not handled by any other\r\n # handler. If not defined, a SwitchError will be raised for an unhandled\r\n # switch value.\r\n \r\n # All handlers are passed the value being switched on, along with any\r\n # other optional positional and keyword parameters given to the switch.\r\n # Handlers can return a value, which is then returned by the call to\r\n # switch which caused the handler to be called.\r\n \r\n # Naturally, handlers can do anything you want. In this default case\r\n # example, we return a string of interesting info.\r\n >>> @mySwitch.default\r\n ... def gotDefault(value, *args, **kwargs):\r\n ... return \"Default handler: I got unregistered value %r, \"\\\r\n ... \"with args: %r and kwargs: %r\" % \\\r\n ... (value, args, kwargs)\r\n \r\n # A single numeric case value. The 'case' decorator is for exact matching\r\n # to the switch value. The value given to the case function, called\r\n # the case value, can be any type usable with the '==' equality operator.\r\n >>> @mySwitch.case(0)\r\n ... def gotZero(value, *args, **kwargs):\r\n ... return \"gotZero: I got a %d, with args: %r and kwargs: %r\" % \\\r\n ... (value, args, kwargs)\r\n\r\n # A range of numeric case values. An iterable of values can be given\r\n # as the case value.\r\n >>> @mySwitch.case(range(5, 10))\r\n ... def gotFiveThruNine(value, *args, **kwargs): \r\n ... return \"gotFiveThruNine: I got a %d, with args: %r and kwargs: %r\" % \\\r\n ... (value, args, kwargs)\r\n \r\n # A string case value, for an exact match.\r\n >>> @mySwitch.case('Guido')\r\n ... def gotGuido(value, *args, **kwargs):\r\n ... return \"gotGuido: I got '%s', with args: %r and kwargs: %r\" % \\\r\n ... (value, args, kwargs)\r\n \r\n # A string value for use with the 'in' operator.\r\n >>> @mySwitch.caseIn('lo')\r\n ... def gotLo(value, *args, **kwargs):\r\n ... return \"gotLo: I got '%s', with args: %r and kwargs: %r\" % \\\r\n ... (value, args, kwargs)\r\n \r\n # A regular expression pattern match in a string.\r\n # You can also pass in a pre-compiled regular expression.\r\n # For caseRegEx, the value passed to the case handler is actually\r\n # the Match Object resulting from the successful regular expression\r\n # pattern match.\r\n >>> @mySwitch.caseRegEx(r'\\b([Pp]y\\w*)\\b')\r\n ... def gotPyword(matchObj, *args, **kwargs):\r\n ... return \"gotPyword: I got a matchObject where group(1) is '%s', \"\\\r\n ... \"with args: %r and kwargs: %r\" % \\\r\n ... (matchObj.group(1), args, kwargs)\r\n \r\n # And lastly, you can pass a iterable of mixed-type values to case,\r\n # caseIn, and caseRegEx. Here, we pass in a list of heterogenous\r\n # values to be use for exact matches.\r\n >>> @mySwitch.case([ 99, 'yo', 200 ])\r\n ... def gotStuffInSeq(value, *args, **kwargs):\r\n ... return \"gotStuffInSeq: I got %r, with args: %r and kwargs: %r\" % \\\r\n ... (value, args, kwargs)\r\n \r\n \r\n # Now show what we can do.\r\n \r\n >>> mySwitch(0, testing=False)\r\n \"gotZero: I got a 0, with args: () and kwargs: {'testing': False}\"\r\n \r\n >>> mySwitch(6, flag='boring') \r\n \"gotFiveThruNine: I got a 6, with args: () and kwargs: {'flag': 'boring'}\"\r\n \r\n >>> mySwitch(10, 42)\r\n 'Default handler: I got unregistered value 10, with args: (42,) and kwargs: {}'\r\n \r\n >>> mySwitch('Guido', BDFL=True)\r\n \"gotGuido: I got 'Guido', with args: () and kwargs: {'BDFL': True}\"\r\n \r\n >>> mySwitch('Anyone seen Guido around?')\r\n \"Default handler: I got unregistered value 'Anyone seen Guido around?', with args: () and kwargs: {}\"\r\n \r\n >>> mySwitch('Yep, and he said \"hello\".', 99, yes='no')\r\n \"gotLo: I got 'lo', with args: (99,) and kwargs: {'yes': 'no'}\"\r\n \r\n >>> mySwitch('Bird is the Python word of the day.') \r\n \"gotPyword: I got a matchObject where group(1) is 'Python', with args: () and kwargs: {}\"\r\n \r\n >>> mySwitch('yo')\r\n \"gotStuffInSeq: I got 'yo', with args: () and kwargs: {}\"", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "UNKNOWN", "keywords": "pyswitch switch dispatch", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "pyswitch", "package_url": "https://pypi.org/project/pyswitch/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/pyswitch/", "project_urls": { "Download": "UNKNOWN", "Homepage": "UNKNOWN" }, "release_url": "https://pypi.org/project/pyswitch/1.2/", "requires_dist": null, "requires_python": null, "summary": "A simple yet powerful 'switch'-like dispatcher system for Python", "version": "1.2" }, "last_serial": 797793, "releases": { "1.2": [ { "comment_text": "", "digests": { "md5": "f32ba681b2378255fad6aeed08743828", "sha256": "ba1610198b07b3153b135ec446406d4051e2eac176a33d56f33860e3a3df48a0" }, "downloads": -1, "filename": "pyswitch-1.2.tar.gz", "has_sig": false, "md5_digest": "f32ba681b2378255fad6aeed08743828", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5925, "upload_time": "2011-01-07T04:50:17", "url": "https://files.pythonhosted.org/packages/b2/35/c931c654156a9e5f9dd270342748514cd8ee295d2b80fc18fdf54b7d4816/pyswitch-1.2.tar.gz" }, { "comment_text": "", "digests": { "md5": "94d6b1789921f66e81c745bfc3b5c4b5", "sha256": "32da1c4057924d1af20a45ed617c815b4b63d83aaed1e071cfeb800368db5fb8" }, "downloads": -1, "filename": "pyswitch-1.2.zip", "has_sig": false, "md5_digest": "94d6b1789921f66e81c745bfc3b5c4b5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8579, "upload_time": "2011-01-07T04:50:18", "url": "https://files.pythonhosted.org/packages/7d/fa/adbd9febd9ed145395ab8bf749488d564f3155f70feadb367ccba8d47036/pyswitch-1.2.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "f32ba681b2378255fad6aeed08743828", "sha256": "ba1610198b07b3153b135ec446406d4051e2eac176a33d56f33860e3a3df48a0" }, "downloads": -1, "filename": "pyswitch-1.2.tar.gz", "has_sig": false, "md5_digest": "f32ba681b2378255fad6aeed08743828", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5925, "upload_time": "2011-01-07T04:50:17", "url": "https://files.pythonhosted.org/packages/b2/35/c931c654156a9e5f9dd270342748514cd8ee295d2b80fc18fdf54b7d4816/pyswitch-1.2.tar.gz" }, { "comment_text": "", "digests": { "md5": "94d6b1789921f66e81c745bfc3b5c4b5", "sha256": "32da1c4057924d1af20a45ed617c815b4b63d83aaed1e071cfeb800368db5fb8" }, "downloads": -1, "filename": "pyswitch-1.2.zip", "has_sig": false, "md5_digest": "94d6b1789921f66e81c745bfc3b5c4b5", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8579, "upload_time": "2011-01-07T04:50:18", "url": "https://files.pythonhosted.org/packages/7d/fa/adbd9febd9ed145395ab8bf749488d564f3155f70feadb367ccba8d47036/pyswitch-1.2.zip" } ] }