abc

What's the difference between the mro method and the __mro__ attribute of a class?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 14:57:34
问题 I stumbled across this extra, no-underscores mro method when I was using __metaclass__ = abc.ABCMeta . It seems to be the same as __mro__ except that it returns a list instead of a tuple. Here's a random example (ideone snippet): import abc import copy class Life(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def reproduce(self): pass class Bacterium(Life): def reproduce(self): return copy.deepcopy(self) wiggly = Bacterium() print wiggly.__class__.__mro__ # (<class '__main__

 commons-codec 加密算法

佐手、 提交于 2019-12-09 23:28:33
commons-codec包可以从apache下载,最新版是1.3 不可逆算法 1.MD5 String str = "abc"; DigestUtils.md5Hex(str); 附.net生成MD5的方法,生成内容跟java一致: String str = "abc" ; FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5"); 2.SHA1 String str = "abc"; DigestUtils.shaHex(str); 附.net生成SHA1的方式,生成内容跟java一致: String str = "abc" ; FormsAuthentication.HashPasswordForStoringInConfigFile(str, "SHA1"); 可逆算法 常规加密解密算法:BASE64 加密 String str= "abc"; // abc为要加密的字符串 byte[] b = Base64.encodeBase64(str.getBytes(), true); System.out.println(new String(b)); 解密 String str = "YWJj"; // YWJj为要解密的字符串 byte[] b = Base64.decodeBase64(str

Actual difference in implementing/overriding using @abstractproperty and @abstractmethod

淺唱寂寞╮ 提交于 2019-12-07 06:04:37
问题 Consider an abstract base class with a function which you want each subsequent subclass to override. Using the abc module and ABCMeta; does decorating with @abstractproperty or @abstractmethod actually force the subclass/developer implementing to create the type of function specified by the decorator? From my experiments you can override an abstract property with a method and an abstract method with a property in the subclass. Is this notion incorrect? 回答1: The notion is correct; the ABCMeta

ABC for String?

别说谁变了你拦得住时间么 提交于 2019-12-07 05:15:07
问题 I recently discovered abstract base classes (ABCs) in collections and like their clear, systematic approach and Mixins. Now I also want to create customs strings (*), but I couldn't find an ABC for strings. There is UserString, but UserDict was discouraged!? Deriving from str itself would have no Mixins? How would you access the "data" part of the string in overridden methods? Somewhere I saw the suggestions to derive from Sequence and Hashable, but then I couldn't write if 'test' in my

Python abc module: Extending both an abstract base class and an exception-derived class leads to surprising behavior

放肆的年华 提交于 2019-12-07 01:46:43
问题 Extending both an abstract base class and a class derived from "object" works as you would expect: if you you haven't implemented all abstract methods and properties, you get an error. Strangely, replacing the object-derived class with an class that extends "Exception" allows you to create instances of classes which do not implement all the required abstract methods and properties. For example: import abc # The superclasses class myABC( object ): __metaclass__ = abc.ABCMeta @abc

re模块2

帅比萌擦擦* 提交于 2019-12-06 13:03:32
# 元字符+,*遇到?后就会变为贪婪匹配 print(re.findall('abc+?','abcccccc')) #['abc'] print(re.findall('abc*?','abcccccc')) #['ab'] print(re.findall('abc{1,4}','abcccabc')) #['abccc', 'abc'] print(re.findall('abc{1,4}?','abcccabc')) #['abcc', 'abc'] #--------------------------------------------------------------------- #元字符之字符集[] print(re.findall("[abc]","sdfgbahjkkk")) #若是字符集的前后没有字母,就会分别匹配a,b,c ['b', 'a'],否在就字符集里选择一个字母和外面的连接使用 print(re.findall("a[a-z]","fwefaefewaasfwe")) #['ae', 'aa'] print(re.findall("a[.*+]","a.b*c+")) #在字符集里的符号就是普通符号 ['a.'] print(re.findall("[^ab]","fgddab")) #['f', 'g', 'd', 'd'] print(re

python定义接口继承类invalid syntax解决办法

回眸只為那壹抹淺笑 提交于 2019-12-05 16:45:37
zxq547 python定义接口继承类invalid syntax解决办法 1 2 3 4 5 6 7 class s_all(metaclass = abc.ABCMeta): #python2.7用此方法定义接口继承 # __metaclass__ = abc.ABCMeta @abc .abstractmethod def read( self ): pass pyhton2.7会报错,此方法用于python3+pyhton2.7应用次方法定义 1 2 3 4 5 6 7 class s_all(): #python2.7用此方法定义接口继承 __metaclass__ = abc.ABCMeta @abc .abstractmethod def read( self ): pass    1 2 3 4 5 6 7 class s_all(metaclass = abc.ABCMeta): #python2.7用此方法定义接口继承 # __metaclass__ = abc.ABCMeta @abc .abstractmethod def read( self ): pass pyhton2.7会报错,此方法用于python3+pyhton2.7应用次方法定义 1 2 3 4 5 6 7 class s_all(): #python2.7用此方法定义接口继承 _

ABC for String?

狂风中的少年 提交于 2019-12-05 12:25:52
I recently discovered abstract base classes (ABCs) in collections and like their clear, systematic approach and Mixins. Now I also want to create customs strings (*), but I couldn't find an ABC for strings. There is UserString, but UserDict was discouraged!? Deriving from str itself would have no Mixins? How would you access the "data" part of the string in overridden methods? Somewhere I saw the suggestions to derive from Sequence and Hashable, but then I couldn't write if 'test' in my_string: ?! Which approach do you recommend? (*) The reasons are: - write strings that order in an internally

Python abc module: Extending both an abstract base class and an exception-derived class leads to surprising behavior

我怕爱的太早我们不能终老 提交于 2019-12-05 04:17:23
Extending both an abstract base class and a class derived from "object" works as you would expect: if you you haven't implemented all abstract methods and properties, you get an error. Strangely, replacing the object-derived class with an class that extends "Exception" allows you to create instances of classes which do not implement all the required abstract methods and properties. For example: import abc # The superclasses class myABC( object ): __metaclass__ = abc.ABCMeta @abc.abstractproperty def foo(self): pass class myCustomException( Exception ): pass class myObjectDerivedClass( object )

Why doesn't the abc.ABCMeta abstract instantiation check work on derivatives of `list` and `dict`?

帅比萌擦擦* 提交于 2019-12-05 04:01:12
问题 I have been experimenting a little with the abc module in python. A la >>> import abc In the normal case you expect your ABC class to not be instantiated if it contains an unimplemented abstractmethod . You know like as follows: >>> class MyClass(metaclass=abc.ABCMeta): ... @abc.abstractmethod ... def mymethod(self): ... return -1 ... >>> MyClass() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: Can't instantiate abstract class MyClass with abstract methods