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

@@ -0,0 +1,9 @@
from dataclasses import dataclass, field
@dataclass
class ClubMember:
name: str
guests: list = field(default_factory=list)

View File

@@ -0,0 +1,6 @@
from dataclasses import dataclass, field
@dataclass
class ClubMember:
name: str
guests: list[str] = field(default_factory=list) # <1>

View File

@@ -0,0 +1,9 @@
from dataclasses import dataclass
# tag::CLUBMEMBER[]
@dataclass
class ClubMember:
name: str
guests: list = []
# end::CLUBMEMBER[]

View File

@@ -0,0 +1,23 @@
"""
``Coordinate``: simple class decorated with ``dataclass`` and a custom ``__str__``::
>>> moscow = Coordinate(55.756, 37.617)
>>> print(moscow)
55.8°N, 37.6°E
"""
# tag::COORDINATE[]
from dataclasses import dataclass
@dataclass(frozen=True)
class Coordinate:
lat: float
lon: float
def __str__(self):
ns = 'N' if self.lat >= 0 else 'S'
we = 'E' if self.lon >= 0 else 'W'
return f'{abs(self.lat):.1f}°{ns}, {abs(self.lon):.1f}°{we}'
# end::COORDINATE[]

View File

@@ -0,0 +1,50 @@
# tag::DOCTESTS[]
"""
``HackerClubMember`` objects accept an optional ``handle`` argument::
>>> anna = HackerClubMember('Anna Ravenscroft', handle='AnnaRaven')
>>> anna
HackerClubMember(name='Anna Ravenscroft', guests=[], handle='AnnaRaven')
If ``handle`` is ommitted, it's set to the first part of the member's name::
>>> leo = HackerClubMember('Leo Rochael')
>>> leo
HackerClubMember(name='Leo Rochael', guests=[], handle='Leo')
Members must have a unique handle. The following ``leo2`` will not be created,
because its ``handle`` would be 'Leo', which was taken by ``leo``::
>>> leo2 = HackerClubMember('Leo DaVinci')
Traceback (most recent call last):
...
ValueError: handle 'Leo' already exists.
To fix, ``leo2`` must be created with an explicit ``handle``::
>>> leo2 = HackerClubMember('Leo DaVinci', handle='Neo')
>>> leo2
HackerClubMember(name='Leo DaVinci', guests=[], handle='Neo')
"""
# end::DOCTESTS[]
# tag::HACKERCLUB[]
from dataclasses import dataclass
from club import ClubMember
@dataclass
class HackerClubMember(ClubMember): # <1>
all_handles = set() # <2>
handle: str = '' # <3>
def __post_init__(self):
cls = self.__class__ # <4>
if self.handle == '': # <5>
self.handle = self.name.split()[0]
if self.handle in cls.all_handles: # <6>
msg = f'handle {self.handle!r} already exists.'
raise ValueError(msg)
cls.all_handles.add(self.handle) # <7>
# end::HACKERCLUB[]

View File

@@ -0,0 +1,51 @@
# tag::DOCTESTS[]
"""
``HackerClubMember`` objects can be created with a ``name`` and an optional ``handle``::
>>> anna = HackerClubMember('Anna Ravenscroft', handle='AnnaRaven')
>>> anna
HackerClubMember(name='Anna Ravenscroft', guests=[], handle='AnnaRaven')
If ``handle`` is ommitted, it's set to the first part of the member's name::
>>> leo = HackerClubMember('Leo Rochael')
>>> leo
HackerClubMember(name='Leo Rochael', guests=[], handle='Leo')
Members must have a unique handle. This ``leo2`` will not be created,
because its ``handle`` would be 'Leo', which was taken by ``leo``::
>>> leo2 = HackerClubMember('Leo DaVinci')
Traceback (most recent call last):
...
ValueError: handle 'Leo' already exists.
To fix, ``leo2`` must be created with an explicit ``handle``::
>>> leo2 = HackerClubMember('Leo DaVinci', handle='Neo')
>>> leo2
HackerClubMember(name='Leo DaVinci', guests=[], handle='Neo')
"""
# end::DOCTESTS[]
# tag::HACKERCLUB[]
from dataclasses import dataclass
from typing import ClassVar
from club import ClubMember
@dataclass
class HackerClubMember(ClubMember):
all_handles: ClassVar[set[str]] = set()
handle: str = ''
def __post_init__(self):
cls = self.__class__
if self.handle == '':
self.handle = self.name.split()[0]
if self.handle in cls.all_handles:
msg = f'handle {self.handle!r} already exists.'
raise ValueError(msg)
cls.all_handles.add(self.handle)
# end::HACKERCLUB[]

View File

