|
3 | 3 | Create a metaclass
|
4 | 4 | """
|
5 | 5 | import matplotlib.artist as martist
|
6 |
| -import matplotlib.lines as mlines |
7 | 6 |
|
8 | 7 | __all__ = []
|
9 | 8 |
|
@@ -46,67 +45,7 @@ def _gen_properties(cls):
|
46 | 45 | yield name, property(*args, doc=getter.__doc__)
|
47 | 46 |
|
48 | 47 |
|
49 |
| -# Approach #1: Apply properties to all artists |
50 | 48 | for cls in _gen_subclasses(martist.Artist):
|
51 | 49 | for name, prop in _gen_properties(cls):
|
52 | 50 | setattr(cls, name, prop)
|
53 | 51 | print(f'Added properties to {cls.__name__}.')
|
54 |
| - |
55 |
| - |
56 |
| -# Approach #2: Requires importing proplot before matplotlib. |
57 |
| -# This will not work for ProPlot but may be in matplotlib's future. |
58 |
| -class Propertifier(type): # create new class definitions |
59 |
| - """ |
60 |
| - Artist metaclass. |
61 |
| - """ |
62 |
| - def __init__(cls, *args, **kwargs): |
63 |
| - """ |
64 |
| - Add property getters and setters to all matplotlib artist subclasses. |
65 |
| - """ |
66 |
| - super().__init__(*args, **kwargs) |
67 |
| - for name, property in _gen_properties(cls): |
68 |
| - setattr(cls, name, property) |
69 |
| - print(f'Added properties to {cls.__name__}.') |
70 |
| - |
71 |
| - |
72 |
| -class Artist(martist.Artist, metaclass=Propertifier): |
73 |
| - """ |
74 |
| - New base class for all matplotlib artists that |
75 |
| - uses `Propertifier` as the metaclass. |
76 |
| - """ |
77 |
| - |
78 |
| - |
79 |
| -# Apply monkey patch |
80 |
| -import matplotlib.artist as martist |
81 |
| -martist.Artist = Artist |
82 |
| - |
83 |
| - |
84 |
| -# Testing metaclasses |
85 |
| -class MetaClass(type): |
86 |
| - def __init__(cls, *args, **kwargs): |
87 |
| - print('Brand new class!', cls) |
88 |
| - super().__init__(*args, **kwargs) |
89 |
| - |
90 |
| - |
91 |
| -A = MetaClass('A', (object,), {}) |
92 |
| -B = MetaClass('B', (A,), {}) |
93 |
| - |
94 |
| - |
95 |
| -# Testing Artist |
96 |
| -class Test(Artist): |
97 |
| - def set_foo(self, foo): |
98 |
| - print('set foo!', foo) |
99 |
| - |
100 |
| - def get_foo(self): |
101 |
| - print('get foo!') |
102 |
| - |
103 |
| - def get_bar(self): |
104 |
| - print('get bar!') |
105 |
| - |
106 |
| - |
107 |
| -class Line2D(Artist, mlines.Line2D): |
108 |
| - pass |
109 |
| - |
110 |
| - |
111 |
| -test = Test() |
112 |
| -line = Line2D([0], [0]) |
0 commit comments