标签: introspection

使用super的问题(python 2.5.2)

我正在为我的程序编写一个插件系统,我无法解决一件事:

class ThingLoader(object):
'''
Loader class
'''

    def loadPlugins(self):
        '''
        Get all the plugins from plugins folder
        '''
        from diones.thingpad.plugin.IntrospectionHelper import loadClasses

        classList=loadClasses('./plugins', IPlugin)#Gets a list of 
        #plugin classes
        self.plugins={}#Dictionary that should be filled with 
        #touples of objects and theirs states, activated, deactivated.
        classList[0](self)#Runs nicelly
        foo = classList[1]
        print foo#prints <class 'TestPlugin.TestPlugin'>
        foo(self)#Raise an exception
Run Code Online (Sandbox Code Playgroud)

测试插件看起来像这样:

import diones.thingpad.plugin.IPlugin as plugin
   class TestPlugin(plugin.IPlugin):
       '''
     classdocs
    '''
    def __init__(self, loader):
        self.name='Test Plugin'
        super(TestPlugin, self).__init__(loader)
Run Code Online (Sandbox Code Playgroud)

现在IPlugin看起来像这样:

class IPlugin(object):
    '''
    classdocs
    '''
    name=''
    def …
Run Code Online (Sandbox Code Playgroud)

python introspection python-datamodel

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

Python可以确定访问方法的对象的类

无论如何要做这样的事情:

class A:
    def foo(self):
        if isinstance(caller, B):
           print "B can't call methods in A"
        else:
           print "Foobar"
class B:
    def foo(self, ref): ref.foo()

class C:
    def foo(self, ref): ref.foo()


a = A();
B().foo(a)    # Outputs "B can't call methods in A"
C().foo(a)    # Outputs "Foobar"
Run Code Online (Sandbox Code Playgroud)

调用者在哪里A使用某种形式的内省来确定调用方法对象的类?

编辑:

最后,我根据一些建议把它放在一起:

import inspect
...
def check_caller(self, klass):
    frame = inspect.currentframe()
    current = lambda : frame.f_locals.get('self')
    while not current() is None:
        if isinstance(current(), klass): return True
        frame = frame.f_back …
Run Code Online (Sandbox Code Playgroud)

python introspection

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

使用字典自动填充Objective C实例的所有属性

我有一个属性的类,我想从字典中设置值.

换句话说,我想自动化这个:

objectInstace.val1 = [dict objectForKey:@"val1"];
objectInstace.val2 = [dict objectForKey:@"val2"];
Run Code Online (Sandbox Code Playgroud)

用这样的东西(伪代码):

for key, value in dict:
    setattr(objectInstance, key, value)
Run Code Online (Sandbox Code Playgroud)

introspection objective-c

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

获取没有方法和内置函数的类的类和对象属性

说我有这个班:

class MyClass(object):
  my_attrib = 'foo'
  my_other_attrib = 'bar'

  def mymethod():
    pass
Run Code Online (Sandbox Code Playgroud)

现在我怎样才能获得类MyClass,WITHOUT方法和内置类等的属性__dict__

当我{'my_attrib':'foo', 'my_other_attrib':'bar'}应用于上面的类时,我希望得到一本字典.

python introspection

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

对Objective-C对象属性的反思

是否可以在不使用运行时方法的情况下以编程方式获取objective-c中对象的属性?我只是问,因为它似乎没有必要在运行时检查它,它什么时候不会改变.我正在考虑这个效果:

MyObject *foo = [[MyObject alloc] init];
NSDictionary *propertiesNamesAndValues = [foo getAllProperties];
Run Code Online (Sandbox Code Playgroud)

目前我的解决方案如下:

id currentClass = [MyObject class];
NSString *propertyName;
unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList(currentClass, &outCount);
for (i = 0; i < outCount; i++) 
{
    objc_property_t property = properties[i];
    propertyName = [NSString stringWithCString:property_getName(property)];
    NSLog(propertyName);
    NSLog(@"%@",[foo valueForKey:propertyName]);
} 
Run Code Online (Sandbox Code Playgroud)

oop introspection objective-c ios

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

Python - 如何查询定义方法的类?

我的问题有点类似于这个问题; 它涉及对象方法而不是模块内容.我想知道我是否可以使用该inspect模块来获取仅在我询问的类中定义的方法,而不是它的父级.

