Skip to content

Commit db7e765

Browse files
committed
Add source files
1 parent ebe9520 commit db7e765

File tree

1,387 files changed

+36510
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,387 files changed

+36510
-0
lines changed

abc/abc_abc_base.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
12+
13+
class PluginBase(abc.ABC):
14+
15+
@abc.abstractmethod
16+
def load(self, input):
17+
"""Retrieve data from the input source
18+
and return an object.
19+
"""
20+
21+
@abc.abstractmethod
22+
def save(self, output, data):
23+
"""Save the data object to the output."""
24+
25+
26+
class SubclassImplementation(PluginBase):
27+
28+
def load(self, input):
29+
return input.read()
30+
31+
def save(self, output, data):
32+
return output.write(data)
33+
34+
35+
if __name__ == '__main__':
36+
print('Subclass:', issubclass(SubclassImplementation,
37+
PluginBase))
38+
print('Instance:', isinstance(SubclassImplementation(),
39+
PluginBase))

abc/abc_abstractproperty.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
12+
13+
class Base(abc.ABC):
14+
15+
@property
16+
@abc.abstractmethod
17+
def value(self):
18+
return 'Should never reach here'
19+
20+
@property
21+
@abc.abstractmethod
22+
def constant(self):
23+
return 'Should never reach here'
24+
25+
26+
class Implementation(Base):
27+
28+
@property
29+
def value(self):
30+
return 'concrete property'
31+
32+
constant = 'set by a class attribute'
33+
34+
35+
try:
36+
b = Base()
37+
print('Base.value:', b.value)
38+
except Exception as err:
39+
print('ERROR:', str(err))
40+
41+
i = Implementation()
42+
print('Implementation.value :', i.value)
43+
print('Implementation.constant:', i.constant)

abc/abc_abstractproperty_rw.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
12+
13+
class Base(abc.ABC):
14+
15+
@property
16+
@abc.abstractmethod
17+
def value(self):
18+
return 'Should never reach here'
19+
20+
@value.setter
21+
@abc.abstractmethod
22+
def value(self, new_value):
23+
return
24+
25+
26+
class PartialImplementation(Base):
27+
28+
@property
29+
def value(self):
30+
return 'Read-only'
31+
32+
33+
class Implementation(Base):
34+
35+
_value = 'Default value'
36+
37+
@property
38+
def value(self):
39+
return self._value
40+
41+
@value.setter
42+
def value(self, new_value):
43+
self._value = new_value
44+
45+
46+
try:
47+
b = Base()
48+
print('Base.value:', b.value)
49+
except Exception as err:
50+
print('ERROR:', str(err))
51+
52+
p = PartialImplementation()
53+
print('PartialImplementation.value:', p.value)
54+
55+
try:
56+
p.value = 'Alteration'
57+
print('PartialImplementation.value:', p.value)
58+
except Exception as err:
59+
print('ERROR:', str(err))
60+
61+
i = Implementation()
62+
print('Implementation.value:', i.value)
63+
64+
i.value = 'New value'
65+
print('Changed value:', i.value)

abc/abc_base.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
12+
13+
class PluginBase(metaclass=abc.ABCMeta):
14+
15+
@abc.abstractmethod
16+
def load(self, input):
17+
"""Retrieve data from the input source
18+
and return an object.
19+
"""
20+
21+
@abc.abstractmethod
22+
def save(self, output, data):
23+
"""Save the data object to the output."""

abc/abc_class_static.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
12+
13+
class Base(abc.ABC):
14+
15+
@classmethod
16+
@abc.abstractmethod
17+
def factory(cls, *args):
18+
return cls()
19+
20+
@staticmethod
21+
@abc.abstractmethod
22+
def const_behavior():
23+
return 'Should never reach here'
24+
25+
26+
class Implementation(Base):
27+
28+
def do_something(self):
29+
pass
30+
31+
@classmethod
32+
def factory(cls, *args):
33+
obj = cls(*args)
34+
obj.do_something()
35+
return obj
36+
37+
@staticmethod
38+
def const_behavior():
39+
return 'Static behavior differs'
40+
41+
42+
try:
43+
o = Base.factory()
44+
print('Base.value:', o.const_behavior())
45+
except Exception as err:
46+
print('ERROR:', str(err))
47+
48+
i = Implementation.factory()
49+
print('Implementation.const_behavior :', i.const_behavior())

abc/abc_concrete_method.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
import io
12+
13+
14+
class ABCWithConcreteImplementation(abc.ABC):
15+
16+
@abc.abstractmethod
17+
def retrieve_values(self, input):
18+
print('base class reading data')
19+
return input.read()
20+
21+
22+
class ConcreteOverride(ABCWithConcreteImplementation):
23+
24+
def retrieve_values(self, input):
25+
base_data = super(ConcreteOverride,
26+
self).retrieve_values(input)
27+
print('subclass sorting data')
28+
response = sorted(base_data.splitlines())
29+
return response
30+
31+
32+
input = io.StringIO("""line one
33+
line two
34+
line three
35+
""")
36+
37+
reader = ConcreteOverride()
38+
print(reader.retrieve_values(input))
39+
print()

abc/abc_find_subclasses.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
from abc_base import PluginBase
12+
import abc_subclass
13+
import abc_register
14+
15+
for sc in PluginBase.__subclasses__():
16+
print(sc.__name__)

abc/abc_incomplete.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
from abc_base import PluginBase
12+
13+
14+
@PluginBase.register
15+
class IncompleteImplementation(PluginBase):
16+
17+
def save(self, output, data):
18+
return output.write(data)
19+
20+
21+
if __name__ == '__main__':
22+
print('Subclass:', issubclass(IncompleteImplementation,
23+
PluginBase))
24+
print('Instance:', isinstance(IncompleteImplementation(),
25+
PluginBase))

abc/abc_register.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
from abc_base import PluginBase
12+
13+
14+
class LocalBaseClass:
15+
pass
16+
17+
18+
@PluginBase.register
19+
class RegisteredImplementation(LocalBaseClass):
20+
21+
def load(self, input):
22+
return input.read()
23+
24+
def save(self, output, data):
25+
return output.write(data)
26+
27+
28+
if __name__ == '__main__':
29+
print('Subclass:', issubclass(RegisteredImplementation,
30+
PluginBase))
31+
print('Instance:', isinstance(RegisteredImplementation(),
32+
PluginBase))

abc/abc_subclass.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2009 Doug Hellmann All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import abc
11+
from abc_base import PluginBase
12+
13+
14+
class SubclassImplementation(PluginBase):
15+
16+
def load(self, input):
17+
return input.read()
18+
19+
def save(self, output, data):
20+
return output.write(data)
21+
22+
23+
if __name__ == '__main__':
24+
print('Subclass:', issubclass(SubclassImplementation,
25+
PluginBase))
26+
print('Instance:', isinstance(SubclassImplementation(),
27+
PluginBase))

argparse/argparse_FileType.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# encoding: utf-8
3+
#
4+
# Copyright (c) 2010 Doug Hellmann. All rights reserved.
5+
#
6+
"""
7+
"""
8+
9+
#end_pymotw_header
10+
import argparse
11+
12+
parser = argparse.ArgumentParser()
13+
14+
parser.add_argument('-i', metavar='in-file',
15+
type=argparse.FileType('rt'))
16+
parser.add_argument('-o', metavar='out-file',
17+
type=argparse.FileType('wt'))
18+
19+
try:
20+
results = parser.parse_args()
21+
print('Input file:', results.i)
22+
print('Output file:', results.o)
23+
except IOError as msg:
24+
parser.error(str(msg))

0 commit comments

Comments
 (0)