From 7caf795ee87eacc44adc659f4023fdef6237480b Mon Sep 17 00:00:00 2001 From: geekpi Date: Wed, 19 May 2021 08:46:07 +0800 Subject: [PATCH] translated --- ... features that are still relevant today.md | 176 ------------------ ... features that are still relevant today.md | 176 ++++++++++++++++++ 2 files changed, 176 insertions(+), 176 deletions(-) delete mode 100644 sources/tech/20210514 3 Python 3.2 features that are still relevant today.md create mode 100644 translated/tech/20210514 3 Python 3.2 features that are still relevant today.md diff --git a/sources/tech/20210514 3 Python 3.2 features that are still relevant today.md b/sources/tech/20210514 3 Python 3.2 features that are still relevant today.md deleted file mode 100644 index 218a600cc9..0000000000 --- a/sources/tech/20210514 3 Python 3.2 features that are still relevant today.md +++ /dev/null @@ -1,176 +0,0 @@ -[#]: subject: (3 Python 3.2 features that are still relevant today) -[#]: via: (https://opensource.com/article/21/5/python-32) -[#]: author: (Moshe Zadka https://opensource.com/users/moshez) -[#]: collector: (lujun9972) -[#]: translator: (geekpi) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) - -3 Python 3.2 features that are still relevant today -====== -Explore some of the underutilized but still useful Python features. -![Business woman on laptop sitting in front of window][1] - -This the third article in a series about features that first appeared in a version of Python 3.x. Some of those Python versions have been out for a while. For example, Python 3.2 was first released in 2011, yet some of the cool and useful features introduced in it are still underused. Here are three of them. - -### argparse subcommands - -The `argparse` module first appeared in Python 3.2. There are many third-party modules for command-line parsing. But the built-in `argparse` module is more powerful than many give it credit for. - -Documenting all the ins and outs of `argparse` would take its own article series. For a small taste, here is an example of how you can do subcommands with `argparse`. - -Imagine a command with two subcommands: `negate`, which takes one argument, and `multiply` which takes two: - - -``` -$ computebot negate 5 --5 -$ computebot multiply 2 3 -6 - -[/code] [code] - -import argparse - -parser = argparse.ArgumentParser() -subparsers = parser.add_subparsers() -``` - -The `add_subparsers()` methods creates an object that you can add subcommands to. The only trick to remember is that you need to add what subcommand was called through a `set_defaults()`: - - -``` -negate  = subparsers.add_parser("negate") -negate.set_defaults(subcommand="negate") -negate.add_argument("number", type=float) - -[/code] [code] - -multiply  = subparsers.add_parser("multiply") -multiply.set_defaults(subcommand="multiply") -multiply.add_argument("number1", type=float) -multiply.add_argument("number2", type=float) -``` - -One of my favorite `argparse` features is that, because it separates parsing from running, testing the parsing logic is particularly pleasant. - - -``` -`parser.parse_args(["negate", "5"])`[/code] [code]`    Namespace(number=5.0, subcommand='negate')`[/code] [code]`parser.parse_args(["multiply", "2", "3"])`[/code] [code]`    Namespace(number1=2.0, number2=3.0, subcommand='multiply')` -``` - -### contextlib.contextmanager - -Contexts are a powerful tool in Python. While many _use_ them, writing a new context often seems like a dark art. With the `contextmanager` decorator, all you need is a one-shot generator. - -Writing a context that prints out the time it took to do something is as simple as: - - -``` -import contextlib, timeit - -@contextlib.contextmanager -def timer(): -    before = timeit.default_timer() -    try: -        yield -    finally: -        after = timeit.default_timer() -        print("took", after - before) -``` - -And you can use it with just: - - -``` -import time - -with timer(): -    time.sleep(10.5) - -[/code] [code]`    took 10.511025413870811` -``` - -### functools.lru_cache - -Sometimes the caching results from a function in memory make sense. For example, imagine the classical problem: "How many ways can you make change for a dollar with quarters, dimes, nickels, and cents?" - -The code for this can be deceptively simple: - - -``` -def change_for_a_dollar(): -    def change_for(amount, coins): -        if amount == 0: -            return 1 -        if amount < 0 or len(coins) == 0: -            return 0 -        some_coin = next(iter(coins)) -        return ( -            change_for(amount, coins - set([some_coin])) -            + -            change_for(amount - some_coin, coins) -        ) -    return change_for(100, frozenset([25, 10, 5, 1])) -``` - -On my computer, this takes around 13ms: - - -``` -with timer(): -    change_for_a_dollar() - -[/code] [code]`    took 0.013737603090703487` -``` - -It turns out that when you calculate how many ways you can do something like making change from 50 cents, you use the same coins repeatedly. You can use `lru_cache` to avoid recalculating this over and over. - - -``` -import functools - -def change_for_a_dollar(): -    @functools.lru_cache -    def change_for(amount, coins): -        if amount == 0: -            return 1 -        if amount < 0 or len(coins) == 0: -            return 0 -        some_coin = next(iter(coins)) -        return ( -            change_for(amount, coins - set([some_coin])) -            + -            change_for(amount - some_coin, coins) -        ) -    return change_for(100, frozenset([25, 10, 5, 1])) - -[/code] [code] - -with timer(): -    change_for_a_dollar() - -[/code] [code]`    took 0.004180959425866604` -``` - -A three-fold improvement for the cost of one line. Not bad. - -### Welcome to 2011 - -Although Python 3.2 was released 10 years ago, many of its features are still cool—and underused. Add them to your toolkit if you haven't already. - --------------------------------------------------------------------------------- - -via: https://opensource.com/article/21/5/python-32 - -作者:[Moshe Zadka][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://opensource.com/users/moshez -[b]: https://github.com/lujun9972 -[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-concentration-focus-windows-office.png?itok=-8E2ihcF (Woman using laptop concentrating) diff --git a/translated/tech/20210514 3 Python 3.2 features that are still relevant today.md b/translated/tech/20210514 3 Python 3.2 features that are still relevant today.md new file mode 100644 index 0000000000..9556dce213 --- /dev/null +++ b/translated/tech/20210514 3 Python 3.2 features that are still relevant today.md @@ -0,0 +1,176 @@ +[#]: subject: (3 Python 3.2 features that are still relevant today) +[#]: via: (https://opensource.com/article/21/5/python-32) +[#]: author: (Moshe Zadka https://opensource.com/users/moshez) +[#]: collector: (lujun9972) +[#]: translator: (geekpi) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) + +3 个 Python 3.2 到今天仍然有用的功能 +====== +探索一些未被充分利用但仍然有用的 Python 特性。 +![Business woman on laptop sitting in front of window][1] + +这是关于首次出现在 Python 3.x 版本中的特性的系列文章中的第三篇。其中一些 Python 版本已经推出了一段时间。例如,Python 3.2 是在 2011 年首次发布的,但其中引入的一些很酷、很有用的特性仍然没有被使用。下面是其中的三个。 + +### argparse 子命令 + +`argparse` 模块首次出现在 Python 3.2 中。有许多用于命令行解析的第三方模块。但是内置的 `argparse` 模块比许多人认为的要强大。 + +记录所有的 `argparse` 的特性将需要自己的系列文章。下面是一个例子,说明如何用 `argparse` 做子命令。 + +想象一下,一个命令有两个子命令:`negate`,需要一个参数,`multiply`,需要两个参数: + + +``` +$ computebot negate 5 +-5 +$ computebot multiply 2 3 +6 + +[/code] [code] + +import argparse + +parser = argparse.ArgumentParser() +subparsers = parser.add_subparsers() +``` + +`add_subparsers()` 方法创建一个对象,你可以向其添加子命令。唯一需要记住的技巧是,你需要添加通过 `set_defaults()` 调用的子命令: + + +``` +negate = subparsers.add_parser("negate") +negate.set_defaults(subcommand="negate") +negate.add_argument("number", type=float) + +[/code] [code] + +multiply = subparsers.add_parser("multiply") +multiply.set_defaults(subcommand="multiply") +multiply.add_argument("number1", type=float) +multiply.add_argument("number2", type=float) +``` + +我最喜欢的一个 `argparse` 功能是,因为它把解析和运行分开,测试解析逻辑特别令人愉快。 + + +``` +`parser.parse_args(["negate", "5"])`[/code] [code]` Namespace(number=5.0, subcommand='negate')`[/code] [code]`parser.parse_args(["multiply", "2", "3"])`[/code] [code]` Namespace(number1=2.0, number2=3.0, subcommand='multiply')` +``` + +### contextlib.contextmanager + +上下文是 Python 中一个强大的工具。虽然很多人_使用_它们,但编写一个新的上下文常常看起来像一门黑暗的艺术。有了 `contextmanager` 装饰器,你所需要的只是一个一次性的生成器。 + +编写一个打印出做某事所需时间的上下文,就像这样简单: + + +``` +import contextlib, timeit + +@contextlib.contextmanager +def timer(): + before = timeit.default_timer() + try: + yield + finally: + after = timeit.default_timer() + print("took", after - before) +``` + +你可以这样使用: + + +``` +import time + +with timer(): + time.sleep(10.5) + +[/code] [code]` took 10.511025413870811` +``` + +### functools.lru_cache + +有时,在内存中缓存一个函数的结果是有意义的。例如,想象一下经典的问题:”有多少种方法可以用 25 美分、1 美分、2 美分和 3 美分可以来换取 1 美元?“ + +这个问题的代码可以说是非常简单: + + +``` +def change_for_a_dollar(): + def change_for(amount, coins): + if amount == 0: + return 1 + if amount < 0 or len(coins) == 0: + return 0 + some_coin = next(iter(coins)) + return ( + change_for(amount, coins - set([some_coin])) + + + change_for(amount - some_coin, coins) + ) + return change_for(100, frozenset([25, 10, 5, 1])) +``` + +在我的电脑上,这需要 13ms 左右: + + +``` +with timer(): + change_for_a_dollar() + +[/code] [code]` took 0.013737603090703487` +``` + +事实证明,当你计算有多少种方法可以做一些事情,比如用 50 美分找钱,你会重复使用相同的硬币。你可以使用 `lru_cache` 来避免重复计算。 + + +``` +import functools + +def change_for_a_dollar(): + @functools.lru_cache + def change_for(amount, coins): + if amount == 0: + return 1 + if amount < 0 or len(coins) == 0: + return 0 + some_coin = next(iter(coins)) + return ( + change_for(amount, coins - set([some_coin])) + + + change_for(amount - some_coin, coins) + ) + return change_for(100, frozenset([25, 10, 5, 1])) + +[/code] [code] + +with timer(): + change_for_a_dollar() + +[/code] [code]` took 0.004180959425866604` +``` + +一行的代价是三倍的改进。不错。 + +### 欢迎来到 2011 年 + +尽管 Python 3.2 是在 10 年前发布的,但它的许多特性仍然很酷,而且没有得到充分利用。如果你还没使用,那么将他们添加到你的工具箱中。 + +-------------------------------------------------------------------------------- + +via: https://opensource.com/article/21/5/python-32 + +作者:[Moshe Zadka][a] +选题:[lujun9972][b] +译者:[geekpi](https://github.com/geekpi) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://opensource.com/users/moshez +[b]: https://github.com/lujun9972 +[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/lenovo-thinkpad-laptop-concentration-focus-windows-office.png?itok=-8E2ihcF (Woman using laptop concentrating) \ No newline at end of file