inherit assertion : why assertion is not working while inheriting the class - assert

class a:
def init(self,a:int):
assert a>0 ,"a is less than zero"
class b(a):
def init(self,a):
self.a=a
a.init(self,a)
m=b(-8)
Blockquote

Related

DRF: how to call a serializer (as a nested serializer field) before it is defined?

class ASerializer(serializers.ModelSerializer):
b = Bserializer()
...
class BSerializer(serializers.ModelSerializer):
...
I need to call BSerializer before it is defined. How do I do that?
class ASerializer(serializers.ModelSerializer):
def __init__(self, instance=None, **kwargs):
super().__init__(instance, **kwargs)
self.fields['b'] = BSerializer()
class BSerializer(serializers.ModelSerializer):
...

Generic dispatcher for multiple classes

I am trying to achieve the same effect in C++. In Python it is so simple to just pass a class method pointer to a dispatcher and to execute the function callback when the specific event occurs. The effect I am looking for is a true generic dispatch class method. Also, the classes should not be related in any way (like in the example below).
class Dispatcher:
def __init__(self):
self.map = {}
def register(self, func, event):
try:
self.map[event].append(func)
except KeyError:
self.map[event] = [func]
def dispatch(self, event):
for func in self.map[event]:
func(event)
class A:
def __init__(self):
pass
def foo(self, event):
print("A", event)
class B:
def __init__(self):
pass
def bar(self, event):
print("B", event)
if __name__ == '__main__':
d = Dispatcher()
a = A()
b = B()
d.register(a.foo, 6)
d.register(b.bar, 6)
d.register(a.foo, 7)
d.dispatch(6)
Expected output:
A 6
B 6
#include <map>
#include <vector>
#include <functional>
using namespace std;
class dispatcher
{
public:
using event_t = int;
using callback_t = function<void(event_t)>;
void register(callback_t c, event_t e)
{
m[e].emplace_back(move(c));
}
void dispatch(event_t e)
{
for(auto&&c: m[e])
c(e);
}
private:
map<event_t, vector<callback_t>> m;
};
You can do something like this. I wrote this in one go on mobile so I have no idea if it works.
That said, this won’t really work exactly the same way as you cannot pass functions that automatically capture the thisptr of an object. You’ll use it as follows, with a functor:
A a;
d.register([&](int i){a.foo(i);}, 6);

How to mock a method call which is being called using setattr during class instantiation?

I have the following class:
class ClassA(Object):
VERSIONS = {
'1': {
'x': a.b.x.X # where x is the module and X is the class
}
}
ATTRS = ['y', 'z']
def __init__(self, **kwargs):
...
do_something...
...
for attr in ATTRS:
setattr(self, attr, VERSIONS[ver][attr]())
I am using the above code as follows:
class_a = ClassA()
class_a.x.y
where x is a module and y is the method in that module
I need to mock this call now. How do I mock it, since the method y is not part of ClassA?
This can be done as follows:
mock_class_a.return_value.x.y = value_to_return

Inheritance in SWIG Directors

When I create a SWIG Director for a class A inheriting from class B it fails when the C++ library tries to call methods of class B.
libc++abi.dylib: terminating with uncaught exception of type Swig::DirectorMethodException: SWIG director method error. Error detected when calling 'B.MethodB'
Is there any limitation on SWIG Directors support for classes with multiple levels of inheritance?
UPDATE: The actual code below
class RefCountInterface {
public:
virtual int AddRef() = 0;
virtual int Release() = 0;
protected:
virtual ~RefCountInterface() {}
};
class SessionObserver : public RefCountInterface {
public:
virtual void OnSuccess() = 0;
virtual void OnFailure() = 0;
protected:
~SessionObserver() {}
};
void CreateOffer(SessionObserver* observer); // This ends up calling observer->Release and that raises the exception
%module(directors="1") module
%feature("director") SessionObserver;
Python code:
class MySessionObserver(module.SessionObserver):
def __init__(self, *args, **kwargs):
module.SessionObserver.__init__(self, *args, **kwargs)
self.count = 1
def AddRef(self):
self.count += 1
return self.count
def Release(self):
self.count -= 1
return self.count
def OnSuccess(self, desc):
print "OnSuccess"
def OnFailure(self, err):
print "OnFailure"

Django - ManyToOne relation to a child class

Is there a way to declare this case so that it works? I hope the code is self-explanatory.
class A(Model):
many_to_one = models.ForeignKey(B)
(...)
class B(A):
(...)
class A(Model):
many_to_one = models.ForeignKey('B')
(...)
class B(A):
(...)