@measured是标准的装饰者吗?它在哪个库?

时间:2021-03-02 13:19:23

In this blog article they use the construct:

在这篇博客文章中,他们使用了构造:

  @measured
  def some_func():
    #...
  # Presumably outputs something like "some_func() is finished in 121.333 s" somewhere

This @measured directive doesn't seem to work with raw python. What is it?

这个@measured指令似乎不适用于原始python。它是什么?

UPDATE: I see from Triptych that @something is valid, but is where can I find @measured, is it in a library somewhere, or is the author of this blog using something from his own private code base?

更新:我从Triptych看到@something是有效的,但我在哪里可以找到@measured,它是在某个地方的某个库中,还是这个博客的作者使用他自己的私人代码库中的东西?

3 个解决方案

#1


@measured decorates the some_func() function, using a function or class named measured. The @ is the decorator syntax, measured is the decorator function name.

@measured使用名为measured的函数或类来装饰some_func()函数。 @是装饰器语法,测量的是装饰器函数名称。

Decorators can be a bit hard to understand, but they are basically used to either wrap code around a function, or inject code into one.

装饰器可能有点难以理解,但它们基本上用于将代码包装在函数周围,或者将代码注入其中。

For example the measured function (used as a decorator) is probably implemented like this...

例如,测量函数(用作装饰器)可能就像这样实现......

import time

def measured(orig_function):
    # When you decorate a function, the decorator func is called
    # with the original function as the first argument.
    # You return a new, modified function. This returned function
    # is what the to-be-decorated function becomes.

    print "INFO: This from the decorator function"
    print "INFO: I am about to decorate %s" % (orig_function)

    # This is what some_func will become:
    def newfunc(*args, **kwargs):
        print "INFO: This is the decorated function being called"

        start = time.time()

        # Execute the old function, passing arguments
        orig_func_return = orig_function(*args, **kwargs)
        end = time.time()

        print "Function took %s seconds to execute" % (end - start)
        return orig_func_return # return the output of the original function

    # Return the modified function, which..
    return newfunc

@measured
def some_func(arg1):
    print "This is my original function! Argument was %s" % arg1

# We call the now decorated function..
some_func(123)

#.. and we should get (minus the INFO messages):
This is my original function! Argument was 123
# Function took 7.86781311035e-06 to execute

The decorator syntax is just a shorter and neater way of doing the following:

装饰器语法只是一种更简洁,更简洁的方式来执行以下操作:

def some_func():
    print "This is my original function!"

some_func = measured(some_func)

There are some decorators included with Python, for example staticmethod - but measured is not one of them:

Python中包含一些装饰器,例如staticmethod - 但测量不是其中之一:

>>> type(measured)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'measured' is not defined

Check the projects import statements to see where the function or class is coming from. If it uses from blah import * you'll need to check all of those files (which is why import * is discouraged), or you could just do something like grep -R def measured *

检查项目import语句以查看函数或类的来源。如果它使用来自blah import *你需要检查所有这些文件(这就是为什么不鼓励导入*),或者你可以做一些像grep -R def measured *

#2


Yes it's real. It's a function decorator.

是的,这是真的。这是一个功能装饰器。

Function decorators in Python are functions that take a function as it's single argument, and return a new function in it's place.

Python中的函数修饰器是将函数作为单个参数的函数,并在其中返回一个新函数。

@classmethod and @staticmethod are two built in function decorators.

@classmethod和@staticmethod是两个内置函数装饰器。

Read more »

阅读更多 ”

#3


measured is the name of a function that must be defined before that code will work.

measured是在该代码起作用之前必须定义的函数的名称。

In general any function used as a decorator must accept a function and return a function. The function will be replaced with the result of passing it to the decorator - measured() in this case.

通常,用作装饰器的任何函数都必须接受函数并返回函数。该函数将被替换为将其传递给装饰器的结果 - 在这种情况下为measured()。

#1


@measured decorates the some_func() function, using a function or class named measured. The @ is the decorator syntax, measured is the decorator function name.

@measured使用名为measured的函数或类来装饰some_func()函数。 @是装饰器语法,测量的是装饰器函数名称。

Decorators can be a bit hard to understand, but they are basically used to either wrap code around a function, or inject code into one.

装饰器可能有点难以理解,但它们基本上用于将代码包装在函数周围,或者将代码注入其中。

For example the measured function (used as a decorator) is probably implemented like this...

例如,测量函数(用作装饰器)可能就像这样实现......

import time

def measured(orig_function):
    # When you decorate a function, the decorator func is called
    # with the original function as the first argument.
    # You return a new, modified function. This returned function
    # is what the to-be-decorated function becomes.

    print "INFO: This from the decorator function"
    print "INFO: I am about to decorate %s" % (orig_function)

    # This is what some_func will become:
    def newfunc(*args, **kwargs):
        print "INFO: This is the decorated function being called"

        start = time.time()

        # Execute the old function, passing arguments
        orig_func_return = orig_function(*args, **kwargs)
        end = time.time()

        print "Function took %s seconds to execute" % (end - start)
        return orig_func_return # return the output of the original function

    # Return the modified function, which..
    return newfunc

@measured
def some_func(arg1):
    print "This is my original function! Argument was %s" % arg1

# We call the now decorated function..
some_func(123)

#.. and we should get (minus the INFO messages):
This is my original function! Argument was 123
# Function took 7.86781311035e-06 to execute

The decorator syntax is just a shorter and neater way of doing the following:

装饰器语法只是一种更简洁,更简洁的方式来执行以下操作:

def some_func():
    print "This is my original function!"

some_func = measured(some_func)

There are some decorators included with Python, for example staticmethod - but measured is not one of them:

Python中包含一些装饰器,例如staticmethod - 但测量不是其中之一:

>>> type(measured)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'measured' is not defined

Check the projects import statements to see where the function or class is coming from. If it uses from blah import * you'll need to check all of those files (which is why import * is discouraged), or you could just do something like grep -R def measured *

检查项目import语句以查看函数或类的来源。如果它使用来自blah import *你需要检查所有这些文件(这就是为什么不鼓励导入*),或者你可以做一些像grep -R def measured *

#2


Yes it's real. It's a function decorator.

是的,这是真的。这是一个功能装饰器。

Function decorators in Python are functions that take a function as it's single argument, and return a new function in it's place.

Python中的函数修饰器是将函数作为单个参数的函数,并在其中返回一个新函数。

@classmethod and @staticmethod are two built in function decorators.

@classmethod和@staticmethod是两个内置函数装饰器。

Read more »

阅读更多 ”

#3


measured is the name of a function that must be defined before that code will work.

measured是在该代码起作用之前必须定义的函数的名称。

In general any function used as a decorator must accept a function and return a function. The function will be replaced with the result of passing it to the decorator - measured() in this case.

通常,用作装饰器的任何函数都必须接受函数并返回函数。该函数将被替换为将其传递给装饰器的结果 - 在这种情况下为measured()。