Skip to content

Commit 16c80f0

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-based system for organizing the logic of the various graph types.
1 parent a97aa1f commit 16c80f0

File tree

2 files changed

+139
-38
lines changed

2 files changed

+139
-38
lines changed

econplayground/main/graphs.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
line_actions = [
49+
'up',
50+
'down',
51+
'slope up',
52+
'slope down',
53+
]
54+
variable_actions = [
55+
'up',
56+
'down',
57+
'<exact value>',
58+
]
59+
60+
@classmethod
61+
def get_rule_options(cls) -> dict:
62+
return {
63+
'line1': {
64+
'name': 'Orange line',
65+
'possible_values': cls.line_actions,
66+
},
67+
68+
'line2': {
69+
'name': 'Blue line',
70+
'possible_values': cls.line_actions,
71+
},
72+
73+
'line1 label': 'Orange line label',
74+
'line2 label': 'Blue line label',
75+
'intersectionLabel': 'Intersection label',
76+
'intersectionHorizLineLabel':
77+
'Orange-Blue intersection horizontal label',
78+
'intersectionVertLineLabel':
79+
'Orange-Blue intersection vertical label',
80+
'x-axis label': 'X-axis label',
81+
'y-axis label': 'Y-axis label',
82+
}
83+
84+
85+
class Graph0(Graph):
86+
name = GRAPH_TYPES[0][1]
87+
graph_type = 0
88+
89+
@classmethod
90+
def get_rule_options(cls) -> dict:
91+
return super().get_rule_options()
92+
93+
94+
class Graph1(Graph):
95+
name = GRAPH_TYPES[1][1]
96+
graph_type = 1
97+
98+
99+
class Graph3(Graph):
100+
name = GRAPH_TYPES[2][1]
101+
graph_type = 3
102+
103+
104+
class Graph5(Graph):
105+
name = GRAPH_TYPES[3][1]
106+
graph_type = 5
107+
108+
109+
class Graph7(Graph):
110+
name = GRAPH_TYPES[4][1]
111+
graph_type = 7

econplayground/main/models.py

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,8 @@
99
from django.dispatch import receiver
1010
from django.db.models.signals import post_save, pre_delete
1111

12+
from econplayground.main.graphs import GRAPH_TYPES, Graph0
1213

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'),
20-
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-
)
5114

5215
ASSIGNMENT_TYPES = (
5316
(0, 'Template'),
@@ -475,6 +438,33 @@ def get_rule_options(graph_type: int = 0) -> dict:
475438
'possible_values': line_actions,
476439
}
477440
})
441+
elif graph_type == 25:
442+
rules = {
443+
'a1': {
444+
'name': 'Choke Price',
445+
'possible_values': variable_actions,
446+
},
447+
'a2': {
448+
'name': 'Demand Slope',
449+
'possible_values': variable_actions,
450+
},
451+
'a3': {
452+
'name': 'Reservation Price',
453+
'possible_values': variable_actions,
454+
},
455+
'a4': {
456+
'name': 'Supply Slope',
457+
'possible_values': variable_actions,
458+
},
459+
'a5': {
460+
'name': 'Global/Minimum/Maximum Price or Production Quota',
461+
'possible_values': variable_actions,
462+
},
463+
'a6': {
464+
'name': 'Tariff',
465+
'possible_values': variable_actions,
466+
},
467+
}
478468
elif graph_type == 26:
479469
rules = {
480470
'a1': {

0 commit comments

Comments
 (0)