标签: inspect

如何以编程方式更改python装饰器中函数的argspec?

给定一个功能:

def func(f1, kw='default'):
    pass
bare_argspec = inspect.getargspec(func)

@decorator
def func2(f1, kw='default'):
    pass
decorated_argspec = inspect.getargspec(func2)
Run Code Online (Sandbox Code Playgroud)

我怎样才能创建这样的装饰器bare_argspec == decorated_argspec

(至于为什么,调用装饰函数的框架执行argspec检查以选择要传入的内容,因此装饰器必须保留相同的argspec才能发挥出色.当我在#python上提出这个问题时,我得到了一个长的关于为什么框架糟糕的演讲,这不是我正在寻找的;我必须在这里解决问题.另外,我也只是对答案感兴趣)

python reflection decorator inspect

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

isinstance没有导入候选人

我们有一个函数,它接受各种不同类型的输入:函数,字符串,编译的正则表达式,Hamcrest匹配器,并根据输入的类型适当地过滤列表.

我们目前正在使用isinstance(our_filter, hamcrest.matcher.Matcher),但这需要我们安装Hamcrest.

我们正在考虑使用字符串匹配inspect.getmro(type(POSSIBLE_MATCHER)); 但这感觉不洁净.import语句可能还有try/ except周围的选项.

什么是最好的方法?


在@dblslash的帮助下,这是迄今为止我所做的最好的:

[x.__module__+"."+x.__name__ for x in inspect.getmro(type(POSSIBLE_MATCHER))] ['hamcrest.core.core.isequal.IsEqual', 'hamcrest.core.base_matcher.BaseMatcher', 'hamcrest.core.matcher.Matcher', 'hamcrest.core.selfdescribing.SelfDescribing', '__builtin__.object']

python class hamcrest inspect isinstance

12
推荐指数
2
解决办法
1646
查看次数

如何从容器中列出Docker挂载的卷

我想列出已装入卷的所有容器目录.

即我能得到类似的信息

docker inspect --format "{{ .Volumes }}" <self>
Run Code Online (Sandbox Code Playgroud)

但是从容器内部并没有docker安装在那里.

我试过cat /proc/mounts,但我找不到合适的过滤器.

mount inspect docker

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

如何在python中获取该函数内的当前函数名称

为了我的日志记录目的,我想记录我的代码所在的函数的所有名称

谁调用函数无关紧要,我想要声明这一行的函数名称

import inspect

def whoami():
    return inspect.stack()[1][3]

def foo():
    print(whoami())
Run Code Online (Sandbox Code Playgroud)

目前它打印foo,我想打印 whoami

python inspect

12
推荐指数
5
解决办法
2万
查看次数

通过 Chrome DevTools API 或面板以编程方式访问 [[Scopes]] (闭包变量)?(不在断点处)

2014 年, JS 代码无法从闭包外部访问闭包内部的变量。从那时起,Chrome 的内部结构已更改为使用[[Scopes]]而不是Closure.

\n\n

Chrome DevTools 现在(2018 年)可以以[[Scopes]]编程方式读取吗?如果是这样,是否有现有的 DevTools 扩展可以做到这一点?

\n\n

用例:检查使用 RequireJS 加载的 UMD 模块内的变量。我知道我可以使用面板在断点或语句处执行此操作,但希望即使不在断点处也能执行此操作。debuggerScopes

\n\n

编辑截至 2017 年,无法访问[[FunctionLocation]],但我不知道[[Scopes]]

\n\n

尝试

\n\n

我调查了以下内容但没有成功(模块工厂函数名称main):

\n\n
    \n
  • 此评论提到了console.dir(),但没有以编程方式访问 的输出console.dir()。我可以console.dir({main})手动使用然后扩展结果,但不能以编程方式。
  • \n
  • 从 DevTools 控制台,我可以使用inspect({function})per this。这让我更接近,但不是[[Scopes]]

    \n\n
    > var x = inspect({main})\n> x.main.name\n\xe2\x86\x90 "main"\n> x.main[Symbol(\'Scopes\')]\n\xe2\x86\x90 undefined\n> x.main[\'[[Scopes]]\']\n\xe2\x86\x90 …
    Run Code Online (Sandbox Code Playgroud)

javascript closures google-chrome inspect google-chrome-devtools

12
推荐指数
0
解决办法
3045
查看次数

嵌套函数装饰器,用于操作python中的参数

我正在编写一个函数装饰器,它将转换应用于函数的第一个参数.如果我只修改一次我的函数,它工作正常,但如果我装饰它们两次我得到一个错误.下面是一些演示该问题的代码,它是我正在处理的代码的简化版本.我已经排除了进行转换的代码,以免分散注意力

from inspect import getargspec
from functools import wraps

def dec(id):
    def _dec(fn):
        @wraps(fn)
        def __dec(*args, **kwargs):
            if len(args):
                return fn(args[0], *args[1:], **kwargs)
            else:
                first_arg = getargspec(fn).args[0]
                new_kwargs = kwargs.copy()
                del new_kwargs[first_arg]
                return fn(kwargs[first_arg], **new_kwargs)
        return __dec
    return _dec

@dec(1)
def functionWithOneDecorator(a, b, c):
    print "functionWithOneDecorator(a = %s, b = %s, c = %s)" % (a, b, c)

@dec(1)
@dec(2)
def functionWithTwoDecorators(a, b, c):
    print "functionWithTwoDecorators(a = %s, b = %s, c = %s)" % (a, b, c)

functionWithOneDecorator(1, 2, …
Run Code Online (Sandbox Code Playgroud)

python decorator inspect kwargs

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

python inspect get方法用@property修饰

这是我实际问题的简化示例.

我有一个这样foo定义的类foo.py:

class foo(object):
    def __init__(self):
        pass

    def bar(self):
        return True

    @property
    def baz(self):
        return False
Run Code Online (Sandbox Code Playgroud)

现在,我想使用该inspect模块来获取foo类的方法(包括baz).这是我到目前为止所做的getmethods.py:

import foo
import inspect

classes = inspect.getmembers(foo, inspect.isclass)
for cls in classes:
    methods = inspect.getmembers(cls[1], inspect.ismethod)
    print methods
Run Code Online (Sandbox Code Playgroud)

当我运行此脚本时,我得到以下输出(这并非完全出乎意料):

[('__init__', <unbound method foo.__init__>), ('bar', <unbound method foo.bar>)]
Run Code Online (Sandbox Code Playgroud)

所以,我的问题是,为什么baz不被认为是一种方法,我如何修改getmethods.py以获得以下输出:

[('__init__', <unbound method foo.__init__>), ('bar', <unbound method foo.bar>), ('baz', <property object at 0x7fbc1a73d260>)]
Run Code Online (Sandbox Code Playgroud)

python properties inspect

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

如何反转Hash.inspect或Array.inspect?(又名.to_s)在Ruby中

我不小心通过调用my_hash.to_s等于的方式将Ruby散列保存到Ruby 1.9中的字符串my_hash.inspect.这给了我一个像这样的字符串:

'{"foo"=>{"bar"=>"baz", "qux"=>"quux"}' 
Run Code Online (Sandbox Code Playgroud)

我现在想将其恢复为哈希值.这是怎么做到的?

我不是在寻找其他序列化技术的解释,我知道它们.我只需要一种方法来恢复它,所以我可以正确的方式保存它.

ruby hash serialization inspect

10
推荐指数
2
解决办法
2569
查看次数

Chrome://检查显示设备,但不显示任何已打开的标签

我正在尝试利用chrome来远程调试android web应用程序.我无法从设备中获取任何打开的标签,以显示在chrome:// inspect list下进行远程调试.

我做了以下事情:

  1. 确保我的设备驱动程序安装得当.
  2. 通过开发人员菜单在我的设备上打开开发人员模式和USB调试.
  3. 选中并通过命令行发出"adb devices"确保我的设备列在adb列表中.

我的设备通过(chrome:// inspect)显示在设备列表下,似乎无法列出任何打开的标签,任何帮助都将非常感激.谢谢

设备:三星Galaxy Tablet 10.1 Android 4.2.2

在此输入图像描述

android google-chrome remote-debugging inspect

10
推荐指数
2
解决办法
6万
查看次数

Firefox 有类似 chrome://inspect 的功能吗?

在 Chrome 中,您可以转到chrome://inspect进入调试页面。火狐浏览器有这样的东西吗?

firefox inspect google-chrome-devtools node-inspector

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