使用python编写嗅探器_Python中的装饰器:为什么以及如何使用它们并自己编写

使用python编写嗅探器

When you’re just getting started with Python, essentially everything you’re learning is foundational. There is not a lot of time or motivation to stop and ask when will I use this? when you’re learning about basic things like data types, conditionals, loops, and functions…because the answer is as a Python programmer, you’ll use all of these things all the time! Whether you are a data scientist building machine learning models, or a software developer building the backend of a website, it’s unlikely you’ll be able to make any progress at all if you don’t know the basics.

你是刚刚开始使用PythonW¯¯母鸡,基本上一切你学习是基础。 没有太多时间或动力停止询问我什么时候使用它? 当您学习诸如数据类型,条件,循环和函数之类的基本知识时……因为答案是作为Python程序员,您将始终使用所有这些东西! 无论您是构建机器学习模型的数据科学家,还是构建网站后端的软件开发人员,如果您不了解基础知识,那么您根本不可能取得任何进展。

But once you understand the essential foundations of Python, it can be challenging to identify which new tools and concepts will be helpful — and possibly even necessary—in your advancement, compared to those that might just be a waste of your time.

但是,一旦您了解了Python的基本基础,与那些可能只是浪费时间的工具和概念相比,确定哪些新工具和概念对您的前进将是有帮助的,甚至可能是必要的。

In this story, I’ll walk you through:

在这个故事中,我将向您介绍:

  1. Review of SOLID software development principles

    审查SOLID软件开发原则

  2. Examples of “tacked-on” scenarios that are challenging to implement while following the SOLID principles

    遵循SOLID原则时难以实施的“附加”方案示例
  3. How a decorator approach overcomes this challenge

    装饰方法如何克服这一挑战

  4. Using Python decorators written by others

    使用其他人编写的Python装饰器

  5. Writing your own decorators in Python

    用Python编写自己的装饰器

The code in this blog post can also be found in this Google Colaboratory notebook:

此博客文章中的代码也可以在以下Google合作实验室笔记本中找到:

SOLID原则 (SOLID principles)

If you’re planning on contributing to “real” software projects, not just one-off scripts or personal projects, it’s a good idea to be familiar with the SOLID principles.

如果您打算为“实际”软件项目做出贡献,而不仅仅是一次性脚本或个人项目,那么熟悉SOLID原则是个好主意。

Green circular logo with a check mark and the words “clean code”
Uncle Bob Consulting, LLC 叔叔鲍勃咨询有限公司

The SOLID principles were originally created by Robert C. Martin, AKA “Uncle Bob” of Uncle Bob Consulting. To quote from his website:

SOLID原则最初是由Bob Uncle Bob Consulting的AKA“ Uncle Bob”创建的。 引用他的网站

The principles are mental cubby-holes. They give a name to a concept so that you can talk and reason about that concept. They provide a place to hang the feelings we have about good and bad code. They attempt to categorize those feelings into concrete advice.

原则是精神上的漏洞。 他们给一个概念起了个名字,以便您可以谈论和推理这个概念。 它们提供了一个地方,可以使我们对代码的好坏有所感触 。 他们试图将这些感觉归类为具体建议。

The 5 SOLID principles are:

SOLID的5条原则是:

  • Single-Responsibility Principle

    小号英格尔-责任原则

  • Open-Closed Principle

    Ø笔封闭原则

  • Liskov Substitution Principle

    大号 iskov替换原则

  • Interface Segregation Principle

    覆盖整个院落分离原则

  • Dependency Inversion Principle

    d ependency倒置原则

The two we’re going to focus on in this blog post are the single-responsibility principle and the open-closed principle. These are the two principles that are most challenging to follow in the “tacked-on” scenarios described later.

我们在本博文中将重点关注的是单一责任原则和开放式封闭原则。 这是在后面所述的“固定”方案中要遵循的最具挑战性的两个原则。

单一责任原则 (Single-responsibility principle)

Every component of a piece of software should only be responsible for a single set of functionality. In the context of the examples in this blog, a “component” will mean a Python function.

