abc

Difference between from collections import Container and from collections.abc import Container

与世无争的帅哥 提交于 2019-12-24 11:49:12
问题 We can import Container in two ways: from collections import Container from collections.abc import Container help function for both Container returns the same documentation. help(collections.Container) : Help on class Container in module collections.abc: class Container(builtins.object) | Methods defined here: | | __contains__(self, x) | | ---------------------------------------------------------------------- | Class methods defined here: | | __subclasshook__(C) from abc.ABCMeta | Abstract

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

Linux文件操作高频使用命令

时间秒杀一切 提交于 2019-12-19 13:58:17
文章目录 0.新建操作: 1.查看操作 2.删除操作 3.复制操作 4.移动操作: 5.重命名操作: 6.解压压缩操作 7.上传文件工具 8.ln、file和touch命令 9.查找操作命令: 0.新建操作: mkdir abc #新建一个文件夹 touch abc.sh #新建一个文件 echo "abc" > test.txt #新建一个文件,并将abc写入。这里用到了重定向符 1.查看操作 查看目录: ll #显示目录文件详细信息 du -h 文件/目录 #查看大小 pwd #显示路径 查看文件内容: cat|head|tail命令 cat abc.txt #查看abc的内容 head -5 abc.txt #查看abc前5行内容。默认是10行 tail [ 选项 ] 文件名 各选项的含义如下: +num:从第num行以后开始显示 -num:从距文件尾num行处开始显示。如果省略num参数,系统默认值为10. -f: 循环读取,例如查看服务器日志时,可以实时观察 #filename 文件里的最尾部的内容显示在屏幕上,并且不断刷新。 tail -f filename #查看最后20行 tail -f filename ​ more命令: more命令一次显示一屏信息,若信息未显示完屏幕底部将出现“-More-(xx%)”。 此时按Space键,可显示下一屏内容; 按“回车”键

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

北城以北 提交于 2019-12-18 13:54:22
问题 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 hour < 12: self.display("Good morning!") elif hour < 18: self.display("Good afternoon!")

Using abc.ABCMeta in a way it is compatible both with Python 2.7 and Python 3.5

笑着哭i 提交于 2019-12-18 10:13:32
问题 I'd like to create a class which has abc.ABCMeta as a metaclass and is compatible both with Python 2.7 and Python 3.5. Until now, I only succeeded doing this either on 2.7 or on 3.5 - but never on both versions simultaneously. Could someone give me a hand? Python 2.7: import abc class SomeAbstractClass(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def do_something(self): pass Python 3.5: import abc class SomeAbstractClass(metaclass=abc.ABCMeta): @abc.abstractmethod def do_something

Why use Abstract Base Classes in Python?

情到浓时终转凉″ 提交于 2019-12-17 03:47:07
问题 Because I am used to the old ways of duck typing in Python, I fail to understand the need for ABC (abstract base classes). The help is good on how to use them. I tried to read the rationale in the PEP, but it went over my head. If I was looking for a mutable sequence container, I would check for __setitem__ , or more likely try to use it (EAFP). I haven't come across a real life use for the numbers module, which does use ABCs, but that is the closest I have to understanding. Can anyone

Why use Abstract Base Classes in Python?

末鹿安然 提交于 2019-12-17 03:46:54
问题 Because I am used to the old ways of duck typing in Python, I fail to understand the need for ABC (abstract base classes). The help is good on how to use them. I tried to read the rationale in the PEP, but it went over my head. If I was looking for a mutable sequence container, I would check for __setitem__ , or more likely try to use it (EAFP). I haven't come across a real life use for the numbers module, which does use ABCs, but that is the closest I have to understanding. Can anyone

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

Implementing Singleton as metaclass, but for abstract classes

雨燕双飞 提交于 2019-12-13 02:35:13
问题 I have an abstract class and I would like to implement Singleton pattern for all classes that inherit from my abstract class. I know that my code won't work because there will be metaclass attribute conflict. Any ideas how to solve this? from abc import ABCMeta, abstractmethod, abstractproperty class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls]

No error while instantiating abstract class, even though abstract method is not implemented

冷暖自知 提交于 2019-12-11 03:04:55
问题 I was trying out the below python code: from abc import ABCMeta, abstractmethod class Bar: __metaclass__ = ABCMeta @abstractmethod def foo(self): pass class Bar2(Bar): def foo2(self): print("Foo2") b = Bar() b2 = Bar2() I thought having @abstractmethod will ensure that my parent class will be abstract and the child class would also be abstract as it is not implementing the abstract method. But here, I get no error trying to instantiate both the classes. Can anyone explain why? 回答1: You must