AnyBase Functions#

import - Import a configuration file#

$import{[filename], [expected DTI]}

Import a JSON configuration file. If the second parameter is given, it specifies the expected DTI of the configuration file.

Important

This function caches the files’ data for the lifetime of the Python interpreter instance. This is meant to speed up parsing of large configurations with many imports. Do NOT use this function for reading data files whose content may change during the lifetime of the Python session.

Related:

  • __includes__: This is a language feature of the default ISON parser. It can be used instead of $import{}. The advantage of this language feature is that it updates the enclosing dictionary with the dictionary loaded. For example, it can be used directely as elememt of a __globals__ element to load additional variable definitions. See Advanced Features.

  • $read{}: Function of default ISON parser to load a text or JSON file. See File/Path Functions

from pathlib import Path
from ison.util import text, data

# The 'CAnyCML' class is just a wrapper for ison.Parser,
# which adds more functions to the parser.
from anybase.cls_anycml import CAnyCML

dicData = {
    # The second parameter specifies the expected DTI.
    # If the DTI string does not start with a '/', 
    # a '/catharsys/' is added to the front.
    "result": "$import{cfg.json, test:1}"
}

xParser = CAnyCML()
dicResult = xParser.Process(dicData, sImportPath=Path.cwd())
print(text.ToString(data.StripVarsFromData(dicResult)))
{"result": {"sDTI": "/catharsys/test:1.0", "hello": "world", "sId": "cfg"}}

json - Create a JSON string from data#

$json{[data object]}

This function converts any data objects to a JSON string.

from pathlib import Path
from ison.util import text, data

# The 'CAnyCML' class is just a wrapper for ison.Parser,
# which adds more functions to the parser.
from anybase.cls_anycml import CAnyCML

dicData = {
    "__globals__": {
        "dicA": {
            "a": 1,
            "b": 2
        }
    },
     
    "result": "$json{$dicA}"
}

xParser = CAnyCML()
dicResult = xParser.Process(dicData, sImportPath=Path.cwd())
print(text.ToString(data.StripVarsFromData(dicResult)))
{"result": "{\"a\":1,\"b\":2}"}

py - Execute a python command#

$py{[python expression]}

This function interprets the string passed to it as a python expression and evaluates it with the python eval() command.

Important

There are no checks on the code itself. So be careful parsing files from sources you don’t trust.

The python evaluation imports the modules:

  • random as rnd and random

  • numpy as np and numpy

All variables available to the ISON parser are also made available in the dictionary variable dicVar. The result of the python evaluation replaces the command.

from pathlib import Path
from ison.util import text, data

# The 'CAnyCML' class is just a wrapper for ison.Parser,
# which adds more functions to the parser.
from anybase.cls_anycml import CAnyCML

dicData = {
    "__globals__": {
        "iCnt": 10
    },
     
    "result": "$py{list(range($iCnt))}"
}

xParser = CAnyCML()
dicResult = xParser.Process(dicData, sImportPath=Path.cwd())
print(text.ToString(data.StripVarsFromData(dicResult)))
{"result": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}
from pathlib import Path
from ison.util import text, data

# The 'CAnyCML' class is just a wrapper for ison.Parser,
# which adds more functions to the parser.
from anybase.cls_anycml import CAnyCML

dicData = {
    "__globals__": {
        "iCnt": 10
    },
     
    "result": "$py{list(dicVar.keys())}",
    "result2": "$py{dicVar['@glo']}"
}

xParser = CAnyCML()
dicResult = xParser.Process(dicData, sImportPath=Path.cwd())
print(text.ToString(data.StripVarsFromData(dicResult), iIndent=4))
{
    "result": [
        "@loc",
        "@loc-eval",
        "@loc-s",
        "@loc-eval-s",
        "@glo",
        "@glo-eval",
        "@func-loc",
        "@func-loc-s",
        "@func-glo",
        "@rtv",
        "@rtv-eval",
        "@func-storage",
        "@top"
    ],
    "result2": {
        "iCnt": 10
    }
}