一个软件的每个组件应该只负责一组功能。 在本博客中的示例上下文中,“组件”表示Python函数。

开闭原理 (Open-closed principle)

Already-written software should be “closed” for modification, but “open” for extension. Meaning, we should avoid re-writing existing code whenever possible, but ideally we should be able to add extended functionality to that code.

已经编写的软件应“关闭”以进行修改,而应“打开”以进行扩展。 意思是,我们应该尽可能避免重写现有代码,但是理想情况下,我们应该能够为该代码添加扩展功能。

“附加”软件方案 (“Tacked-on” software scenarios)

While “SOLID” is a widely-known term in software development, “tacked-on” is just a name that I came up with for a category of software scenarios where you have a perfectly-functional software library, but now a stakeholder is asking you to “tack on” some more functionality that is related to the original functionality while simultaneously being conceptually distinct.

虽然“ SOLID”是软件开发中一个众所周知的术语,但是“ tacked-on”只是我在一个具有完美功能的软件库的软件场景类别中使用的名称,但是现在,利益相关者正在询问您可以“附加”更多与原始功能相关的功能,同时又在概念上有所区别。

Some examples of “tacked-on” software scenarios are:

“附加”软件方案的一些示例包括:

  • Caching

    快取
  • Logging

    记录中
  • Access control

    访问控制
  • Input validation

    输入验证
  • Tweaks to input or output format

    调整输入或输出格式

In all of these cases, you want to have the original functionality, but also every time you invoke that original functionality, you want something else to happen on top of it.

在所有这些情况下,您都希望拥有原始功能,而且每次调用该原始功能时,您都希望在其上发生其他事情。

缓存示例 (Caching example)

Let’s focus on caching as a specific example of a “tacked-on” scenario. In general, caching means that you save the result of a costly operation in a cache (usually implemented as some form of a dictionary/hashmap) so that later you can just look up that result instead of re-doing that costly operation.

让我们专注于缓存作为“固定”方案的特定示例。 通常, 缓存意味着将代价高昂的操作结果保存在缓存中 (通常以某种形式的字典/哈希图的形式实现),以便稍后您可以查找该结果,而不用重新执行该代价高昂的操作。

If you’ve done any technical interview algorithms practice before, you’ve likely encountered the task of optimizing the recursive Fibonacci number algorithm. We’ll use it as a stand-in for a more realistic computing task. An example implementation of this algorithm (found in the Python docs) is:

如果您以前做过任何技术面试算法练习,那么您可能已经遇到了优化递归斐波那契数算法的任务。 我们将使用它作为更实际的计算任务的替身。 该算法的示例实现(可在Python docs中找到)是:

def fib(n):
if n < 2:
return n
return fib(n-1) + fib(n-2)

Let’s assume that your coworker already wrote that code as part of a larger software project, as well as this code (standing in for a more realistic unit test suite) to test the functionality:

假设您的同事已经将该代码作为较大的软件项目的一部分进行了编写,同时还假定该代码(代表更现实的单元测试套件)已用于测试功能:

assert fib(25) == 75025

The problem with this implementation is that you end up re-computing the same values repeatedly. For example, if you’re computing fib(10) that means fib(9) + fib(8). Then fib(9) =fib(8) + fib(7) and fib(8) = fib(7) + fib(6). Already you can see that fib(8) and fib(7) are being computed twice, and this repetition cascades through the rest of the recursion. How can you make this code faster?

此实现的问题是,最终您需要重复地重新计算相同的值。 例如,如果您要计算fib(10) ,则意味着fib(9) + fib(8) 。 然后fib(9) = fib(8) + fib(7)fib(8) = fib(7) + fib(6) 。 您已经看到fib(8)fib(7)进行了两次计算,并且此重复在其余的递归过程中级联。 如何使此代码更快?

The “known” way to optimize your Fibonacci solution is to add in caching, so that once fib(8) is calculated, you never have to calculate it again, you just have to retrieve it from the cache. But how to do that while following the SOLID principles?

优化Fibonacci解决方案的“已知”方法是添加缓存,这样一来,一旦计算出fib(8)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值