Warp 10 2.0 introduced interactive mode which makes easier to explore WarpScript. In this tutorial we set up a WarpScript interpreter using this feature.
Since the 2.0 version of Warp 10, it is possible to submit WarpScript interactively to an instance: you can send WarpScript functions, get back the state of the stack, send other functions, get the new state, and so on. This functionality opens new possibilities, and we will explore one of them in this article: a simple WarpScript interactive console, a.k.a. a Read–Eval–Print Loop (REPL).
Discover the latest version of Warp 10. |
Setting it up
To test the interactive mode, you will need your own Warp 10 instance. If you don't have one yet and don't want to bother with that now, you can set up Warp 10 in Docker with the following command:
docker run -d -p 8080:8080 -p 8081:8081 warp10io/warp10:latest
Next, the client interactive console we developed is in Python which should be already installed on your computer. You will also need the ws4py module to connect through WebSockets to the Warp 10 endpoint. Install it with pip
using the command:
sudo -H pip install ws4py
Now download the Python program here and run it with the command:
python wscli.py ws://127.0.0.1:8080/api/v0/interactive
WarpScript functions for interactive sessions
When in an interactive session, you have access to several new functions to specify how the Warp 10 instance responds to the input:
- ECHOON to display part of the stack for each input
- ECHOOFF to display nothing for each input
- JSONSTACK to set the display of the stack as a JSON
- WSSTACK to set the display of the stack as SNAPSHOT would do, this is the default
- PSTACK to display once the stack, even if ECHOOFF
Examples
You can now interact with Warp 10 in WarpScript, typing commands like you would do in a terminal or a Python console. For instance, we can set the interactive session to display 100 levels of stack in JSON, create a GTS with 10 random locations and values, make a typo and compute the NSUMSUMSQ:
//
//
// ___ __ ____________
// __ | / /_____ _______________ __< /_ __ \
// __ | /| / /_ __ `/_ ___/__ __ \ __ /_ / / /
// __ |/ |/ / / /_/ /_ / __ /_/ / _ / / /_/ /
// ____/|__/ \__,_/ /_/ _ .___/ /_/ \____/
// /_/
//
// Revision 2.8.1-152-g111a1ee6//
WS> 100 ECHOON JSONSTACK
WS> NEWGTS
/* 1 */ {"c":"","l":{},"a":{},"la":0,"v":[]}
WS<1> 1 10 <% RAND RAND RAND RAND ADDVALUE %> FOR
/* 1 */ {"c":"","l":{},"a":{},"la":0,"v":[[1,0.7500306330621243,0.539723401889205,0,0.9764217281457779],[2,0.15266664326190948,0.6082757189869881,0,0.8346367916719499],[3,0.40475426707416773,0.285873357206583,0,0.20616845847470688],[4,0.8777862181887031,0.5682937055826187,0,0.007658266783904688],[5,0.06223990581929684,0.4686684161424637,0,0.6560162690047148],[6,0.7260161871090531,0.9387802798300982,0,0.39041866950073134],[7,0.7118573086336255,0.7838528696447611,0,0.6239963466050209],[8,0.8499191142618656,0.35952329635620117,0,0.5844455590835379],[9,0.10045891162008047,0.2561959717422724,0,0.8223646151057479],[10,0.15573848504573107,0.7110142987221479,0,0.8127311520196275]]}
WS<1> IMADEATYPO
// ERROR Exception at '%3D%3EIMADEATYPO%3C%3D' in section %5BTOP%5D %28Unknown function 'IMADEATYPO'%29
WS<1>
However, be aware that interactive does not mean step-by-step. When executing macros or using any function using macros, the state of the stack will be given only after the macro is executed. Here is an example:
WS> 100 ECHOON JSONSTACK
WS> []
/* 1 */ []
WS<1> 1 10
/* 3 */ []
/* 2 */ 1
/* 1 */ 10
WS<3> <% +! %>
/* 4 */ []
/* 3 */ 1
/* 2 */ 10
/* 1 */ "<% +! %>"
WS<4> FOR
/* 1 */ [1,2,3,4,5,6,7,8,9,10]
WS<1>
Conclusion
If you're new to WarpScript, this is a good way to understand how it works. You can really visualize how each function modifies the stack. If you're already a WarpScript veteran, the interactive mode can help you explore and debug your scripts. You can also develop your own tools to leverage the capabilities offered by the interactive endpoint.
Subscribe to the newsletter to never miss new posts.
Read more
A review of smoothing transforms in WarpLib
FETCHez la data !
Aggregate by calendar duration in WarpScript
WarpScript™ Doctor