metaclass

How can the shape of a pytables table column be defined by a variable?

 ̄綄美尐妖づ 提交于 2019-12-13 08:46:37
问题 I'm trying to create an IsDescription subclass, so that I can define the structure of a table I'm trying to create. One of the attributes of the subclass** needs to be shaped given a certain length that is unknown until runtime (it depends on a file being parsed), but is fixed at runtime. Sample code: import tables class MyClass(tables.IsDescription): def __init__(self, param): var1 = tables.Float64Col(shape=(param)) MyClass1 = MyClass(12) Which returns: TypeError: object.__new__() takes no

Python metaclasses tutorial

拟墨画扇 提交于 2019-12-13 07:56:27
问题 Sorry if I have to use you as Google, but Google does not help. I was looking for a very good tutorial about metaclasses in Python. One feature I remember about it was a three-paned image where you had metaclasses, classes and instances, one for each column of the image. The author progressively added blocks for each of them, while explaining the magic behind metaclasses. It was a very good tutorial but I really cannot find it any more. 回答1: You may be referring to Python Types and Objects .

Class that takes another class as argument, copies behavior

北城以北 提交于 2019-12-13 07:26:45
问题 I'd like to create a class in Python that takes a single argument in the constructor, another Python class. The instance of the Copy class should have all the attributes and methods of the original class, without knowing what they should be beforehand. Here's some code that almost works: import copy class A(): l = 'a' class Copy(): def __init__(self, original_class): self = copy.deepcopy(original_class) print(self.l) c = Copy(A) print(c.l) The print statement in the constructor prints 'a',

Metaclass deligate not being instance

霸气de小男生 提交于 2019-12-13 04:49:40
问题 I have this stub of code to add dynamic attributes. I work with mongodb and i want to add the properties dynamically. This is what i tried to do when unit testing. User.metaClass.dynamicAttributes = [:] User.metaClass.propertyMissing = { String name -> delegate.dynamicAttributes[name] } User.metaClass.propertyMissing = { String name, value -> delegate.dynamicAttributes[name] = value } But this fails and i am stepping over my patience limit! User u = new User() u.ppt = 0 User u2 = new User()

Python - Metaclass decorator - How to use @classmethod

旧街凉风 提交于 2019-12-13 04:22:02
问题 I have the following Python metaclass that adds a deco_with_args decorator to each class: def deco_with_args(baz): def decorator(func): ... return func return decorator class Foo(type): def __prepare__(name, bases): return {'deco_with_args': deco_with_args} This allows me to use the decorator like this: class Bar(metaclass=Foo): @deco_with_args('baz') def some_function(self): ... How do I make the deco_with_args decorator behave like an @classmethod so that I can access the Bar class (or

creating Python classes with arbitrarily substituted attribute name

亡梦爱人 提交于 2019-12-13 04:09:18
问题 I apologize for not giving this question a better title; the reason that I am posting it is that I don't even have the correct terminology to know what I am looking for. I have defined a class with an attribute 'spam': def SpamClass(object): def __init__(self, arg): self.spam = arg def __str__(self): return self.spam I want to create a (sub/sibling?)class that has exactly the same functionality, but with an attribute named 'eggs' instead of 'spam': def EggsClass(object): def __init__(self,

Groovy Meta Programming

人走茶凉 提交于 2019-12-13 04:08:23
问题 I want to override a method definition in Grails. I am trying to use Groovy metaprogramming as the class which I want to override belongs to a framework. Below is the original class. class SpringSocialSimpleSignInAdapter implements SignInAdapter { private RequestCache requestCache SpringSocialSimpleSignInAdapter(RequestCache requestCache) { this.requestCache = requestCache; } String signIn(String localUserId, Connection<?> connection, NativeWebRequest request) { SignInUtils.signin localUserId

Groovy and the Metaclass

倾然丶 夕夏残阳落幕 提交于 2019-12-13 03:59:16
问题 I am attempting to use the metaclass to overwrite a static method in a test. The method is: org.boon.HTTP.jsonRestCallWithHeaders It it is a Java class, and it is beyond my control. The declaration is: public static Response jsonRestCallWithHeaders( final String url, final Map<String, ?> headers ) I am attempting to overwrite the method with a closure via: HTTP.metaClass.static.jsonRestCallWithHeaders = { String url, Map<String, ?> headers -> if (url.path?.contains('/quotes')) { [prop1:value1

Python abc inheritance with specified metaclass

*爱你&永不变心* 提交于 2019-12-13 03:47:19
问题 What is the proper way of inheriting from ABC if class have metaclass specified? Straightforward attempt class KindMeta(type): ... class Kind(ABC, metaclass=KindMeta): ... resulted in TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases Inheriting ABCMeta in KindMeta seems kind of wrong, because this mcs is also used to create nonabstract classes. Creating custom ABC inherited from KindMeta sounds well neither. Is

When calling the metaclass bases, object.__init__() takes no parameters

柔情痞子 提交于 2019-12-13 02:57:42
问题 When I try to use this one approach of singleton: class Singleton(object): def __init__(self, name, bases, dict): super(Singleton, self).__init__(name, bases, dict) self._instance = None def __call__(self): if self._instance is None: self._instance = super(Singleton, self).__call__() return self._instance class NewClass(object): __metaclass__ = Singleton I got an error: Error when calling the metaclass bases object. init () takes no parameters I'm not sure, am I correctly understand what the