-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.py
32 lines (25 loc) · 889 Bytes
/
text.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 os
class Text:
"""
Class for managing texts retrieved from a directory of text files.
After instantiation, texts can be retrieved through attribute access, for
instance, the following example will create a Text instance and print the
contents of the ./text/about file:
>>> t = Text('./text')
>>> print(t.about)
"""
def __init__(self, directory):
"""
Create a new Text object.
Args:
directory (str): Directory in which to find text files
"""
self._texts = {}
for file_name in os.listdir(directory):
with open(os.path.join(directory, file_name)) as f:
self._texts[file_name] = f.read().strip()
def __getattr__(self, attr):
if attr in self._texts:
return self._texts[attr]
else:
raise AttributeError()