Alexander Hultnér
Founder, Hultnér Technologies (https://hultner.se). Want me to speak at your company? Corporate training? Or just a fresh pair of eyes on your project? Contact me for contracts.
By Alexander Hultnér
Cetrez is a digital innovation company.
We exist to accelerate our clients’ advantage in their digital journey.
pip install dataclasses
Event host
@dataclass
class Host:
name: str # Required
nickname: Optional[str] = None # Optional
@dataclass
class Event:
title: str # Required field
# Factory default, run when created
hosts: List[Host] = field(default_factory=list)
# Default initiated at startup
day: date = date.today()
# Executes after init
def __post_init__(self):
self.hosts = [Host(**hoster) for hoster in self.hosts]
class EventConventionalVerbose:
def __init__(
self, title: str, hosts: Host = None, day: date = None
):
self.title = title
# Can't set mutable objects as default argument
if hosts is None:
self.hosts = []
else:
self.hosts = [Host(**hoster) for hoster in hosts]
if day is None:
self.day = date.today()
else:
self.day = day
def __repr__(self):
return f"EventConventionalVerbose"
f"(title={self.title!r}, hosts={self.hosts!r},"
f" day={self.day!r})"
def __eq__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) == (
other.title, other.hosts, other.day
)
return NotImplemented
def __ne__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) != (
other.title, other.hosts, other.day
)
return NotImplemented
def __lt__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) < (
other.title, other.hosts, other.day
)
return NotImplemented
def __le__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) <= (
other.title, other.hosts, other.day
)
return NotImplemented
def __gt__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) > (
other.title, other.hosts, other.day
)
return NotImplemented
def __ge__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) >= (
other.title, other.hosts, other.day
)
return NotImplemented
@dataclass
class Event:
title: str # Required field
# Mutable objects requires a factory,
# otherwise they'll share state just
# like conventional Python classes.
# A suggestion to change this was rejected.
hosts: List[Host] = field(default_factory=list)
# Initiated once at startup, in reality you'd probably
# want a factory here as well.
day: date = date.today()
# Executes after init,
# good for *validation* and *post processing*
def __post_init__(self):
# Transform host dict/mappings to instances of Host
self.hosts = [Host(**hoster) for hoster in self.hosts]
class EventConventionalVerbose:
def __init__(
self, title: str, hosts: Host = None, day: date = None
):
self.title = title
# Can't set mutable objects as default argument
if hosts is None:
self.hosts = []
else:
self.hosts = [Host(**hoster) for hoster in hosts]
if day is None:
self.day = date.today()
else:
self.day = day
def __repr__(self):
return f"EventConventionalVerbose"
f"(title={self.title!r}, hosts={self.hosts!r},"
f" day={self.day!r})"
def __eq__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) == (
other.title, other.hosts, other.day
)
return NotImplemented
def __ne__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) != (
other.title, other.hosts, other.day
)
return NotImplemented
def __lt__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) < (
other.title, other.hosts, other.day
)
return NotImplemented
def __le__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) <= (
other.title, other.hosts, other.day
)
return NotImplemented
def __gt__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) > (
other.title, other.hosts, other.day
)
return NotImplemented
def __ge__(self, other):
if other.__class__ is self.__class__:
return (self.title, self.hosts, self.day) >= (
other.title, other.hosts, other.day
)
return NotImplemented
By Alexander Hultnér
GothPy Meetup, 2018-05-17 Talk about Dataclasses from Python 3.7 showcase of using the backport in Python 3.6
Founder, Hultnér Technologies (https://hultner.se). Want me to speak at your company? Corporate training? Or just a fresh pair of eyes on your project? Contact me for contracts.