-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharticle.py
32 lines (26 loc) · 1007 Bytes
/
article.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import re
import json
class Article:
def __init__(self, title: str, subtitle: str, date: str, text: str):
# Initializing instance of article.
self._title = title
self._subtitle = subtitle
self._date = date
self._text = text
def __str__(self):
# To String method to print nicely to file
out = '{title} - {subtitle} ({date}) \n {text}'
return out.format(title=self._title, subtitle=self._subtitle, date=self._date, text=self._text)
def to_dict(self):
# To dict method to dump in json file
article = {'title': self._title,
'subtitle': self._subtitle,
'date': self._date,
'text': self._text
}
return article
def load_from_json(path: str):
with open(path, 'r') as article_file:
article = json.load(article_file)
art = Article(article['title'], article['subtitle'], article['date'], article['text'])
return art