The last time I used ironpython 2.0 A1, I downloaded the official version 2.0.1, and found that the change was not small... I am out of date.
The original code is no longer running normally, so I adjusted it on the official version 2.0.1.
Using system; using system. collections. generic; using system. text; using ironpython. hosting; using ironpython. runtime; using Microsoft. scripting; using Microsoft. scripting. hosting; namespace ipconsole {class program {static void main (string [] ARGs) {/** Method 1: Read the configuration file through scriptruntimesetup and load the Dynamic Language engine scriptruntimesetup SRS = scriptruntimesetup. readconfiguration (); scriptruntime runtime = new scriptruntime (SRS); scriptengine engine = runtime. getengine ("Python"); ***** // Method 2: Read the configuration file through scriptruntime and load the engine scriptruntime runtime = scriptruntime. createfromconfiguration (); scriptengine engine = runtime. getengine ("Python"); ***** // method 3: directly create an engine *****/scriptengine engine = python. createengine (); stringbuilder cmdbuff = new stringbuilder (); string line = ""; console. writeline (string. format ("ironpython {0}", engine. languageversion); // print version scriptscope scope = engine. createid (); // enter the interaction while (true) {If (cmdbuff. length = 0) {console. write (">>>"); // first prompt} else {console. write ("... "); // 2nd and above prompt} // GET command line = console. readline (); cmdbuff. appendline (line); If (line. startswith ("") | line. startswith ("\ t") | line. endswith (":") {// do nothing... get next input} else {// get the command and execute it String cmd = cmdbuff. tostring (); try {If (CMD. trimend () = "quit") break; // you need to create a scriptsource for execution, and interactivecode is the interactive environment scriptsource source = engine. createscriptsourcefromstring (CMD, sourcecodekind. interactivecode); source. execute (scope);} catch (exception e) {console. writeline (E. message);} // clear command buffer cmdbuff. length = 0 ;}} console. writeline ("quit ");}}}
There are three ways to create an engine:
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <section name = "Microsoft. scripting "type =" Microsoft. scripting. hosting. configuration. section, Microsoft. scripting "requirepermission =" false "/> </configsections> <Microsoft. scripting> <languages> <language names = "ironpython, Python, Py" extensions = ". PY "displayname =" ironpython 2.0.1 "type =" ironpython. runtime. pythoncontext, ironpython "/> <! -- You can add other engines, such as ironruby --> </ages> </Microsoft. Scripting> </configuration>
Let's take a look at the test results, which is better than the original one. Because of this interactive mode, input a will also display the value of A, rather than print.
IronPython 2.0.0.0>>> a=1>>> a1>>> b=2>>> b2>>> a==bFalse>>> def hello(name):... print "hello %s" % name...>>> hello('hydonlee')hello hydonlee>>> quitQuit
The future work will be carried out in the official version 2.0.1. However, I have not found the API documentation? There are all alpha versions. Why is there no official version?