This repository was archived by the owner on Jan 2, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathexamples.py
More file actions
172 lines (139 loc) · 4.02 KB
/
Copy pathexamples.py
File metadata and controls
172 lines (139 loc) · 4.02 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from example_tables import *
# %% ^━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━^
ex1 = School.query()
ex2 = School.query(lambda x: x.campus == 'south')
ex3 = School.query().sort(lambda x: x.campus)
ex4 = School.query().fmap(lambda x: x.name % x.campus)
ex4 = School.query()['name','campus']
@do(Query)
def ex4():
sc0 = yield School.query()
returnM (sc0.name, sc0.campus)
@do(Query)
def ex5():
sc0 = yield School.query()
returnM (
sc0.name,
sc0.departments().count()
)
@do(Query)
def ex6():
dept0 = yield Department.query()
returnM (
dept0.name,
dept0.school.name,
dept0.school.campus
)
@injective()
def num_dept(self):
return self.departments().count()
@do(Query)
def ex7():
sc0 = yield School.query(lambda x: x.num_dept > 3)
returnM (
sc0.code,
sc0.num_dept
)
ex7 = School.query(lambda x: x.num_dept > 3).fmap(lambda x: x.code % x.num_dept)
ex7 = School.query(lambda x: x.num_dept > 3)[['code','num_dept']]
@do(Query)
def ex8():
cs0 = yield Course.query()
returnM (
cs0.no.like_('100%'),
cs0.credits.avg_()
)
@do(Query)
def ex9():
sc0 = yield School.query()
dept0 = yield sc0.departments()
cs0 = yield dept0.courses()
returnM (
sc0.name,
dept0.name,
cs0.title
)
# %% ^━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━^
# AGGREGATION
ex10 = Department.query().count()
ex11 = School.query().avg('num_dept')
@do(Query)
def ex12():
dept0 = yield Department.query()
cs = dept0.courses()
returnM (
dept0.name,
cs.count(),
cs.max('credits'),
cs.sum('credits'),
dept0.courses(lambda x: x.title.like_('Introduction%')).count()
)
# %% ^━━━━━━━━━━━━━━━━━━ ━━━━━━━━━━━━━━━━━━━━━━━━━━━^
@do(Query)
def ex13():
sc0 = yield School.query()
returnM (
sc0.name,
sc0.programs()['degree'].count(),
)
@do(Query)
def ex14():
dept0 = yield Department.query()
returnM (
dept0.name,
dept0.school.campus
)
@do(Query)
def ex15():
dept0 = yield Department.query()
returnM (
dept0.name,
dept0.courses(lambda x: x.credits > 2).lj().count().coalesce_(0)
)
@do(Query)
def ex15():
sc0 = yield School.query()
returnM (
sc0.name,
sc0.departments(lambda x: x.courses(lambda y: y.credits > 2)
.exists())
.lj().count().coalesce_(0)
)
@do(Query)
def ex16():
sc0 = yield School.query()
returnM (
sc0.campus,
sc0.departments().lj().count()
)
@Lifted
def coursestart(course, start):
return (course.no > start) & (course.no < start + 99)
@do(Query)
def ex17():
dept0 = yield Department.query()
returnM (
dept0.name,
*[dept0.courses(lambda x: x.coursestart(i*100)).count() for i in range(1,5)]
)
@injective()
def high_credit_count(self):
return self.courses(lambda x: x.credits > 3).count()
@do(Query)
def ex18():
sc0 = yield School.query(lambda x: x.programs().exists())
returnM (
sc0.name,
sc0.departments()['high_credit_count'].avg()
)
@do(Query)
def ex19():
sc0 = yield School.query(lambda x: x.programs(lambda y: y.title == x.name)
.count() >= 5)
returnM (
sc0.name,
sc0.departments()
.fmap(lambda x: x.courses(lambda y: y.credits > 3).count())
.lj().avg()
)
print(ex19().sql(debug=True))