我需要这个,因为我的子类定义了'宏'方法,它在更高的抽象级别访问父方法,我不希望用户不得不担心在继承中一直定义的低级方法树.

这是一个简化的例子:

class Foo(object):
    def __init__(self): pass
    def f1(self): return 3
    def f2(self): return 1

class Bar(Foo):
    def __init__(self): Foo.__init__(self)
    def g1(self): return self.f1() + self.f2()
    def g2(self): return self.f1() - self.f2()

import inspect
inspect.getmembers(Bar, inspect.ismethod)
Run Code Online (Sandbox Code Playgroud)

输出:

[('__init__', <unbound method Bar.__init__>),
 ('f1', <unbound method Bar.f1>),
 ('f2', <unbound method Bar.f2>),
 ('g1', <unbound method Bar.g1>),
 ('g2', <unbound method Bar.g2>)]
Run Code Online (Sandbox Code Playgroud)

用户不需要知道或关心fs 的存在,因为她只对gs 有兴趣.(当然,这个输出在绝大多数上下文中都是有意义的,因为所有这些方法在实例化时都会被绑定到对象.)对于一个长继承树,返回的列表可以变得很长并且充满了所有的东西.与用户无关.

我怎样才能得到它离开f1f2关闭这个名单?是否有类似于__module__类中定义的方法的属性?更好的是,是否可以使用实例 …

python methods introspection inspect

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

在编译时块会发生什么,我可以在运行时创建一个块吗?

这是关于Objective-C中(^ {})的两部分问题.我已经搜索了一些答案,但没有任何内容出现在Google或SO中.这个问题源于为iOS创建自定义XML Layout Engine的愿望,支持块 - 这意味着我想解析NSStrings并在运行时创建一个块.

1)这甚至可能吗?如果是这样,怎么办呢?

无法找到太多NSString to Block,我认为原因可能是编译器如何处理一个块 - 所以我再次搜索一个答案,但空手而归.所以:

2)在Objective-C编译时块发生了什么?

introspection objective-c objective-c-blocks

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

为什么在访问Python对象属性时使用getattr()而不是__dict__?

在源示例和具有某种程度的Python对象内省的SO答案中,常见的模式是:

getattr(some_object, attribute_name_string)
Run Code Online (Sandbox Code Playgroud)

是否有理由将此模式作为首选:

some_object.__dict__[attribute_name_string]
Run Code Online (Sandbox Code Playgroud)

这似乎更直接地显示了正在发生的事情?是因为后者太接近CPython中可能会发生变化的特定实现吗?

NB原始问题错误地将流行的成语识别为:

some_object.__getattr__(attribute_name_string)
Run Code Online (Sandbox Code Playgroud)

python introspection

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

确定用于提供到JSON转换的Moose类型

我有一个班,MyClass:

package MyClass;
use Moose;

has 'IntegerMember' => (
    is => 'rw',
    isa => 'Int'
);

has 'BooleanMember' => (
    is => 'rw',
    isa => 'Bool'
);

sub TO_JSON {
    my $self = shift;
    return { % { $self } };
}
Run Code Online (Sandbox Code Playgroud)

目前,当我实例化MyClass并将新对象传递给json_encoder时,我得到了一个按预期返回的JSON字符串.我希望将perl布尔值(1,0)转换为(true,false),但这不是JSON模块的设计方式:

use JSON;
use MyClass;

my $object_to_encode = MyClass->new ( 
    IntegerMember => 10,
    BooleanMember => 1
);
my $json_encoder = JSON->new->convert_blessed;
my $json_data = $json_encoder->encode( $object_to_encode );
Run Code Online (Sandbox Code Playgroud)

在MyClass中,我想改进我的TO_JSON子例程,以提供任何Moose'Bool'成员从(1或0)到(true或false)的转换:

sub TO_JSON {
    my $self = shift;
    for my $member …
Run Code Online (Sandbox Code Playgroud)

perl json introspection moose

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

Haskell宏从他们的名字到一些表达式创建一个Map?

我有一些变数ab.我想Data.Map.fromList [("a",a),("b",b)]通过输入类似的东西来快速创建地图magic [a,b].我想在GHCI中这样做,而不是在模块中.

我花了一些时间学习模板Haskell,但我仍然无法判断它是否可能.是吗?

macros haskell introspection template-haskell

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