Skip to content

Instantly share code, notes, and snippets.

@scorphus
Created December 19, 2017 16:16
Show Gist options
  • Select an option

  • Save scorphus/9b83ba094ad6ec0212adb7d48d546ef3 to your computer and use it in GitHub Desktop.

Select an option

Save scorphus/9b83ba094ad6ec0212adb7d48d546ef3 to your computer and use it in GitHub Desktop.
Experimentations with Data Classes (PEP 557 – https://www.python.org/dev/peps/pep-0557/)
from dataclasses import dataclass
from itertools import groupby
from operator import countOf
from typing import Callable, Dict, Iterable, List, Tuple
@dataclass
class Document:
url: str
title: str
thumbnail: str
caption: str
description: str
issued: str
tenant_id: str
publisher: str
live_now: bool
is_live: bool
def __post_init__(self) -> None:
self.id = self.url
def live_doc(doc: Document) -> bool:
return doc.live_now or doc.is_live
class Roster:
def __init__(self) -> None:
self.docs_list: List[Document] = []
self.docs_dict: Dict[str, Document] = {}
def add(self, doc: Document):
if doc.id in self.docs_dict:
raise ValueError(f'Duplicated doc {doc.id}')
self.docs_list.append(doc)
self.docs_dict[doc.id] = doc
return self
def _projection(
self, attr: str, filter: Callable[[Document], bool]
) -> Iterable[str]:
return (getattr(doc, attr) for doc in self.docs_list if filter(doc))
@property
def tenant_id(self) -> Iterable[Tuple[str, Iterable[str]]]:
return groupby(self._projection('tenant_id', live_doc))
@property
def count_tenant_id(self) -> Iterable[Tuple[str, int]]:
return [
(tenant_id, len(list(docs)))
for tenant_id, docs in self.tenant_id
]
@property
def num_docs(self) -> int:
return len(self.docs_list)
@property
def live_docs(self) -> int:
return countOf(list(map(live_doc, self.docs_list)), True)
def __repr__(self):
return (
f'Roster Numdocs {{{self.num_docs}}} '
f'Live docs {{{self.live_docs}: {self.count_tenant_id}}}.'
)
if __name__ == '__main__':
doc1 = Document(
'https://g1.globo.com/foo.ghtml',
'Sint laborum ex tempor incididunt',
'https://s2.glbimg.com/foo.jpg',
'Velit labore dolor amet',
'Occaecat ea duis mollit nisi et occaecat cupidatat ullamco. Nisi '
'ex elit. Magna eu duis magna occaecat officia ex officia et. '
'Deserunt fugiat quis nulla elit duis ex cupidatat qui aute. Et '
'cupidatat laboris voluptate ullamco.',
'2017-12-17T19:48:29Z',
'g1',
'G1',
True,
False,
)
print(doc1)
doc2 = Document(
'http://gshow.globo.com/bar.html',
'Eu id elit officia velit commodo',
'https://s2.glbimg.com/bar.jpg',
'Quis in culpa dolor quis',
'Esse adipisicing elit amet anim minim enim eiusmod laborum nisi '
'laborum exercitation. Labore adipisicing anim deserunt veniam id '
'minim fugiat voluptate. Eiusmod voluptate consequat esse anim '
'reprehenderit nostrud.',
'2017-12-17T19:48:29Z',
'gshow',
'Gshow',
False,
True,
)
print(doc2)
roster = Roster()
roster.add(doc1).add(doc2)
print(roster)
doc1.live_now = False
print(roster)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment