This repository was archived by the owner on Sep 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
202 lines (169 loc) · 6.58 KB
/
utils.py
File metadata and controls
202 lines (169 loc) · 6.58 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
"""The utils module is the most important module of the entire library with the
most important classes stored in the utils module. Due to the importance of
these classes, all these classes can be directly imported from
ARgorithmToolkit.
Both work:
>>> algo = ARgorithmToolkit.utils.StateSet()
>>> algo = ARgorithmToolkit.StateSet()
"""
class ARgorithmError(Exception):
"""The error class for ARgorithmToolkit.
Used to debug errors that are caused due to the logic and internal
workings of ARgorithmToolkit
"""
def __init__(self,*args):
super().__init__(*args)
if args:
self.message = args[0]
else:
self.message = None
def __str__(self):
if self.message:
return f'{self.message}'
return "There's an error within ARgorithm"
class ARgorithmClientError(Exception):
"""The error class for programmers to use in their ARgorithm.
Programmers can throw this error from their program when they want
to raise an error For example when user gives incorrect input to
program
"""
def __init__(self,*args):
super().__init__(*args)
if args:
self.message = args[0]
else:
self.message = None
def __str__(self):
if self.message:
return f'{self.message}'
return "User has entered faulty data"
class State:
"""The Instance of State class can be considered as an event in the
sequential order of events that get played out in Augmented Reality Each
Data structure has a helper class that makes states for it.
For example , ARgorithmToolkit.array.Array has
ARgorithmToolkit.array.ArrayState
"""
def __init__(self,**kwargs):
self.content = {}
for x in ['state_type','state_def','comments']:
try:
self.content[x] = kwargs[x]
except KeyError as e:
raise ARgorithmError(f"{x} should be present in State arguments") from e
def __str__(self):
data = str(self.content)
return data
class StateSet:
"""The most important class in the entire toolkit. An object of this class
has to exist in every algorithm. That object of StateSet is what should
returned by ARgorithm as that is what is rendered in Augmented Reality As
these are what contain the metadata for rendering the algorithm. Instance
of this class is conventionally called ``algo``
Attributes:
states (list): This is list of State objects that is sequentially rendered in Augmented Reality.
Examples:
>>> algo = ARgorithmToolkit.StateSet()
"""
def __init__(self):
self.states = []
def add_state(self,state):
"""This method adds State to the list of states.
Args:
state (ARgorithmToolkit.utils.State): The state that has to be added
Raises:
ARgorithmError: Raised if state is not of type State
Example:
>>> algo.add_state(state)
"""
assert isinstance(state,State) , ARgorithmError("state should be of Type state")
self.states.append(state)
def __str__(self):
"""String representation of StateSet.
Returns:
str: The list of all states in a multiline string
"""
state_desc = "\n".join([x.__str__() for x in self.states]) if len(self.states) > 0 else ""
return f"{state_desc}"
def add_comment(self,comments:str):
"""Adds a blank state with just text information that could be used for
describing content. Check the comments section for more info.
Args:
comments (str): Comments for descriptive purpose
"""
comment_state = State(
state_type="comment",
state_def=None,
comments=comments
)
self.add_state(comment_state)
class ARgorithmHashable:
"""Interface from which main classes for datastructures can inherit to make
them hashable in Set and Map implementations.
This interface will enable different classes to be keys in Map(hash-
map) and Set(hash-set) implementations.
"""
class ARgorithmStructure:
"""Interface from which main classes for datastructures.
This interface enables different classes to be a values in Map(hash-
map) implementations.
"""
class Variable(ARgorithmStructure, ARgorithmHashable):
"""Some programs might need us to listen and store states of primitive
datatypes like int, str , bool etc. For tha purpose, we have the Variable
class.
Attributes:
name (str): name given to the rendered block in augmented reality. Essential. Should not be altered after initialisation
algo (ARgorithmToolkit.utils.StateSet): The stateset that will store the states generated by the instance of Variable Class
value : The data stored in the variable
Example:
>>> flag = ARgorithmToolkit.Variable(name="flag",algo=algo,value=True)
>>> count = ARgorithmToolkit.Variable(name="count",algo=algo,value=0)
Note:
Making changes in values of variable might seen hectic for now but future versions will fix the matter
"""
def __init__(self,name:str,algo:StateSet,value=None,comments=""):
self.algo = algo
self.name = name
self.__flag = False
self.value = value
self._id = str(id(self))
state_type = "variable_declare"
state_def = {
"id" : self._id,
"variable_name" : name ,
"value" : value
}
self.algo.add_state(State(
state_type=state_type,
state_def=state_def,
comments=comments
))
def __setattr__(self,key,value):
"""Operator overload to listen to changes in value of Variables.
Args:
key ([type]): [description]
value ([type]): [description]
"""
last_value = None
if key=='value' and self.__flag:
last_value = self.value
self.__dict__[key] = value
if(key == 'value' and self.__flag):
state_type = "variable_highlight"
state_def = {
"id" : self._id,
"variable_name" : self.name,
"value" : self.value,
}
if last_value is not None:
state_def["last_value"] = last_value
self.algo.add_state(State(
state_type=state_type,
state_def=state_def,
comments=""
))
elif key=='value':
self.__flag = True
def __repr__(self):
return f"Variable({repr(self.value)})"