给定一个功能:
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上提出这个问题时,我得到了一个长的关于为什么框架糟糕的演讲,这不是我正在寻找的;我必须在这里解决问题.另外,我也只是对答案感兴趣)
我们有一个函数,它接受各种不同类型的输入:函数,字符串,编译的正则表达式,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']
我想列出已装入卷的所有容器目录.
即我能得到类似的信息
docker inspect --format "{{ .Volumes }}" <self>
Run Code Online (Sandbox Code Playgroud)
但是从容器内部并没有docker
安装在那里.
我试过cat /proc/mounts
,但我找不到合适的过滤器.
为了我的日志记录目的,我想记录我的代码所在的函数的所有名称
谁调用函数无关紧要,我想要声明这一行的函数名称
import inspect
def whoami():
return inspect.stack()[1][3]
def foo():
print(whoami())
Run Code Online (Sandbox Code Playgroud)
目前它打印foo
,我想打印 whoami
2014 年, JS 代码无法从闭包外部访问闭包内部的变量。从那时起,Chrome 的内部结构已更改为使用[[Scopes]]
而不是Closure
.
Chrome DevTools 现在(2018 年)可以以[[Scopes]]
编程方式读取吗?如果是这样,是否有现有的 DevTools 扩展可以做到这一点?
用例:检查使用 RequireJS 加载的 UMD 模块内的变量。我知道我可以使用面板在断点或语句处执行此操作,但希望即使不在断点处也能执行此操作。debugger
Scopes
编辑截至 2017 年,无法访问[[FunctionLocation]]
,但我不知道[[Scopes]]
。
我调查了以下内容但没有成功(模块工厂函数名称main
):
console.dir()
,但没有以编程方式访问 的输出console.dir()
。我可以console.dir({main})
手动使用然后扩展结果,但不能以编程方式。从 DevTools 控制台,我可以使用inspect({function})
per this。这让我更接近,但不是[[Scopes]]
:
> 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
我正在编写一个函数装饰器,它将转换应用于函数的第一个参数.如果我只修改一次我的函数,它工作正常,但如果我装饰它们两次我得到一个错误.下面是一些演示该问题的代码,它是我正在处理的代码的简化版本.我已经排除了进行转换的代码,以免分散注意力
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) 这是我实际问题的简化示例.
我有一个这样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) 我不小心通过调用my_hash.to_s
等于的方式将Ruby散列保存到Ruby 1.9中的字符串my_hash.inspect
.这给了我一个像这样的字符串:
'{"foo"=>{"bar"=>"baz", "qux"=>"quux"}'
Run Code Online (Sandbox Code Playgroud)
我现在想将其恢复为哈希值.这是怎么做到的?
我不是在寻找其他序列化技术的解释,我知道它们.我只需要一种方法来恢复它,所以我可以正确的方式保存它.
我正在尝试利用chrome来远程调试android web应用程序.我无法从设备中获取任何打开的标签,以显示在chrome:// inspect list下进行远程调试.
我做了以下事情:
我的设备通过(chrome:// inspect)显示在设备列表下,似乎无法列出任何打开的标签,任何帮助都将非常感激.谢谢
设备:三星Galaxy Tablet 10.1 Android 4.2.2
在 Chrome 中,您可以转到chrome://inspect
进入调试页面。火狐浏览器有这样的东西吗?
inspect ×10
python ×5
decorator ×2
android ×1
class ×1
closures ×1
docker ×1
firefox ×1
hamcrest ×1
hash ×1
isinstance ×1
javascript ×1
kwargs ×1
mount ×1
properties ×1
reflection ×1
ruby ×1