Skip to content

Instantly share code, notes, and snippets.

@purefunctor
Created July 23, 2020 00:35
Show Gist options
  • Save purefunctor/05676a934cf33dee9e82969870dd3d9b to your computer and use it in GitHub Desktop.
Save purefunctor/05676a934cf33dee9e82969870dd3d9b to your computer and use it in GitHub Desktop.
"""
Use this file to write your solution for the Summer Code Jam 2020 Qualifier.
Important notes for submission:
- Do not change the names of the two classes included below. The test suite we
will use to test your submission relies on existence these two classes.
- You can leave the `ArticleField` class as-is if you do not wish to tackle the
advanced requirements.
- Do not include "debug"-code in your submission. This means that you should
remove all debug prints and other debug statements before you submit your
solution.
"""
import collections
import datetime
import typing
import re
import textwrap
import weakref
class ArticleField:
"""The `ArticleField` class for the Advanced Requirements."""
def __init__(self, field_type: typing.Type[typing.Any]):
self.field_type = field_type
self.instances = weakref.WeakKeyDictionary()
self.name = None
def __get__(self, instance, owner=None):
if instance is None:
return self
try:
return self.instances[instance]
except KeyError:
raise AttributeError(f"article field '{self.name}' is not set") from None
def __set__(self, instance, value):
msg = " ".join((
f"expected an instance of type '{self.field_type.__qualname__}'",
f"for attribute '{self.name}', got '{type(value).__qualname__}'",
"instead.",
))
if not isinstance(value, self.field_type):
raise TypeError(msg)
self.instances[instance] = value
def __set_name__(self, owner, name):
self.name = name
class Article:
"""The `Article` class you need to write for the qualifier."""
current_article_idx = 0
def __init__(self, title: str, author: str, publication_date: datetime.datetime, content: str):
self.title = title
self.author = author
self.publication_date = publication_date
self.id = self._new_article_id()
self._content = content
self.last_edited = None
@classmethod
def _new_article_id(cls):
article_id = cls.current_article_idx
cls.current_article_idx += 1
return article_id
@property
def content(self):
return self._content
@content.setter
def content(self, content):
self._content = content
self.last_edited = datetime.datetime.now()
def __repr__(self):
title = self.title
author = self.author
publication_date = self.publication_date.isoformat()
return f"<Article {title=} {author=} {publication_date=}>"
def __len__(self):
return len(self.content)
def __lt__(self, other):
return self.publication_date < other.publication_date
def __gt__(self, other):
return self.publication_date > other.publication_date
def short_introduction(self, n_characters: int):
return textwrap.shorten(self.content, n_characters, placeholder="")
def most_common_words(self, n_words: int):
word_list = re.findall("\w+", self.content.lower())
return dict(collections.Counter(word_list).most_common(n_words))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment