-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.jl
100 lines (86 loc) · 2.32 KB
/
Program.jl
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
using DataFrames, HTTP, JSON
struct Bloc
name::String
max::Int
min::Int
id::Symbol
courses::Vector{Symbol}
end
function json_to_bloc(bloc_json)
courses = Symbol.(bloc_json["courses"])
Bloc(
bloc_json["name"],
round(Int, bloc_json["max"]),
round(Int, bloc_json["min"]),
Symbol(bloc_json["id"]),
courses
)
end
struct Segment
name::String
id::Symbol
blocs::Vector{Bloc}
description::String
end
function json_to_segment(segment_json)
blocs = [json_to_bloc(bloc) for bloc in segment_json["blocs"]]
Segment(
segment_json["name"],
Symbol(segment_json["id"]),
blocs,
segment_json["description"]
)
end
struct Program
name::String
id::Symbol
segments::Vector{Segment}
structure::String # need to parse
end
function Program(program_json)
segments = [json_to_segment(segment) for segment in program_json["segments"]]
Program(
program_json["name"],
Symbol(program_json["_id"]),
segments,
program_json["structure"]
)
end
struct Programs ## Turn into a Maskable (df for programs)
progs::Vector{Program}
id::Dict{Symbol, Int}
name::Dict{String, Int}
end
function Programs(url::String)
rsp = HTTP.get(url)
@assert(rsp.status == 200)
prs = JSON.parse(String(rsp.body))["programs"]
progs = Program[]
id = Dict{Symbol, Int}()
name = Dict{String, Int}()
for prog_json in prs
prog = Program(prog_json)
push!(progs, prog)
id[prog.id] = length(progs)
name[prog.name] = length(progs)
end
return Programs(progs, id, name)
end
Base.getindex(p::Programs, index::Int) = p.progs[index]
Base.getindex(p::Programs, sym::Symbol) = p.progs[p.id[sym]]
Base.getindex(p::Programs, name::String) = p.progs[p.name[name]]
Base.getindex(p::Programs, r::Regex) = [p.progs[i] for (name, i) in p.name if !isnothing(match(r, name))]
function getcourses(p::Program)
df = DataFrame(prog=Symbol[], segment=Symbol[], bloc=Symbol[], sigle=Symbol[])
p_sym = p.id
for segment in p.segments
s_sym = segment.id
for bloc in segment.blocs
b_sym = bloc.id
for course in bloc.courses
push!(df, [p_sym, s_sym, b_sym, course])
end
end
end
return df
end