accrocchio是一个用来标记和接收臭味代码通知的库(也称为“accrocchio”)。
accrocchio的Python项目详细描述
阿克罗奇奥
accrocchio是一个标记和接收臭味代码通知的库。 (又名“accrocchio”)。
示例
fromaccrocchio.badgeofshameimportaccrocchiofromaccrocchioimportobserversclassAClassThatSmells(metaclass=accrocchio):pass@accrocchiodefa_function_that_smells():passaccrocchio.how_many()# here we have 1, as you have declared a smelly classAClassThatSmells()accrocchio.how_many()# here we have 2, as you have created an instance of a smelly classa_function_that_smells()accrocchio.how_many()# here we have 3, as you have invoked a smelly functionaccrocchio.reset()accrocchio.how_many()# here we have 0# You can also be notified of smelly code execution, such as:classMyAccrocchioObserver(observers.AccrocchioObserver):defon_accrocchio(self):print('Another accrocchio!')defreset(self):print('Reset accrocchi')accrocchio.add_observer(MyAccrocchioObserver())a_function_that_smells()# prints 'Another accrocchio!'accrocchio.reset()# prints 'Reset accrocchi'
也可以装饰整个班级。两份声明 类的实例化增加了accrocchio计数器。
fromaccrocchio.badgeofshameimportaccrocchiofromaccrocchioimportobservers@accrocchioclassAClassThatSmells:passaccrocchio.how_many()# here we have 1, as you have declared a smelly classAClassThatSmells()accrocchio.how_many()# here we have 2, as you have created an instance of a smelly class
您可以使用类型提示声明accrocchio,如下所示:
fromaccrocchio.badgeofshameimportaccrocchio,detonatordeff(a:detonator[int]):passaccrocchio.how_many()# here we have 1, as you have declared a smelly parameterdetonator.how_many()# here we have 1, as you have declared a smelly parameterf(1)accrocchio.how_many()# here we still have 1detonator.how_many()# here we still have 1
库还实现Michael Duell’s resign patterns。
fromaccrocchio.badgeofshameimportaccrocchio,detonator@accrocchiodefaccrocchio_fun():pass@detonatordefdetonator_fun():passaccrocchio_fun()accrocchio.how_many()# here we have 1, as you have invoked an accrocchio functiondetonator.how_many()# here we have 0, as you have never invoked a detonator functiondetonator_fun()detonator.how_many()# here we have 1, as you have invoked a detonator functionaccrocchio.how_many()# here we have 2, as you have invoked a detonator function, which is an accrocchio
您可以将任意代码标记为accrocchio:
fromaccrocchio.badgeofshameimportdetonator,epoxy,this_is_a,this_is_anthis_is_an(epoxy)this_is_a(detonator)detonator.how_many()# this will be 1epoxy.how_many()# this will be 1
如果需要将代码的特定部分标记为 accrocchio,您可以将其用作上下文管理器,如下所示:
fromaccrocchio.badgeofshameimportdetonator,epoxywithdetonator:...withepoxy:...detonator.how_many()# this will be 1epoxy.how_many()# this will be 1
有关已实现的accrocchio辞职模式的完整列表,请 咨询Michael Duell’s resign patterns。
一些最后的注释:
- 只有当软件的一小部分是 阿克罗奇奥
- 我们故意忽略了3.5之前的python版本,正如我们所想的那样 他们是一个完整的阿克洛奇奥。
- 我们故意不把阿克洛奇奥传给“on_阿克洛奇奥” 观察者功能,因为你应该对所有的accrocchioes一视同仁 方式
- accrocchio的复数形式是accrocchioes
- 如果你使用这个库,你是故意残暴 python的禅;因此它被一个更合适的禅所取代。 只要在import accrocchio之后做import this。