-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsome_query.py
More file actions
68 lines (48 loc) · 1.8 KB
/
Copy pathsome_query.py
File metadata and controls
68 lines (48 loc) · 1.8 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
import sys
import os
# This makes the file executable
if os.environ.get('PROJECT_PATH'):
sys.path.append(os.environ.get('PROJECT_PATH'))
from database.models import Theatre, Show, Person, Role, ShowsRolesLink
from database import session, DB_URI
show_a_id = session.query(Show.id).filter_by(title='Hamilton').first()
show_b_id = session.query(Show.id).filter_by(title='Wicked').first()
session.query(ShowsRolesLink).all()
#
# my_people = session.query(ShowsRolesLink)\
# .filter(
# ShowsRolesLink.show_id.in_(show_a_id.id, show_b_id.id)
# )\
# .all()
#
# my_people
#
# res = session.query(ShowsRolesLink).all()
#
#
# ------------------------------------------------------------------------------
def people_work_on_both_shows(show_name_a, show_name_b):
"""
Which people worked on 2 shows?
(Didn't get around to finiing this.)
"""
show_a_id = session.query(Show.id).filter_by(title=show_name_a).subquery()
show_b_id = session.query(Show.id).filter_by(title=show_name_b).subquery()
people_show_a = session.query(ShowsRolesLink.person_id)\
.filter(
ShowsRolesLink.show_id==show_a_id
)\
.all()
people_show_b = session.query(ShowsRolesLink.person_id)\
.filter(
ShowsRolesLink.show_id==show_b_id
)\
.all()
people_show_a = set(x[0] for x in people_show_a)
people_show_b = set(x[0] for x in people_show_b)
shared_people_ids = people_show_a.intersection(people_show_b)
shared_people = session.query(Person).filter(Person.id.in_(shared_people_ids)).all()
return shared_people
if __name__ == '__main__':
shared_people = people_work_on_both_shows('Hamilton', 'Wicked')
print(f'There are {len(shared_people):,} people who have worked on both shows;\nTheir names are:', *[x.])