metaclass

Using ABC, PolymorphicModel, django-models gives metaclass conflict

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:36:26
问题 So far every other answer on SO answers in the exact same way: construct your metaclasses and then inherit the 'joined' version of those metaclasses, i.e. class M_A(type): pass class M_B(type): pass class A(metaclass=M_A): pass class B(metaclass=M_B): pass class M_C(M_A, M_B): pass class C:(A, B, metaclass=M_C): pass But I don't know what world these people are living in, where they're constructing your own metaclasses! Obviously, one would be using classes from other libraries and unless you

Django: Error when calling the metaclass bases

烂漫一生 提交于 2019-12-23 07:55:48
问题 Here is the error TypeError: Error when calling the metaclass bases metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases The class in question within my models.py class Business(models.Model, forms.Form): name = models.CharField(max_length=128) tel_no = models.CharField(max_length=11) address_ln1 = models.CharField(max_length=128) address_ln2 = models.CharField(max_length=128) city = models.CharField(max_length=64) county =

Dynamically creating sqlAlchemy Metaclass based on table library

ぐ巨炮叔叔 提交于 2019-12-23 04:35:15
问题 We have several clients in a ERP system. Each client has it's own database. The databases are identical in terms of schema. Do not ask me why, but the ERP db does not have formally defined PKs, and so it is not possible to reflect the database... In stead we found that declaring a Metaclass, with a table declaration, detailing PK, and autoload works. An example: class Customers(Base): __table__ = Table('Customers', Base.metadata, Column('UniqueNo', Integer, primary_key=True), schema =

Skipping all unit tests but one in Python by using decorators and metaclasses

徘徊边缘 提交于 2019-12-22 18:05:47
问题 I am writing unit tests for an MCU that communicates commands through the USB port and checks their response. If one unit test fails it makes sense for me to do some debugging in the MCU. Therefore I would like to disable all unittests except for the one that I would like to debug on the MCU side because if I set a breakpoint somewhere it might get triggered by another unittest with different commands. I went to the python docs and found this code which is a decorator that will skip all

Does the default type.__call__ do more than call __new__ and __init__?

萝らか妹 提交于 2019-12-22 08:18:12
问题 I'm writing a metaclass, and I want an additional method to be called between __new__ and __init__. If I were calling the method before __new__ or after __init__ I could write e.g. class Meta(type): def __call__(cls): ret = type.__call__() ret.extraMethod() My temptation is to write class Meta(type): def __call__(cls): ret = cls.__new__(cls) ret.extraMethod() ret.__init__() return ret and just reproduce the functionality of type.__call__ myself. But I'm afraid there might be some subtlety to

TypeError: object.__new__(int) is not safe, use int.__new__()

核能气质少年 提交于 2019-12-22 08:17:05
问题 While reading this: What is a metaclass in Python?, I am learning to use __new__ using the following snippet:- class a(object): pass a.__new__(int,'abcdef',(int,),{}) There could be some problem in calling __new__ using a. . But, I get the following error, the meaning of which I do not understand:- TypeError: object.__new__(int) is not safe, use int.__new__() If there is something to do with the usage of __new__ , I can ammend that by reading some books. But, can someone please explain why

Why doesn't the namedtuple module use a metaclass to create nt class objects?

不想你离开。 提交于 2019-12-22 01:34:22
问题 I spent some time investigating the collections.namedtuple module a few weeks ago. The module uses a factory function which populates the dynamic data (the name of the new namedtuple class, and the class attribute names) into a very large string. Then exec is executed with the string (which represents the code) as the argument, and the new class is returned. Does anyone know why it was done this way, when there is a specific tool for this kind of thing readily available, i.e. the metaclass? I

Dynamically creating Django models with `type`

倾然丶 夕夏残阳落幕 提交于 2019-12-21 19:11:12
问题 I have 20+ MySQL tables, prm_a , prm_b , ... with the same basic structure but different names, and I'd like to associate them with Django model classes without writing each one by hand. So, feeling ambitious, I thought I'd try my hand at using type() as a class-factory: The following works: def get_model_meta_class(prm_name): class Meta: app_label = 'myapp' setattr(Meta, 'db_table', 'prm_%s' % prm_name) return Meta prm_class_attrs = { 'foo': models.ForeignKey(Foo), 'val': models.FloatField()

Is it possible to properly copy a class using type

被刻印的时光 ゝ 提交于 2019-12-21 17:15:47
问题 According to this answer, a class object cls can be replicated with cls_copy = type('cls_copy', cls.__bases__, dict(cls.__dict__)) This works perfectly for most normal cases. It does not work when the metaclass of cls is not type . My initial naive fix was to do cls_copy = type(cls)('cls_copy', cls.__bases__, dict(cls.__dict__)) However, this is simply pointless. There is no way to know what a metaclass does, as this answer to a related question points out, how it transforms the input

Relationship of metaclass's “__call__” and instance's “__init__”?

让人想犯罪 __ 提交于 2019-12-21 04:42:08
问题 Say I've got a metaclass and a class using it: class Meta(type): def __call__(cls, *args): print "Meta: __call__ with", args class ProductClass(object): __metaclass__ = Meta def __init__(self, *args): print "ProductClass: __init__ with", args p = ProductClass(1) Output as follows: Meta: __call__ with (1,) Question: Why isn't ProductClass.__init__ triggered...just because of Meta.__call__ ? UPDATE: Now, I add __new__ for ProductClass: class ProductClass(object): __metaclass__ = Meta def __new_