# -*- coding: utf-8 -*-
#
# This file is subject to the terms and conditions defined in
# file 'LICENSE.txt', which is part of this source code package.
#
from typing import Tuple, Any, Union, TypeVar
D = TypeVar("D")
[docs]
class LocalVariables():
"""Class to store user variables names and values
the user can then replace this variable in a string
"""
def __init__(self, prePostChars: Tuple[str, str] = ("{", "}")) -> None:
"""Create a LocalVariables
Parameters
----------
prePostChars : Tuple[str,str], optional
string to be used to search and replace the variables in the Apply functions,
by default ("{","}")
"""
super().__init__()
self.prePostChars = prePostChars
self.variables: dict = {}
[docs]
def SetVariable(self, name: str, value: Any) -> None:
"""Set a internal the value (value) of variable (name)
Parameters
----------
name : str
name of the variable
value : Any
The Value of the variable
"""
self.variables[str(name)] = value
[docs]
def UnsetVariable(self, name: str) -> None:
"""Remove a variable for the internal storage
Parameters
----------
name : str
name of the variable to be removed
"""
self.variables.pop(str(name))
[docs]
def Apply(self, string: Union[str,D]) -> Union[str,D]:
"""Modified and return the string provided.
Search and replace the variable names on the string by their value
the search is done using the prePostChars I.e.
.SetVariable("data",5)
.Apply("{data}+1") -> "5+1"
Warning: the values are converted to string to be inserted in the string
Parameters
----------
string : str
The user string to be treated
Returns
-------
str
The string with the variable values in place of the variable names
"""
if type(string) != str:
return string
for name, value in self.variables.items():
string = string.replace(self.prePostChars[0]+name+self.prePostChars[1], str(value))
return string
globalDict = LocalVariables()
[docs]
def AddCommonConstants(target: LocalVariables = globalDict) -> None:
"""Add common constant to a LocalVariables instance
variable added :
yotta
zetta
exa
peta
tera
giga
mega
kilo
hecto
deka
deci
centi
milli
micro
nano
pico
femto
atto
zepto
yocto
pi
GradToDegree
DegreeToGrad
Parameters
----------
target : LocalVariables, optional
An instance of LocalVariables, by default the global LocalVariables of Muscat is used
"""
from scipy import constants
siPrefixes = {"yotta": constants.yotta,
"zetta": constants.zetta,
"exa": constants.exa,
"peta": constants.peta,
"tera": constants.tera,
"giga": constants.giga,
"mega": constants.mega,
"kilo": constants.kilo,
"hecto": constants.hecto,
"deka": constants.deka,
"deci": constants.deci,
"centi": constants.centi,
"milli": constants.milli,
"micro": constants.micro,
"nano": constants.nano,
"pico": constants.pico,
"femto": constants.femto,
"atto": constants.atto,
"zepto": constants.zepto,
"pi": constants.pi,
"GradToDegree": (180/constants.pi),
"DegreeToGrad": (constants.pi/180)}
if hasattr(constants, "yocto"):
siPrefixes["yocto"] = constants.yocto
for name, value in siPrefixes.items():
target.SetVariable(name, value)
[docs]
def AddToGlobalDictionary(name: str, value: Any) -> None:
"""Add a variable and its value to the global variable space of Muscat
Parameters
----------
name : str
name of the variable
value : Any
The Value of the variable
"""
globalDict.SetVariable(name, value)
[docs]
def RemoveFromGlobalDictionary(name: str) -> None:
"""Remove a variable from the global variable space of Muscat
Parameters
----------
name : str
name of the variable
"""
globalDict.UnsetVariable(name)
[docs]
def ApplyGlobalDictionary(string: Union[str, D]) -> Union[str, D]:
"""Modified and return the string provided using the global variable space of Muscat.
please read py:class:LocalVariables.Apply for more information, if input is not string
then is returned as it (no modification)
Parameters
----------
string : str
The user string to be treated
Returns
-------
str
The string with the variable values in place of the variable names
"""
return globalDict.Apply(string)
[docs]
def CheckIntegrity(GUI: bool = False) -> str:
AddCommonConstants()
lv = LocalVariables()
lv.SetVariable("data", "Hello")
b = lv.Apply("{data} World!")
assert (b == "Hello World!")
lv.UnsetVariable("data")
b = lv.Apply("{data} World!")
assert (b == "{data} World!")
AddToGlobalDictionary("LocalVariables_CheckIntegrity", "TempVariable")
RemoveFromGlobalDictionary("LocalVariables_CheckIntegrity")
assert ("1000.0*0.001" == ApplyGlobalDictionary("{kilo}*{milli}"))
return "ok"
if __name__ == '__main__':
print(CheckIntegrity()) # pragma: no cover