{ "info": { "author": "yupeng", "author_email": "yupeng0921@gmail.com", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python :: 2.6", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3" ], "description": "# do mixin for python\n This project wants to implement a mixin library like ruby's module.\n\n## What mixin is\nIn object-oriented programming languages, a mixin is a class which contains a combination of methods from other classes. How such a combination is done depends on the language, but it is not by inheritance. If a combination contains all methods of combined classes it is equivalent to multiple inheritance.\nFor more information, please refer to this wiki:\nhttp://en.wikipedia.org/wiki/Mixin\n\n## install pymixin\n\ninstall it from pip:\n\n pip install mixin\n\nIn fact, the pymix package only has one file, it is mixin.py, so you can also copy it directly to your project directory.\n\n## how to use pymixin\n\n### a simple example\n\nImport the Mixin class and mixin decorator from the mixin module:\n\n >>> from mixin import Mixin, mixin\n\nThe user's mixin class should always inherit from the Mixin class:\n\n >>> class MixinA(Mixin):\n ... def func_mixin_a(self):\n ... return 'do_func_mixin_a'\n ...\n\nand then use the mixin decorator to add the mixin to a class:\n\n >>> @mixin(MixinA)\n ... class A(object): pass\n ...\n >>> a = A()\n >>> a.func_mixin_a()\n 'do_func_mixin_a'\n\n### add a mixin to another mixin\n\nThe mixin should not be inherited by another class:\n\n >>> class MixinA(Mixin): pass\n ...\n >>> class A(MixinA): pass\n ...\n Traceback (most recent call last):\n File \"\", line 1, in \n File \"mixin.py\", line 58, in __new__\n raise InheritMixinError(clsname)\n __main__.InheritMixinError: A\n\nBut you can add a mixin to another mixin:\n\n >>> class MixinA(Mixin):\n ... def func_mixin_a(self):\n ... return 'do_func_mixin_a'\n ...\n >>> @mixin(MixinA)\n ... class MixinB(Mixin):\n ... def func_mixin_b(self):\n ... return 'do_func_mixin_b'\n ...\n\n\nThen add the MixinB to a normal class\n\n >>> @mixin(MixinB)\n ... class M(object): pass\n ...\n >>> m = M()\n >>> m.func_mixin_a()\n 'do_func_mixin_a'\n >>> m.func_mixin_b()\n 'do_func_mixin_b'\n\n### a mixin class can not be instantiated\n\n >>> class MixinA(Mixin): pass\n ...\n >>> a = MixinA()\n Traceback (most recent call last):\n File \"\", line 1, in \n File \"mixin.py\", line 44, in mixin_new\n raise InstantiationMixinError(cls)\n __main__.InstantiationMixinError: \n\n### inherit multi mixin\n\nTwo kind of methods to inherit multi mixins, one is writing multi mixins in one decorator:\n\n >>> class MixinA(Mixin): pass\n ...\n >>> class MixinB(Mixin): pass\n ...\n >>> @mixin(MixinA, MixinB)\n ... class M(object): pass\n ...\n\nAnother is using multi decorators:\n\n >>> @mixin(MixinA)\n ... @mixin(MixinB)\n ... class M(object): pass\n ...\n\n### inherit priority\n\nWhen writes multi mixins to one decorator, the left mixin has a higher priority\n\n >>> class MixinA(Mixin):\n ... def mixin_func(self):\n ... return 'do_mixin_a'\n ...\n >>> class MixinB(Mixin):\n ... def mixin_func(self):\n ... return 'do_mixin_b'\n ...\n >>> @mixin(MixinA, MixinB)\n ... class M(object): pass\n ...\n >>> m = M()\n >>> m.mixin_func()\n 'do_mixin_a'\n\nWhen writes multi mixins in multi decorators, the up mixin has a higher priority:\n\n >>> @mixin(MixinA)\n ... @mixin(MixinB)\n ... class M(object): pass\n ...\n >>> m = M()\n >>> m.mixin_func()\n 'do_mixin_a'\n\nIf the class has a funciton which has the same name as the mixin, the mixin method will be overwrite:\n\n >>> class MixinA(Mixin):\n ... def mixin_func(self):\n ... return 'do_mixin_a'\n ...\n >>> @mixin(MixinA)\n ... class A(object):\n ... def mixin_func(self):\n ... return 'real_class_mixin_a'\n ...\n >>> a = A()\n >>> a.mixin_func()\n 'real_class_mixin_a'\n\nIf a class has a father class, and the father class has a same name method as the mixin, the father class' method will be overwrite:\n\n >>> class MixinA(Mixin):\n ... def mixin_func(self):\n ... return 'do_mixin_a'\n ...\n >>> class Father(object):\n ... def mixin_func(self):\n ... return 'father_mixin'\n ...\n >>> @mixin(MixinA)\n ... class A(Father): pass\n ...\n >>> a=A()\n >>> a.mixin_func()\n 'do_mixin_a'\n\n## python version support\n\nTested on python2.6, python2.7 and python3.4", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/yupeng820921/pymixin", "keywords": "mixin", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "mixin", "package_url": "https://pypi.org/project/mixin/", "platform": "any", "project_url": "https://pypi.org/project/mixin/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/yupeng820921/pymixin" }, "release_url": "https://pypi.org/project/mixin/1.1/", "requires_dist": null, "requires_python": null, "summary": "python mixin tool", "version": "1.1" }, "last_serial": 1239022, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "c9af3becd88c69d19f253954ba2f122d", "sha256": "c1ff8d51febf18bacb7ebbaec73ad973a177b2ca629920b7f125b2e6bd3d47ba" }, "downloads": -1, "filename": "mixin-1.0.tar.gz", "has_sig": false, "md5_digest": "c9af3becd88c69d19f253954ba2f122d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3035, "upload_time": "2014-09-26T15:12:02", "url": "https://files.pythonhosted.org/packages/ba/40/67b4b83f2f9def447c3e15e5f1b70540dc6cd7898150ac7ace7b7575bc5a/mixin-1.0.tar.gz" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "971403408b5e8fc62bb5b88afb504ddf", "sha256": "8bb037ca43911c93a8cfa5c8741aa8237c555c6fbaf3d367ffd695a33e842941" }, "downloads": -1, "filename": "mixin-1.1.tar.gz", "has_sig": false, "md5_digest": "971403408b5e8fc62bb5b88afb504ddf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3039, "upload_time": "2014-09-26T15:18:50", "url": "https://files.pythonhosted.org/packages/7a/f6/7e59ac066a9f1d9c98a1e16f4f54986a224680353f91a78ea3965513042e/mixin-1.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "971403408b5e8fc62bb5b88afb504ddf", "sha256": "8bb037ca43911c93a8cfa5c8741aa8237c555c6fbaf3d367ffd695a33e842941" }, "downloads": -1, "filename": "mixin-1.1.tar.gz", "has_sig": false, "md5_digest": "971403408b5e8fc62bb5b88afb504ddf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 3039, "upload_time": "2014-09-26T15:18:50", "url": "https://files.pythonhosted.org/packages/7a/f6/7e59ac066a9f1d9c98a1e16f4f54986a224680353f91a78ea3965513042e/mixin-1.1.tar.gz" } ] }