metaclass

Custom metaclass to create hybrid properties in SQLAlchemy

青春壹個敷衍的年華 提交于 2019-12-06 11:05:56
问题 I want to create a custom interface on top of SQLAlchemy so that some pre-defined hybrid properties are supported transparently. Specifically, I want to create a class SpecialColumn and a metaclass so that when a user adds SpecialColumn as an attribute of a class, my custom metaclass replaces that attribute with two SQLAlchemy Column s and adds a hybrid property that gets and sets those two columns as a tuple. Here's my approach so far: First, I defined my special column type: class

In Python, decorate two methods with the same name to distinguish them

风流意气都作罢 提交于 2019-12-06 06:08:48
I am writing a framework to be used by people who know some python. I have settled on some syntax, and it makes sense to me and them to use something like this, where Base is the Base class that implements the framework. class A(Base): @decorator1 @decorator2 @decorator3 def f(self): pass @decorator4 def f(self): pass @decorator5 def g(self) pass All my framework is implemented via Base's metaclass. This set up is appropriate for my use case because all these user-defined classes have a rich inheritance graph. I expect the user to implement some of the methods, or just leave it with pass .

unittest and metaclass: automatic test_* method generation

淺唱寂寞╮ 提交于 2019-12-06 05:13:39
问题 As I create tests for a framework, I start noticing the following pattern: class SomeTestCase(unittest.TestCase): def test_feat_true(self): _test_feat(self, True) def test_feat_false(self): _test_feat(self, False) def _test_feat(self, arg): pass # test logic goes here So I want to programmatically create test_feat_* methods for these type of test classes with a metaclass. In other words, for each private method with signature _test_{featname}(self, arg) , I want two top-level, discoverable

Why isn't __instancecheck__ being called?

这一生的挚爱 提交于 2019-12-06 03:37:20
问题 I have the following python3 code: class BaseTypeClass(type): def __new__(cls, name, bases, namespace, **kwd): result = type.__new__(cls, name, bases, namespace) print("creating class '{}'".format(name)) return result def __instancecheck__(self, other): print("doing instance check") print(self) print(other) return False class A(metaclass=BaseTypeClass): pass print(type(A)) print(isinstance(A(), A)) and when I run it on Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit

Python inheritable functions

吃可爱长大的小学妹 提交于 2019-12-06 02:42:36
Is there a way in Python to either ``type'' functions or for functions to inherit test suites? I am doing some work evaluating several different implementations of different functions with various criteria (for example I may evaluate different sort functions based on speed for the array size and memory requirements). And I want to be able to automate the testing of the functions. So I would like a way to identify a function as being an implementation of a certain operator so that the test suite can just grab all functions that are implementation of that operator and run them through the tests.

The built-in keyword type means a function or a class in python?

点点圈 提交于 2019-12-06 00:45:50
In most posts, people often say type is a built-in function if it is provided with one argument, and it is a metaclass if provided with 3 arguments. But in python's doc , the signature of type is: class type(object) class type(name, bases, dict) So, does it means type is a built-in class and not a built-in function even if it is provided with one argument? type is called a "metaclass" because it's a class that produces other classes (AKA types). It behaves like a normal class. In particular, it has the equivalent of a __new__ method that would look something like this in Python: class type

Can I cast a metaclass object to a protocol type in Swift?

对着背影说爱祢 提交于 2019-12-05 20:38:17
问题 Swift inherited Objective-C's metaclass concept: classes themselves are also considered objects. A class Foo 's object's class is Foo.self , and it is of type Foo.Type . If Foo inherits from Bar , then Foo.self can be assigned to a variable of type Bar.Type , too. This has at least two benefits: it allows to override "static methods"; it's easy to create an instance of an unknown class in a type-safe way and without using reflection. I'm particularly looking at the second one right now. Just

Add method to metaclass

戏子无情 提交于 2019-12-05 20:20:18
问题 I'm just playing with the metaclass programming in Groovy. But suddenly I was facing a little problem which I just could not get working... Here is the simple script: // define simple closure def printValueClosure = { println "The value is: '$delegate'" } String.metaClass.printValueClosure = printValueClosure // works fine 'variable A'.printValueClosure() // define as method def printValueMethod(String s){ println "The value is: '$s'" } // how to do this!? String.metaClass.printValueMethod =

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

可紊 提交于 2019-12-05 17:53:36
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 type.__call__ I have omitted, which will lead to unexpected behavior when my metaclass is implemented.

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

烈酒焚心 提交于 2019-12-05 15:41:29
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 this message comes: object.__new__(int) is not safe, use int.__new__() Jon Clements Put simply, prior to