Advertisement
MushroomMaula

Summer Jam 2020 Code v2

Jul 8th, 2020
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.11 KB | None | 0 0
  1. """
  2. Use this file to write your solution for the Summer Code Jam 2020 Qualifier.
  3.  
  4. Important notes for submission:
  5.  
  6. - Do not change the names of the two classes included below. The test suite we
  7.  will use to test your submission relies on existence these two classes.
  8.  
  9. - You can leave the `ArticleField` class as-is if you do not wish to tackle the
  10.  advanced requirements.
  11.  
  12. - Do not include "debug"-code in your submission. This means that you should
  13.  remove all debug prints and other debug statements before you submit your
  14.  solution.
  15. """
  16. import datetime
  17. import typing
  18. from collections import defaultdict, Counter
  19. import re
  20.  
  21.  
  22. class ArticleField:
  23.     """The `ArticleField` class for the Advanced Requirements."""
  24.  
  25.     def __init__(self, field_type: typing.Type[typing.Any]):
  26.         self.field_type = field_type
  27.  
  28.     def __set_name__(self, owner, name):
  29.         self.name = name
  30.  
  31.     def __set__(self, instance, value: "ArticleField"):
  32.         if isinstance(value, self.field_type):
  33.             instance.__dict__[self.name] = value
  34.         else:
  35.             error_msg = "expected an instance of type '{type}' for attribute '{name}', got '{provided_type}' instead"
  36.             raise TypeError(
  37.                 error_msg.format(
  38.                     type=self.field_type.__name__,
  39.                     name=self.name,
  40.                     provided_type=type(value).__name__
  41.                 )
  42.             )
  43.  
  44.     def __get__(self, instance, owner):
  45.         return instance.__dict__[self.name]
  46.  
  47.  
  48. class Article:
  49.     """The `Article` class you need to write for the qualifier."""
  50.  
  51.     counter = 0
  52.  
  53.     def __init__(self, title: str, author: str, publication_date: datetime.datetime, content: str):
  54.         self.id = Article.counter
  55.         self.title = title
  56.         self.author = author
  57.         self.publication_date = publication_date
  58.         self._content = content
  59.         self.last_edited = None
  60.  
  61.         Article.counter += 1
  62.  
  63.     @property
  64.     def content(self):
  65.         return self._content
  66.  
  67.     @content.setter
  68.     def content(self, value):
  69.         self._content = value
  70.         self.last_edited = datetime.datetime.now()
  71.  
  72.     def short_introduction(self, n_characters):
  73.         if len(self) < n_characters:
  74.             n_characters = len(self)
  75.  
  76.         intro = []
  77.         for word in self.content.split():
  78.             if self.content.index(word) + len(word) < n_characters:
  79.                 intro.append(word)
  80.         return ' '.join(intro)
  81.  
  82.     def most_common_words(self, n_words):
  83.         pattern = re.compile(r'\w+')
  84.         words = pattern.findall(self.content.lower())
  85.         result = Counter(words)
  86.         return dict(result.most_common(n_words))
  87.  
  88.     def __repr__(self):
  89.         text = "<Article title=\"{title}\" author='{author}' publication_date='{date}'>"
  90.         return text.format(
  91.             title=self.title,
  92.             author=self.author,
  93.             date=self.publication_date.isoformat()
  94.         )
  95.  
  96.     def __len__(self):
  97.         return len(self.content)
  98.  
  99.     def __lt__(self, other: "Article"):
  100.         return self.publication_date < other.publication_date
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement