abc

Abstract class for thread-related class without multiple inheritance

Abstract class for thread-related class without multiple inheritance Question: I want to create an abstract class for thread-related class. Class that want to enable threading must inherit threading.Thread class, however at the same time, class that want to enable the @abstractmethod must inherit abc.ABC class. Because multiple inheritance is a bad practice, can i achieve …

Total answers: 2

collections.abc.Iterable doesn't allows runtime structural checks according to Iterable API

collections.abc.Iterable doesn't allows runtime structural checks according to Iterable API Question: Existing Approaches to Structural Subtyping Abstract classes defined in collections.abc module are slightly more advanced since they implement a custom __subclasshook__() method that allows runtime structural checks without explicit registration: from collections.abc import Iterable class MyIterable: def __iter__(self): return [] assert isinstance(MyIterable(), Iterable) But …

Total answers: 2

Abstract classes with varying amounts of parameters

Abstract classes with varying amounts of parameters Question: I was wondering if its possible when creating an abstract class with abstract methods if its possible to allow the implementations of those methods in the derived classes to have different amounts of parameters for each function. I currently have for my abstract class from abc import …

Total answers: 2

Can't instantiate abstract class … with abstract methods

Can't instantiate abstract class with abstract methods Question: I’m working on a kind of lib, and I’m getting an error. Here is my code. Of course @abc.abstractmethod have to be uncommented Here are my tests Sorry couldn’t just copy and paste it I went on the basis that the code below works. test.py: import abc …

Total answers: 4

How do I check if a numpy dtype is integral?

How do I check if a numpy dtype is integral? Question: How do I check if a numpy dtype is integral? I tried: issubclass(np.int64, numbers.Integral) but it gives False. Update: it now gives True. Asked By: Neil G || Source Answers: Do you mean line 17? In [13]: import numpy as np A=np.array([1,2,3]) In [14]: …

Total answers: 6

Why can `__subclasshook__` be monkeypatched onto the metaclass but `__instancecheck__` cannot?

Why can `__subclasshook__` be monkeypatched onto the metaclass but `__instancecheck__` cannot? Question: Here is a toy example of trying to create a decorator that allows declaration of attribute names which should be required parts of “interface checking” along the standard __subclasshook__ and __instancecheck__ patterns. It seems to work as expected when I decorate the Foo …

Total answers: 1

Determine if a Python class is an Abstract Base Class or Concrete

Determine if a Python class is an Abstract Base Class or Concrete Question: My Python application contains many abstract classes and implementations. For example: import abc import datetime class MessageDisplay(object): __metaclass__ = abc.ABCMeta @abc.abstractproperty def display(self, message): pass class FriendlyMessageDisplay(MessageDisplay): def greet(self): hour = datetime.datetime.now().timetuple().tm_hour if hour < 7: raise Exception(“Cannot greet while asleep.”) elif …

Total answers: 3

Python – Testing an abstract base class

Python – Testing an abstract base class Question: I am looking for ways / best practices on testing methods defined in an abstract base class. One thing I can think of directly is performing the test on all concrete subclasses of the base class, but that seems excessive at some times. Consider this example: import …

Total answers: 7

Excluding abstractproperties from coverage reports

Excluding abstractproperties from coverage reports Question: I have an abstract base class along the lines of: class MyAbstractClass(object): __metaclass__ = ABCMeta @abstractproperty def myproperty(self): pass But when I run nosetests (which coverage) on my project, it complains that the property def line is untested. It can’t actually be tested (AFAIK) as instantiation of the abstract …

Total answers: 3