sync with O'Reilly Atlas

This commit is contained in:
Luciano Ramalho
2021-07-07 23:45:54 -03:00
parent f0f160844d
commit 23e78eeb82
64 changed files with 2087 additions and 124 deletions

View File

@@ -15,22 +15,26 @@
'banana'
>>> clip('banana split', 12)
'banana split'
>>> clip('bananasplit', 5)
'bananasplit'
>>> clip('banana split', 8)
'banana'
"""
# tag::CLIP[]
def clip(text, max_len=80):
"""Return text clipped at the last space before or after max_len
"""
end = None
if len(text) > max_len:
space_before = text.rfind(' ', 0, max_len)
if space_before >= 0:
end = space_before
else:
space_after = text.rfind(' ', max_len)
if space_after >= 0:
end = space_after
if end is None: # no spaces were found
return text.rstrip()
"""Return text clipped at the last space before or after max_len"""
text = text.rstrip()
end = len(text)
if end <= max_len:
return text
space_before = text.rfind(' ', 0, max_len)
if space_before >= 0:
end = space_before
else:
space_after = text.find(' ', max_len)
if space_after >= 0:
end = space_after
return text[:end].rstrip()
# end::CLIP[]

View File

@@ -1,8 +1,8 @@
>>> from clip import clip
>>> from inspect import signature
>>> sig = signature(clip)
>>> sig # doctest: +ELLIPSIS
<inspect.Signature object at 0x...>
>>> sig
<Signature (text, max_len=80)>
>>> str(sig)
'(text, max_len=80)'
>>> for name, param in sig.parameters.items():

View File

@@ -28,12 +28,9 @@ def tag(name, *content, class_=None, **attrs):
"""Generate one or more HTML tags"""
if class_ is not None:
attrs['class'] = class_
if attrs:
attr_pairs = (f' {attr}="{value}"' for attr, value
in sorted(attrs.items()))
attr_str = ''.join(attr_pairs)
else:
attr_str = ''
attr_pairs = (f' {attr}="{value}"' for attr, value
in sorted(attrs.items()))
attr_str = ''.join(attr_pairs)
if content:
elements = (f'<{name}{attr_str}>{c}</{name}>'
for c in content)