-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathbook_club.py
More file actions
98 lines (73 loc) · 2.62 KB
/
book_club.py
File metadata and controls
98 lines (73 loc) · 2.62 KB
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
import re
class Book:
pattern = re.compile(r'(.+)\((\d+)\)\. (.+)\. (.+)\..*')
def __set_name__(self, owner, name):
self.name = name
def attr(self, attr):
return f"{self.name}.{attr}"
def __set__(self, instance, value):
matches = self.pattern.match(value)
if not matches:
raise ValueError("Book data must be specified in APA 7 format.")
setattr(instance, self.attr('author'), matches.group(1))
setattr(instance, self.attr('year'), matches.group(2))
setattr(instance, self.attr('title'), matches.group(3))
setattr(instance, self.attr('publisher'), matches.group(4))
def __get__(self, instance, owner=None):
try:
title = getattr(instance, self.attr('title'))
author = getattr(instance, self.attr('author'))
except AttributeError:
return "nothing right now"
return f"{title} by {author}"
def __delete__(self, instance):
delattr(instance, self.attr('author'))
delattr(instance, self.attr('year'))
delattr(instance, self.attr('title'))
delattr(instance, self.attr('publisher'))
class BookClub:
reading = Book()
reading_next = Book()
def __init__(self, name):
self.name = name
self.members = []
def new_member(self, member):
self.members.append(member)
print(
"===== - - - - - - - - - =====",
f"Welcome to the {self.name} Book Club, {member}!",
f"We are reading {self.reading}",
"===== - - - - - - - - - =====",
sep='\n'
)
mystery_lovers = BookClub("Mystery Lovers")
lattes_and_lit = BookClub("Lattes and Lit")
mystery_lovers.reading = (
"McDonald, J.C. (2019). "
"Noah Clue, P.I. AJ Charleson Publishing."
)
lattes_and_lit.reading = (
"Christie, A. (1926). "
"The Murder of Roger Ackroyd. William Collins & Sons."
)
print(mystery_lovers.reading) # prints "'The Murder of Roger Ackroyd..."
print(lattes_and_lit.reading) # prints "'The Murder of Roger Ackroyd..."
del lattes_and_lit.reading
lattes_and_lit.new_member("Jaime")
lattes_and_lit.reading = (
"Hillerman, T. (1973). "
"Dance Hall Of The Dead. Harper and Row."
)
lattes_and_lit.new_member("Danny")
mystery_lovers.reading = (
"McDonald, J.C. (2019). "
"Noah Clue, P.I. AJ Charleson Publishing."
)
mystery_lovers.reading_next = (
"Chesterton, G.K. (1911). The Innocence of Father Brown. "
"Cassell and Company, Ltd."
)
print(f"Now: {mystery_lovers.reading}")
print(f"Next: {mystery_lovers.reading_next}")
import pprint
pprint.pprint(dir(mystery_lovers))