renumbering chapters >= 19

This commit is contained in:
Luciano Ramalho
2021-09-10 12:34:39 -03:00
parent cbd13885fc
commit 4ae4096c4c
154 changed files with 7 additions and 1134 deletions

View 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

View 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)