Mercurial > hg > config
annotate python/example/abstract.py @ 929:7c4be71a560b default tip
remove old aliases
| author | Jeff Hammel <k0scist@gmail.com> | 
|---|---|
| date | Mon, 20 Oct 2025 15:22:19 -0700 | 
| parents | 9149b35b8a2a | 
| children | 
| rev | line source | 
|---|---|
| 550 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 1 #!/usr/bin/env | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 2 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 3 """ | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 4 demo related to abstract classes | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 5 """ | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 6 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 7 ### roll our own to demo | 
| 188 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 8 | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 9 def abstractmethod(method): | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 10 line = method.func_code.co_firstlineno | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 11 filename = method.func_code.co_filename | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 12 def not_implemented(*args, **kwargs): | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 13 raise NotImplementedError('Abstract method %s at File "%s", line %s should be implemented by a concrete class' % (repr(method), filename, line)) | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 14 return not_implemented | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 15 | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 16 class AbstractBaseClass(object): | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 17 | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 18 @abstractmethod | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 19 def foo(self, arg): | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 20 """foo does such and such""" | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 21 | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 22 @abstractmethod | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 23 def bar(self): | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 24 """bar does something else""" | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 25 | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 26 class ConcreteClass(AbstractBaseClass): | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 27 | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 28 def foo(self, arg): | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 29 print 'hello' | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 30 | 
| 550 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 31 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 32 ### now use the abc module | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 33 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 34 import abc | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 35 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 36 class AbstractClass(object): | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 37 @abc.abstractmethod | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 38 def foo(self, arg): | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 39 """blah""" | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 40 @abc.abstractmethod | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 41 def bar(self): | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 42 """bar does nothing""" | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 43 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 44 class CementClass(AbstractBaseClass): | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 45 def foo(self, arg): | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 46 print 'goodbye' | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 47 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 48 class Picasso(object): | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 49 __metaclass__ = abc.ABCMeta | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 50 @abc.abstractmethod | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 51 def foo(self, arg): | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 52 """blah""" | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 53 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 54 | 
| 188 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 55 if __name__ == '__main__': | 
| 550 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 56 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 57 # home-rolled | 
| 188 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 58 c = ConcreteClass() | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 59 c.foo(1) | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 60 a = AbstractBaseClass() | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 61 try: | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 62 a.foo(1) | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 63 except NotImplementedError, e: | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 64 print e | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 65 try: | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 66 a.bar() | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 67 except NotImplementedError, e: | 
| 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 68 print e | 
| 189 
fe8befc5bdfc
add another print just for fun
 Jeff Hammel <jhammel@mozilla.com> parents: 
188diff
changeset | 69 c.foo(1) | 
| 550 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 70 try: | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 71 a.foo(1) | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 72 except NotImplementedError, e: | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 73 print e | 
| 188 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 74 | 
| 550 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 75 ### abc | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 76 print '\nIllustrate `abc` functionality' | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 77 a = AbstractClass() | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 78 e = None | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 79 try: | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 80 a.foo(1) | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 81 # you'd think this would raise an exception since` | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 82 # `AbstractClass.foo` is decorated with `abc.abstractmethod` | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 83 # But see http://docs.python.org/2/library/abc.html : | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 84 # "Using this decorator requires that the class's metaclass | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 85 # is ABCMeta or is derived from it." | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 86 except Exception, e: | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 87 pass | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 88 assert e is None # ! | 
| 188 
5b82653ccda3
add demonstration of abc error
 Jeff Hammel <jhammel@mozilla.com> parents: diff
changeset | 89 | 
| 550 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 90 # as an aside, another good reason not to use super: | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 91 # "Unlike Java abstract methods, these abstract methods may | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 92 # have an implementation. This implementation can be called via the super() | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 93 # mechanism from the class that overrides it. This could be useful as | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 94 # an end-point for a super-call in a framework that uses cooperative | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 95 # multiple-inheritance." | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 96 | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 97 a = Picasso() | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 98 e = None | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 99 try: | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 100 a.foo(1) | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 101 except Exception, e: | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 102 pass | 
| 
9149b35b8a2a
python/example/abstract.py
 Jeff Hammel <jhammel@mozilla.com> parents: 
525diff
changeset | 103 import pdb; pdb.set_trace() | 
