Skip to content

Commit 03b3f01

Browse files
committed
Build out graph logic into different classes on the back-end
`Graph.get_rule_options()` was getting too complex, so I've built out a class system for organizing the logic of the various graph types.
1 parent a97aa1f commit 03b3f01

File tree

2 files changed

+254
-37
lines changed

2 files changed

+254
-37
lines changed

econplayground/main/graphs.py

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
"""
2+
Graph-specific logic
3+
"""
4+
from abc import ABC, abstractmethod
5+
6+
7+
GRAPH_TYPES = (
8+
(0, 'Linear Demand and Supply'),
9+
(1, 'Input Markets'),
10+
(3, 'Cobb-Douglas Production Graph'),
11+
(5, 'Consumption-Leisure: Constraint'),
12+
(7, 'Consumption-Saving: Constraint'),
13+
(8, 'Linear Demand and Supply: 3 Functions'),
14+
15+
# Area Under Curve graphs
16+
(9, 'Linear Demand and Supply: Areas'),
17+
(10, 'Input Markets: Areas'),
18+
19+
(11, 'Consumption-Saving: Optimal Choice'),
20+
(15, 'Consumption-Leisure: Optimal Choice'),
21+
22+
# Joint Graphs
23+
(12, 'Input-Output Illustrations'),
24+
(13, 'Linear Demand and Supply: 2 Diagrams'),
25+
(14, 'Input Markets: 2 Diagrams'),
26+
27+
(16, 'Template Graph'),
28+
29+
(17, 'Optimal Choice: Consumption with 2 Goods'),
30+
(18, 'Cost Functions'),
31+
(20, 'Price Elasticity of Demand and Revenue'),
32+
(21, 'Optimal Choice: Cost-Minimizing Production Inputs'),
33+
34+
(22, 'Tax Rate and Revenue'),
35+
(23, 'Taxation in Linear Demand and Supply'),
36+
(24, 'Tax Supply and Demand vs. Tax Revenue'),
37+
38+
(25, 'Linear Demand and Supply - Surplus Policies'),
39+
40+
# Externalities
41+
(26, 'Negative Production Externality - Producer'),
42+
(27, 'Negative Production Externality - Industry'),
43+
(28, 'Positive Externality - Industry'),
44+
)
45+
46+
47+
class Graph(ABC):
48+
name = 'Generic Graph'
49+
line_actions = [
50+
'up',
51+
'down',
52+
'slope up',
53+
'slope down',
54+
]
55+
variable_actions = [
56+
'up',
57+
'down',
58+
'<exact value>',
59+
]
60+
61+
@classmethod
62+
def get_rule_options(cls) -> dict:
63+
return {
64+
'line1': {
65+
'name': 'Orange line',
66+
'possible_values': cls.line_actions,
67+
},
68+
69+
'line2': {
70+
'name': 'Blue line',
71+
'possible_values': cls.line_actions,
72+
},
73+
74+
'line1 label': 'Orange line label',
75+
'line2 label': 'Blue line label',
76+
'intersectionLabel': 'Intersection label',
77+
'intersectionHorizLineLabel':
78+
'Orange-Blue intersection horizontal label',
79+
'intersectionVertLineLabel':
80+
'Orange-Blue intersection vertical label',
81+
'x-axis label': 'X-axis label',
82+
'y-axis label': 'Y-axis label',
83+
}
84+
85+
86+
class Graph0(Graph):
87+
name = GRAPH_TYPES[0][1]
88+
graph_type = 0
89+
90+
91+
class Graph1(Graph):
92+
name = GRAPH_TYPES[1][1]
93+
graph_type = 1
94+
95+
96+
class Graph3(Graph):
97+
name = GRAPH_TYPES[2][1]
98+
graph_type = 3
99+
100+
@classmethod
101+
def get_rule_options(cls) -> dict:
102+
return {
103+
'a5_name': 'Y label',
104+
'a1_name': 'A label',
105+
'a1': {
106+
'name': 'A',
107+
'possible_values': cls.variable_actions,
108+
},
109+
'a3_name': 'K label',
110+
'a3': {
111+
'name': 'K',
112+
'possible_values': cls.variable_actions,
113+
},
114+
'a4': {
115+
'name': 'α',
116+
'possible_values': cls.variable_actions,
117+
},
118+
'a2_name': 'L label',
119+
'a2': {
120+
'name': 'L',
121+
'possible_values': cls.variable_actions,
122+
}
123+
}
124+
125+
126+
class Graph5(Graph):
127+
name = GRAPH_TYPES[3][1]
128+
graph_type = 5
129+
130+
131+
class Graph7(Graph):
132+
name = GRAPH_TYPES[4][1]
133+
graph_type = 7
134+
135+
136+
class Graph8(Graph):
137+
name = GRAPH_TYPES[5][1]
138+
graph_type = 8
139+
140+
141+
class Graph9(Graph):
142+
graph_type = 9
143+
144+
145+
class Graph10(Graph):
146+
graph_type = 10
147+
148+
149+
class Graph11(Graph):
150+
graph_type = 11
151+
152+
153+
class Graph15(Graph):
154+
graph_type = 15
155+
156+
157+
class Graph12(Graph):
158+
graph_type = 12
159+
160+
161+
class Graph13(Graph):
162+
graph_type = 13
163+
164+
165+
class Graph14(Graph):
166+
graph_type = 14
167+
168+
169+
class Graph16(Graph):
170+
graph_type = 16
171+
172+
173+
class Graph17(Graph):
174+
graph_type = 17
175+
176+
177+
class Graph18(Graph):
178+
graph_type = 18
179+
180+
181+
class Graph20(Graph):
182+
graph_type = 20
183+
184+
185+
class Graph21(Graph):
186+
graph_type = 21
187+
188+
189+
class Graph22(Graph):
190+
graph_type = 22
191+
192+
193+
class Graph23(Graph):
194+
graph_type = 23
195+
196+
197+
class Graph24(Graph):
198+
graph_type = 24
199+
200+
201+
class Graph25(Graph):
202+
graph_type = 25
203+
204+
205+
class Graph26(Graph):
206+
graph_type = 26
207+
208+
209+
class Graph27(Graph):
210+
graph_type = 27
211+
212+
213+
class Graph28(Graph):
214+
graph_type = 28

