renumbering chapters >= 19
This commit is contained in:
41
24-class-metaprog/metabunch/pre3.6/bunch.py
Normal file
41
24-class-metaprog/metabunch/pre3.6/bunch.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import collections
|
||||
import warnings
|
||||
|
||||
class MetaBunch(type):
|
||||
def __prepare__(name, *bases, **kwargs):
|
||||
return collections.OrderedDict()
|
||||
|
||||
def __new__(meta_cls, cls_name, bases, cls_dict):
|
||||
def __init__(self, **kw):
|
||||
for k in self.__defaults__:
|
||||
setattr(self, k, self.__defaults__[k])
|
||||
for k in kw:
|
||||
setattr(self, k, kw[k])
|
||||
|
||||
def __repr__(self):
|
||||
rep = ['{}={!r}'.format(k, getattr(self, k))
|
||||
for k in self.__defaults__
|
||||
if getattr(self, k) != self.__defaults__[k]
|
||||
]
|
||||
return '{}({})'.format(cls_name, ', '.join(rep))
|
||||
|
||||
new_dict = { '__slots__':[],
|
||||
'__defaults__':collections.OrderedDict(),
|
||||
'__init__':__init__, '__repr__':__repr__, }
|
||||
|
||||
for k in cls_dict:
|
||||
if k.startswith('__') and k.endswith('__'):
|
||||
if k in new_dict:
|
||||
warnings.warn(
|
||||
"Can't set attr {!r} in bunch-class {!r}".
|
||||
format(k, cls_name))
|
||||
else:
|
||||
new_dict[k] = cls_dict[k]
|
||||
else:
|
||||
new_dict['__slots__'].append(k)
|
||||
new_dict['__defaults__'][k] = cls_dict[k]
|
||||
|
||||
return super().__new__(meta_cls, cls_name, bases, new_dict)
|
||||
|
||||
class Bunch(metaclass=MetaBunch):
|
||||
pass
|
||||
38
24-class-metaprog/metabunch/pre3.6/bunch_test.py
Normal file
38
24-class-metaprog/metabunch/pre3.6/bunch_test.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import pytest
|
||||
|
||||
from bunch import Bunch
|
||||
|
||||
class Point(Bunch):
|
||||
""" A point has x and y coordinates, defaulting to 0.0,
|
||||
and a color, defaulting to 'gray'—and nothing more,
|
||||
except what Python and the metaclass conspire to add,
|
||||
such as __init__ and __repr__
|
||||
"""
|
||||
x = 0.0
|
||||
y = 0.0
|
||||
color = 'gray'
|
||||
|
||||
|
||||
def test_init_defaults():
|
||||
p = Point()
|
||||
assert repr(p) == 'Point()'
|
||||
|
||||
|
||||
def test_init():
|
||||
p = Point(x=1.2, y=3.4, color='red')
|
||||
assert repr(p) == "Point(x=1.2, y=3.4, color='red')"
|
||||
|
||||
|
||||
def test_init_wrong_argument():
|
||||
with pytest.raises(AttributeError) as exc:
|
||||
p = Point(x=1.2, y=3.4, flavor='coffee')
|
||||
assert "no attribute 'flavor'" in str(exc.value)
|
||||
|
||||
|
||||
def test_slots():
|
||||
p = Point()
|
||||
with pytest.raises(AttributeError) as exc:
|
||||
p.z = 5.6
|
||||
assert "no attribute 'z'" in str(exc.value)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user