Host python in your application with ironpython2.0.1 (1): Create an interactive environment

Source: Internet
Author: User
Tags hosting

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.