{ "info": { "author": "Anshul Kharbanda", "author_email": "akanshul97@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "# Good Handlers\n\nMethod and function handlers that implement common behaviors without writing the entire function\n\n## Handlers\n\nHandlers are objects that act as methods in a class. They are good for configurable, predefined functions which will otherwise be redundant to implement. Handler functionality is implemented in standard python's `__call__` method, which otherwise treats the handler like a function.\n\n```python\nfrom good_handlers import InstanceHandler\n\nclass MyHandler(InstanceHandler):\n def __init__(self, scale):\n self._scale = scale\n\n def __call__(self, instance, arg1, arg2):\n instance.result = scale*(arg1+arg2)\n```\n\nThe class `InstanceHandler` binds to an instance. This requires the extra `instance` parameter, which will become the bound instance of the handler\n\n```python\nfrom good_handlers import InstanceHandler\n\nclass MyHandler(InstanceHandler):\n def __init__(self, scale, save='last_result'):\n self._scale = scale\n self._save = save\n\n def __call__(self, instance, arg1, arg2):\n setattr(instance, self._save, scale*(arg1+arg2))\n\nclass MyClass:\n def __init__(self):\n self.last_result = None\n\n handler = MyHandler(3)\n```\n\nThe class `ClassHandler` binds to a class and passes the bound class to the extra `klass` parameter.\n\n```python\nfrom good_handlers import ClassHandler\n\nclass DefaultAgePersonMaker(ClassHandler):\n def __init__(self, defage):\n self._defage = defage\n\n def __call__(self, klass, name):\n return klass(name=name, age=self._defage)\n\nclass Person:\n make_twenty_year_old = DefaultAgePersonMaker(20)\n\n def __init__(self, name, age):\n self._name = name\n self._age = age\n\njim = Person.make_twenty_year_old('Jim Shim') # Makes Person('Jim Shim', 20)\n```\n\n### Init Handlers\n\nA special case of redundant programming is the `__init__` method. Often times, the `__init__` is only used to set a series of member variables. Thus, they will often contain a lot of boilerplate code. The Good Library offers a few `__init__` handlers, which simplify creating `__init__` methods. `NamedInitHandler` sets member variables according to a tuple of names provided in the `names` parameter.\n\n```python\nfrom good_handlers.init import NameInitHandler\n\nclass Person:\n __init__ = NameInitHandler(\n names=('name', 'age', 'apparel', 'thoughts')\n )\n```\n\nDefault values for these can be provided as a dict in the `defaults` parameter.\n\n```python\nfrom good_handlers.init import NameInitHandler\n\nclass Person:\n __init__ = NameInitHandler(\n names=('name', 'age', 'apparel', 'thoughts'),\n defaults={\n 'apparel': 'Pajamas',\n 'thoughts': 'Nothing at the moment...'\n }\n )\n```\n\n`UnderscoreInitHandler` is a type of `NamedInitHandler` that appends an underscore `_` to each name before setting it in the instance. `DunderInitHandler` adds a dunder, or a double-underscore `__` before each name\n\n### String Handlers\n\nAnother case of redundant programming is string representations. The Good Library provides `ValueStringHandler`, which prints the object name and the values of the given keys to show, and `KeyValueStringHandler`, which is like `ValueStringHandler`, but displays key=value pairs instead of just values\n\n```python\nfrom good_handlers.string import ValueStringHandler, KeyValueStringHandler\n\nclass Person1:\n \"\"\"\n Docstring for Person\n \"\"\"\n def __init__(self, name, age):\n \"\"\"\n Initializes instance\n \"\"\"\n self.name = name\n self.age = age\n\n __repr__ = ValueStringHandler(('name', 'age'))\n\nclass Person2:\n \"\"\"\n Docstring for Person2\n \"\"\"\n def __init__(self, name, age):\n \"\"\"\n Initializes instance\n \"\"\"\n self.name = name\n self.age = age\n\n __repr__ = KeyValueStringHandler(('name', 'age'))\n\njohn1 = Person1('John Numberone', 21)\njohn2 = Person2('John Numbertwo', 28)\n\nprint(john1) # prints 'Person1(\\'John Numberone\\', 21)'\nprint(john2) # prints 'Person2(name=\\'John Numbertwo\\', age=28)'\n```", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://www.github.org/andydevs/good-handlers", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "good-handlers", "package_url": "https://pypi.org/project/good-handlers/", "platform": "", "project_url": "https://pypi.org/project/good-handlers/", "project_urls": { "Homepage": "https://www.github.org/andydevs/good-handlers" }, "release_url": "https://pypi.org/project/good-handlers/1.0.3/", "requires_dist": null, "requires_python": "", "summary": "Method and function handlers that implement common behavior", "version": "1.0.3" }, "last_serial": 4005621, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "19c4832d0228e476b8eff0c6a09b122f", "sha256": "070d6d6e4107e889a2474b94706b8d9fa4981f93930fe1559df189b80712a527" }, "downloads": -1, "filename": "good-handlers-1.0.0.tar.gz", "has_sig": false, "md5_digest": "19c4832d0228e476b8eff0c6a09b122f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2357, "upload_time": "2018-06-25T23:24:23", "url": "https://files.pythonhosted.org/packages/03/58/fe586684ac8696540535611bd4386b66f00bf4daf6227fd69a095876274e/good-handlers-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "dbf8006056796956a4c2e6fd9e19373e", "sha256": "a92ac96f8f493efe42fed2c96b7346be876e8d6f07eb9e6c1de95708dcd73288" }, "downloads": -1, "filename": "good-handlers-1.0.1.tar.gz", "has_sig": false, "md5_digest": "dbf8006056796956a4c2e6fd9e19373e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3600, "upload_time": "2018-06-25T23:37:22", "url": "https://files.pythonhosted.org/packages/a3/ec/4a8eccd3263c7643bc40d6101473af1d342a1b4433d6568a522a1dd470c1/good-handlers-1.0.1.tar.gz" } ], "1.0.2": [ { "comment_text": "", "digests": { "md5": "9dcca4ced65dec29baa896f9b8ae147d", "sha256": "aa7882ba4bde07b5036612507753309b0b378432433ea49748f08a939d0ce981" }, "downloads": -1, "filename": "good-handlers-1.0.2.tar.gz", "has_sig": false, "md5_digest": "9dcca4ced65dec29baa896f9b8ae147d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 2407, "upload_time": "2018-06-27T01:06:46", "url": "https://files.pythonhosted.org/packages/3c/32/d8eae0680a3eb1ff9f2fa7c1a2ef717373f05e17f75e59041ea18e07b722/good-handlers-1.0.2.tar.gz" } ], "1.0.3": [ { "comment_text": "", "digests": { "md5": "21582089281ec589eeb12c78c223612f", "sha256": "6bdb8d3f9d2218449b68250850c673133a3261b5c134b75c7b17a92d969a11ec" }, "downloads": -1, "filename": "good-handlers-1.0.3.tar.gz", "has_sig": false, "md5_digest": "21582089281ec589eeb12c78c223612f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4002, "upload_time": "2018-06-27T01:13:22", "url": "https://files.pythonhosted.org/packages/99/08/36695db639b23ca980ad44b59edad268c767e0d0daf539c228135f3bf727/good-handlers-1.0.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "21582089281ec589eeb12c78c223612f", "sha256": "6bdb8d3f9d2218449b68250850c673133a3261b5c134b75c7b17a92d969a11ec" }, "downloads": -1, "filename": "good-handlers-1.0.3.tar.gz", "has_sig": false, "md5_digest": "21582089281ec589eeb12c78c223612f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4002, "upload_time": "2018-06-27T01:13:22", "url": "https://files.pythonhosted.org/packages/99/08/36695db639b23ca980ad44b59edad268c767e0d0daf539c228135f3bf727/good-handlers-1.0.3.tar.gz" } ] }