小编avo*_*d3d的帖子

Python装饰器内部调用错误的函数

我们实现了一个扭曲的Web API。

为了处理身份验证,我们使用了一个装饰器,该装饰器包装了一些路由。

@requires_auth(roles=[Roles.Admin])
def get_secret_stuff(request):
    return 42
Run Code Online (Sandbox Code Playgroud)

requires_auth包装被执行如下。

def requires_auth(roles):
    def wrap(f):
        def wrapped_f(request, *args, **kwargs):
            # If the user is authenticated then...
            return f(request, *args, **kwargs)
        return wrapped_f
    return wrap
Run Code Online (Sandbox Code Playgroud)

问题是,如果此装饰器有多条路线,则对其中任何一条的调用都会导致要装饰的最新路线被调用。

这显然不是我想要的,并且与我对装饰器应如何工作的理解背道而驰。我在代码中添加了一些打印语句以尝试找出问题:

def requires_auth(roles):
    def wrap(f):
        print(f) # This shows that the decorator is being called correctly once per each
                 # route that is decorated
        def wrapped_f(request, *args, **kwargs):
            # If the user is authenticated then...
            return f(request, *args, **kwargs)
        return wrapped_f
    return wrap
Run Code Online (Sandbox Code Playgroud)

如果很重要,我将为其中的某些路线使用twisted的inlineCallbacks,并@app.route(url, …

python twisted decorator

5
推荐指数
1
解决办法
472
查看次数

FFmpeg使用文件输入计算mp3持续时间,但管道失败?

我正在尝试使用ffmpeg从mp3获取PCM数据,但文件存储在数据库gridfs中,所以我试图使用管道为ffmpeg提供一些成功的数据,但是有一个文件,ffmpeg处理correctlt如果用文件名作为输入,并且在将文件作为管道给出时不正确:(任何想法为什么?

ffmpeg -i - -f s16le -acodec pcm_s16le output.raw < testMp3s/test-corrupt.mp3 
Run Code Online (Sandbox Code Playgroud)

ffmpeg version 0.11.1 Copyright (c) 2000-2012 the FFmpeg developers
built on Jun  9 2012 13:50:13 with gcc 4.7.0 20120505 (prerelease)
configuration: --prefix=/usr --enable-libmp3lame --enable-libvorbis --enable-libxvid --        enable-libx264 --enable-libvpx --enable-libtheora --enable-libgsm --enable-libspeex -- enable-postproc --enable-shared --enable-x11grab --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libschroedinger --enable-libopenjpeg --enable-librtmp --enable-libpulse --enable-libv4l2 --enable-gpl --enable-version3 --enable-runtime-cpudetect --  disable-debug --disable-static
libavutil      51. 54.100 / 51. 54.100
libavcodec     54. 23.100 / 54. 23.100
libavformat    54.  6.100 / 54.  6.100
libavdevice    54. …
Run Code Online (Sandbox Code Playgroud)

bash shell ffmpeg pipe

1
推荐指数
1
解决办法
1622
查看次数

标签 统计

bash ×1

decorator ×1

ffmpeg ×1

pipe ×1

python ×1

shell ×1

twisted ×1