Add a new variable to variable table. Parameters ---------- var : str The name of variable. value : Any The value of variable. allow_shadowing : bool The options of whether variable shadowing allowed for this variable.
(self, var: str, value: Any, allow_shadowing: bool = False)
| 247 | return _deferred(pop_frame) |
| 248 | |
| 249 | def add(self, var: str, value: Any, allow_shadowing: bool = False): |
| 250 | """Add a new variable to variable table. |
| 251 | |
| 252 | Parameters |
| 253 | ---------- |
| 254 | var : str |
| 255 | The name of variable. |
| 256 | |
| 257 | value : Any |
| 258 | The value of variable. |
| 259 | |
| 260 | allow_shadowing : bool |
| 261 | The options of whether variable shadowing allowed for this variable. |
| 262 | """ |
| 263 | # Skip if the key and value are equal to those in the var_table |
| 264 | if self.name2value[var] and isinstance(self.name2value[var][-1], type(value)): |
| 265 | if isinstance(value, np.ndarray) and (self.name2value[var][-1] == value).all(): |
| 266 | return |
| 267 | elif self.name2value[var][-1] == value: |
| 268 | return |
| 269 | if allow_shadowing and var in self.frames[-1].vars: |
| 270 | # Shadowing |
| 271 | self.name2value[var][-1] = value |
| 272 | else: |
| 273 | self.frames[-1].add(var) |
| 274 | self.name2value[var].append(value) |
| 275 | |
| 276 | def get(self) -> dict[str, Any]: |
| 277 | """Get a variable dictionary of latest variables. |
no test coverage detected