econplayground/main/models.py

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,17 @@
88
from ordered_model.models import OrderedModel
99
from django.dispatch import receiver
1010
from django.db.models.signals import post_save, pre_delete
11+
import importlib
1112

13+
from econplayground.main.graphs import GRAPH_TYPES
1214

13-
GRAPH_TYPES = (
14-
(0, 'Linear Demand and Supply'),
15-
(1, 'Input Markets'),
16-
(3, 'Cobb-Douglas Production Graph'),
17-
(5, 'Consumption-Leisure: Constraint'),
18-
(7, 'Consumption-Saving: Constraint'),
19-
(8, 'Linear Demand and Supply: 3 Functions'),
15+
# from econplayground.main.graphs import (
16+
# GRAPH_TYPES, Graph0, Graph1, Graph3, Graph5, Graph7, Graph9, Graph10,
17+
# Graph11, Graph15, Graph12, Graph13, Graph14, Graph16, Graph17, Graph18,
18+
# Graph20, Graph21, Graph22, Graph23, Graph24, Graph25, Graph26, Graph27,
19+
# Graph28
20+
# )
2021

21-
# Area Under Curve graphs
22-
(9, 'Linear Demand and Supply: Areas'),
23-
(10, 'Input Markets: Areas'),
24-
25-
(11, 'Consumption-Saving: Optimal Choice'),
26-
(15, 'Consumption-Leisure: Optimal Choice'),
27-
28-
# Joint Graphs
29-
(12, 'Input-Output Illustrations'),
30-
(13, 'Linear Demand and Supply: 2 Diagrams'),
31-
(14, 'Input Markets: 2 Diagrams'),
32-
33-
(16, 'Template Graph'),
34-
35-
(17, 'Optimal Choice: Consumption with 2 Goods'),
36-
(18, 'Cost Functions'),
37-
(20, 'Price Elasticity of Demand and Revenue'),
38-
(21, 'Optimal Choice: Cost-Minimizing Production Inputs'),
39-
40-
(22, 'Tax Rate and Revenue'),
41-
(23, 'Taxation in Linear Demand and Supply'),
42-
(24, 'Tax Supply and Demand vs. Tax Revenue'),
43-
44-
(25, 'Linear Demand and Supply - Surplus Policies'),
45-
46-
# Externalities
47-
(26, 'Negative Production Externality - Producer'),
48-
(27, 'Negative Production Externality - Industry'),
49-
(28, 'Positive Externality - Industry'),
50-
)
5122

5223
ASSIGNMENT_TYPES = (
5324
(0, 'Template'),
@@ -423,6 +394,11 @@ def get_rule_options(graph_type: int = 0) -> dict:
423394
'<exact value>',
424395
]
425396

397+
module = importlib.import_module('econplayground.main.graphs')
398+
graph_class = getattr(module, 'Graph{}'.format(graph_type))
399+
instance = graph_class()
400+
return instance.get_rule_options()
401+
426402
rules = {
427403
'line1': {
428404
'name': 'Orange line',
@@ -475,6 +451,33 @@ def get_rule_options(graph_type: int = 0) -> dict:
475451
'possible_values': line_actions,
476452
}
477453
})
454+
elif graph_type == 25:
455+
rules = {
456+
'a1': {
457+
'name': 'Choke Price',
458+
'possible_values': variable_actions,
459+
},
460+
'a2': {
461+
'name': 'Demand Slope',
462+
'possible_values': variable_actions,
463+
},
464+
'a3': {
465+
'name': 'Reservation Price',
466+
'possible_values': variable_actions,
467+
},
468+
'a4': {
469+
'name': 'Supply Slope',
470+
'possible_values': variable_actions,
471+
},
472+
'a5': {
473+
'name': 'Global/Minimum/Maximum Price or Production Quota',
474+
'possible_values': variable_actions,
475+
},
476+
'a6': {
477+
'name': 'Tariff',
478+
'possible_values': variable_actions,
479+
},
480+
}
478481
elif graph_type == 26:
479482
rules = {
480483
'a1': {

0 commit comments

Comments
 (0)