Package: pyrrole
pyrrolepyrroleis a Role System for Python3 (3.6 and high).
It's inspired by the roles implementation in theMoose library of Perl,
and its main purpose is to use instead of Mixin classes and multiple inheritance.ATTENTION: This package isWIPinalphareleaseTestIf you would test before install, follow these instructions:$gitclonehttps://github.com/MatteoGuadrini/pyrrole.git$cdpyrrole$python-munittesttest_role.pyInstallFor install this package, run pip install:$pipinstall--userpyrroleSimple roleHere is an example of how a role can be done:importpyrroleclassFallFromTree(metaclass=pyrrole.Role):passclassFruit:pass@FallFromTreeclassApple(Fruit):passapple=Apple()print(apple.__roles__)In this example, theAppleclass inherits fromFruitbut notFallFromTree.FallFromTreeis a role class that applies to classes so that they acquire therolesattribute.Role methodBy default, all methods or attribute is inherited from a role class except the dunder methods (ex.__str__)..
To force the import of a dunder or normal method, just enclose the method to inherit in arole_methoddecorator.importpyrroleclassFallFromTree(metaclass=pyrrole.Role):@pyrrole.role_methoddef__str__(cls):returnf"Fall from tree"classFruit:pass@FallFromTreeclassApple(Fruit):passapple=Apple()print(apple)More rolesAn ordinary class can have multiple roles at the same time.importpyrrole@pyrrole.role# use role with decoratorclassFallFromTree:def__init__(self,instance):self.trees=list()self.trees.append(instance)deffall(self,tree='tree'):returnf"Fall from{tree}"@pyrrole.role# use role with decoratorclassDeciduous:def__init__(self,instance):self.trees=list()self.trees.append(instance)defclean_leaf(self):returnf"Clean all leaf"classFruit:pass@pyrrole.apply_roles(FallFromTree,Deciduous)# apply more role at same time@pyrrole.rename_role_methods(clean_leaf='clean_apple_leaf')# renamed role namesclassApple(Fruit):defclean_leaf(self):returnf"Clean all leaf of apple tree"apple=Apple()# apple has role?pyrrole.has_role(apple,FallFromTree)# Truepyrrole.has_role(apple,Deciduous)# Trueprint(apple.__roles__)# ['FallFromTree', 'Deciduous']print(apple.clean_apple_leaf())# method renamed from the Deciduous roleprint(apple.fall('apple tree'))# method forced inherited from the FallFromTree roleOpen sourcepyrroleis an open source project. Any contribute, It's welcome.A great thanks.For donations, press thisFor meForTelethonThe Telethon Foundation is a non-profit organization recognized by the Ministry of University and Scientific and Technological Research.
They were born in 1990 to respond to the appeal of patients suffering from rare diseases.
Come today, we are organized to dare to listen to them and answers, every day of the year.Adopt the futureAcknowledgmentsThanks toGiacomo Montagnerfor having the idea. Besides, being a contributor he is a great friend!Thanks to Mark Lutz for writing theLearning PythonandProgramming Pythonbooks that make up my python foundation.Thanks to Kenneth Reitz and Tanya Schlusser for writing theThe Hitchhiker’s Guide to Pythonbooks.Thanks to Dane Hillard for writing thePractices of the Python Probooks.Special thanks go to my wife, who understood the hours of absence for this development.
Thanks to my children, for the daily inspiration they give me and to make me realize, that life must be simple.Thanks Python!