Special Functions#
*
- Create Structure from String#
Convert the string argument to an object. Expects the string to be JSON parsable.
import ison
dicData = {
"__locals__": {
"sA": "{ \"a\": 1, \"b\": 2 }"
},
"result": "$*{$sA}",
}
dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
"result": {
"a": 1,
"b": 2
}
}
Instead of using the escaped quotes in the definition of sA
, you can also use the backward quote (`) as in this example:
import ison
dicData = {
"__locals__": {
"sA": "{ `a`: 1, `b`: 2 }"
},
"result": "$*{$sA}",
}
dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
"result": {
"a": 1,
"b": 2
}
}
If you need to nest stings within string, you can use the function $S{}
, which allows arbitrary deep nesting.
import ison
dicData = {
"__locals__": {
"sA": "{ $S{a}: 1, $S{b}: $S{{$S{c}: 1}} }"
},
"result": "$*{$sA}",
}
dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
"result": {
"a": 1,
"b": "{\"c\": 1}"
}
}
print
- Print to Console or Log#
This function can help to debug ISON scripts, by printing out values to the console during parsing. If a log file path is set with the function $set-log-path{}
, $print{}
writes to this log file instead of the console.
Each comma separated argument of the $print{}
function prints to a new line.
import ison
dicData = {
"__locals__": {
"lA": ["Hello", "World"],
"dicA": { "Hello": "World" },
"sLog": "$print{Variable 'dicA':, $json{$dicA}, $lA}"
},
"result": "$dicA"
}
dicResult = ison.run.Run(xData=dicData, bPrintWarnings=True)
print(ison.run.ToString(dicResult))
Variable 'dicA':
{
"Hello": "World"
}
['Hello', 'World']
{
"result": {
"Hello": "World"
}
}
set-log-path
- Set Logging Path#
You can set a logging path in three ways with $set-log-path{}
:
Without arguments the current working directory is used a logging path and a filename is automatically generated.
Give a path without filename. In this case, a filename is automatically generated and the path is created.
Give a full filepath. If the path does not exist, it is created.
import ison
dicData = {
"__locals__": {
"sLogPath": "$set-log-path{}",
"dicA": { "Hello": "World" },
"sLog": "$print{Variable 'dicA':, $json{$dicA}, }"
},
# Uncomment the following line to see the path of the log file
# in the printed output.
# "log-path": "${sLogPath}",
"result": "$dicA"
}
try:
dicResult = ison.run.Run(xData=dicData, bPrintWarnings=True)
print(ison.run.ToString(dicResult))
except Exception as xEx:
print(str(xEx))
# endtry
{
"result": {
"Hello": "World"
}
}
S
- String#
This function is mainly used internally when converting structures to lambda functions. Internally lambda functions are strings. To support arbitrarily nested string expressions the structure $S{}
is used instead of double quotes.
import ison
dicData = {
"result": "$S{Hello $S{Christian}}",
}
dicResult = ison.run.Run(xData=dicData)
print(ison.run.ToString(dicResult))
{
"result": "\"Hello \\\"Christian\\\"\""
}