-
Notifications
You must be signed in to change notification settings - Fork 0
/
poem.py
140 lines (116 loc) · 4.57 KB
/
poem.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from typing import Union
import pandas as pd
from .config import BASE_URL
from .glob import make_request, metadata
class Poem:
'''
Class corresponding to a particular poem.
'''
def __init__(
self,
lang : Union[None,str] = None,
base_url : str = BASE_URL,
id_ : Union[None,int] = None,
metadata : dict = None
):
'''
Store poem metadata (if initialized by Poetree instance) or get them
from API (if initialized directly).
Arguments:
lang (str|None) : ISO code of the corpus, required if initialized directly
base_url (str) : API base URL (default: set in config.py)
id_ (int|None) : Id(DB) of the poem
metadata (dict|None) : Poem metadata passed when initialized by Poetree instance
Raises:
ValueError : If neither [metadata] nor [lang] is passed
: If neither [metadata] nor id_ is passed
Returns:
None
'''
self.base_url = base_url
self.content_ = list()
if metadata is not None:
self.metadata_ = metadata
elif lang is None:
raise ValueError (
'Argument [lang] is required when initializing ' +
f'{__class__.__name__} instance directly'
)
elif id_ is None:
raise ValueError (
'One of the arguments [id_,wiki,viaf] is required when initializing ' +
f'{__class__.__name__} instance directly'
)
else:
self._get_poem_metadata(lang, id_)
for k, v in self.metadata_.items(): setattr(self, k, v)
def _get_poem_metadata(self, lang: str, id_:Union[int,str]):
'''
Get metadata on poem and store them in self.metadata_
Arguments:
lang (str) : ISO code of the corpus
id_ (int|str) : Id of the poem
Returns:
None
'''
self.metadata_ = make_request(
self.base_url,
'poem',
**{'corpus': lang, 'id_poem': id_, 'lines': 0}
)
self.metadata_['corpus'] = lang
def get_body(self, **kwargs):
'''
Get body of the poem (if not fetched yet), store it in self.content_
and return it
Arguments:
None
Returns:
(dict) : Object representing body of the poem
'''
if len(self.content_) == 0:
response = make_request(
self.base_url,
'poem',
**{'corpus': self.corpus, 'id_poem': self.id_, **kwargs}
)
self.content_ = response['body']
return self.content_
def get_all(self):
'''
Get body of the poem (if not fetched yet), store it in self.content_
and return it together with metadata
Arguments:
None
Returns:
(dict) : Object representing body and metadata of the poem
'''
if len(self.content_) == 0:
self.get_body()
return {**self.metadata_, **{'body': self.content_}}
def metadata(
self,
target : str = 'self',
output : str = 'list',
sortby : Union[str,list] = None,
reverse : bool = False
) -> Union[list, pd.DataFrame, None]:
'''
Returns target metadata either as a formatted table (tabular=True)
or as a list as received from API (tabular=False). The list may be
sorted according to any subdict key.
Params:
target (str) : Metadata of what to return; default: 'corpora'
output (str) : Output format: 'list': list as retrieved from API,
'pandas': pd.DataFrame, 'print': stringified table
printed directly; default: 'list'
sortby (str|None) : Subdict key according to which sort the list;
default: None
reverse (bool) : Sort in reversed (descending) order; default False
Returns:
(list|pd.DataFrame|None) : metadata
'''
if target == 'self':
return metadata([self], output, sortby, reverse)
else:
return metadata(self.content_[target], output, sortby, reverse)