@@ -0,0 +1,87 @@
"""
Media resource description class with subset of the Dublin Core fields.
Default field values:
>>> r = Resource('0')
>>> r # doctest: +NORMALIZE_WHITESPACE
Resource(identifier='0', title='<untitled>', creators=[], date=None,
type=<ResourceType.BOOK: 1>, description='', language='', subjects=[])
A complete resource record:
# tag::DOCTEST[]
>>> description = 'Improving the design of existing code'
>>> book = Resource('978-0-13-475759-9', 'Refactoring, 2nd Edition',
... ['Martin Fowler', 'Kent Beck'], date(2018, 11, 19),
... ResourceType.BOOK, description, 'EN',
... ['computer programming', 'OOP'])
>>> book # doctest: +NORMALIZE_WHITESPACE
Resource(identifier='978-0-13-475759-9', title='Refactoring, 2nd Edition',
creators=['Martin Fowler', 'Kent Beck'], date=datetime.date(2018, 11, 19),
type=<ResourceType.BOOK: 1>, description='Improving the design of existing code',
language='EN', subjects=['computer programming', 'OOP'])
# end::DOCTEST[]
"""
# tag::DATACLASS[]
from dataclasses import dataclass, field
from typing import Optional
from enum import Enum, auto
from datetime import date
class ResourceType(Enum): # <1>
BOOK = auto()
EBOOK = auto()
VIDEO = auto()
@dataclass
class Resource:
"""Media resource description."""
identifier: str # <2>
title: str = '<untitled>' # <3>
creators: list[str] = field(default_factory=list)
date: Optional[date] = None # <4>
type: ResourceType = ResourceType.BOOK # <5>
description: str = ''
language: str = ''
subjects: list[str] = field(default_factory=list)
# end::DATACLASS[]
from typing import TypedDict
class ResourceDict(TypedDict):
identifier: str
title: str
creators: list[str]
date: Optional[date]
type: ResourceType
description: str
language: str
subjects: list[str]
if __name__ == '__main__':
r = Resource('0')
description = 'Improving the design of existing code'
book = Resource('978-0-13-475759-9', 'Refactoring, 2nd Edition',
['Martin Fowler', 'Kent Beck'], date(2018, 11, 19),
ResourceType.BOOK, description,
'EN', ['computer programming', 'OOP'])
print(book)
book_dict: ResourceDict = {
'identifier': '978-0-13-475759-9',
'title': 'Refactoring, 2nd Edition',
'creators': ['Martin Fowler', 'Kent Beck'],
'date': date(2018, 11, 19),
'type': ResourceType.BOOK,
'description': 'Improving the design of existing code',
'language': 'EN',
'subjects': ['computer programming', 'OOP']}
book2 = Resource(**book_dict)
print(book == book2)

View File

@@ -0,0 +1,111 @@
"""
Media resource description class with subset of the Dublin Core fields.
Default field values:
>>> r = Resource('0')
>>> r # doctest: +NORMALIZE_WHITESPACE
Resource(
identifier = '0',
title = '<untitled>',
creators = [],
date = None,
type = <ResourceType.BOOK: 1>,
description = '',
language = '',
subjects = [],
)
A complete resource record:
>>> description = 'Improving the design of existing code'
>>> book = Resource('978-0-13-475759-9', 'Refactoring, 2nd Edition',
... ['Martin Fowler', 'Kent Beck'], date(2018, 11, 19),
... ResourceType.BOOK, description, 'EN',
... ['computer programming', 'OOP'])
# tag::DOCTEST[]
>>> book # doctest: +NORMALIZE_WHITESPACE
Resource(
identifier = '978-0-13-475759-9',
title = 'Refactoring, 2nd Edition',
creators = ['Martin Fowler', 'Kent Beck'],
date = datetime.date(2018, 11, 19),
type = <ResourceType.BOOK: 1>,
description = 'Improving the design of existing code',
language = 'EN',
subjects = ['computer programming', 'OOP'],
)
# end::DOCTEST[]
"""
from dataclasses import dataclass, field, fields
from typing import Optional, TypedDict
from enum import Enum, auto
from datetime import date
class ResourceType(Enum):
BOOK = auto()
EBOOK = auto()
VIDEO = auto()
@dataclass
class Resource:
"""Media resource description."""
identifier: str
title: str = '<untitled>'
creators: list[str] = field(default_factory=list)
date: Optional[date] = None
type: ResourceType = ResourceType.BOOK
description: str = ''
language: str = ''
subjects: list[str] = field(default_factory=list)
# tag::REPR[]
def __repr__(self):
cls = self.__class__
cls_name = cls.__name__
indent = ' ' * 4
res = [f'{cls_name}('] # <1>
for f in fields(cls): # <2>
value = getattr(self, f.name) # <3>
res.append(f'{indent}{f.name} = {value!r},') # <4>
res.append(')') # <5>
return '\n'.join(res) # <6>
# end::REPR[]
class ResourceDict(TypedDict):
identifier: str
title: str
creators: list[str]
date: Optional[date]
type: ResourceType
description: str
language: str
subjects: list[str]
if __name__ == '__main__':
r = Resource('0')
description = 'Improving the design of existing code'
book = Resource('978-0-13-475759-9', 'Refactoring, 2nd Edition',
['Martin Fowler', 'Kent Beck'], date(2018, 11, 19),
ResourceType.BOOK, description,
'EN', ['computer programming', 'OOP'])
print(book)
book_dict: ResourceDict = {
'identifier': '978-0-13-475759-9',
'title': 'Refactoring, 2nd Edition',
'creators': ['Martin Fowler', 'Kent Beck'],
'date': date(2018, 11, 19),
'type': ResourceType.BOOK,
'description': 'Improving the design of existing code',
'language': 'EN',
'subjects': ['computer programming', 'OOP']}
book2 = Resource(**book_dict)
print(book == book2)