Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic applications of calling Python scripts?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What are the basic application methods of calling Python scripts? For this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more small partners who want to solve this problem find simpler and easier methods.

When programmers need to choose a suitable computer language for program development, Python is often in their minds. Its flexible and easy-to-use features greatly improve the efficiency of developers. Here we will explain in detail how to invoke Python scripts.

I have a simple understanding of Python language before, but I have not studied it deeply and systematically. I just always feel that it is flexible enough and does not need to be compiled (because Python is an interpreted language), so I always want to apply it to CodeMaker to achieve a flexible and customized configuration.

First, download the IronPython library (here IronPython-v2.6), we use the IronPython library to run the Python script under. Net.

2. Add IronPython and related libraries to the project reference.

IronPython.dll IronPython.Modules.dll Microsoft.Dynamic.dll Microsoft.Scripting.Core.dll Microsoft.Scripting.Debugging.dll Microsoft.Scripting.dll Microsoft.Scripting.ExtensionAttribute.dll

3, only made a small example, not very complex, so directly paste the code that calls Python script.

//main method static void Main(string[] args) { //Create script engine ScriptEngine = Python.CreateEngine(); //Create a script scope container ScriptScope = engine.CreateScope(); //Set variable engine.SetVariable(scope, "UserName", "cnBlogs"); //Execute Python script and get return value ScriptScope resultScope = engine.ExecuteFile("test.py", scope);//Output result Console.WriteLine(resultScope.GetVariable("Result")); } // Python script: def GetType(name): return "hello " + name; Result = GetType(UserName);

Calling Python script content is very simple, declare a function, the parameter is name, the function returns the string after concatenation of name, the function ends. Then call the function with UserName and Result.

Script, UserName and main method

engine.SetVariable(scope, "UserName", "cnBlogs"); corresponds to.

Result and main method

resultScope.GetVariable("Result"), rather than declaring these two variables in the script.

Implementation results:

As you can see, the output string is returned from a Python script, and you can change the execution result by modifying the script.

4. Call Python script application in CodeMaker

Usage:

We have also mentioned before that the code generation tool is to customize templates in the form of tags, and then generate code files based on the templates. So a script tag is added for the corresponding Python script.

< c:script path="test.py" variables="{userName:cnBlogs;password:123456}" result="Result" />

path: Script file path (relative to EXE file path)

variables: variable container, originally intended to use a format similar to JSON, but because some special symbols affect the parsing of labels, so slightly changed the separator, multiple variables and values separated by;(semicolon).

result: The return variable of the script execution result, used to obtain the script return value in the code.

Usage: When generating entity classes, for most type conversions, you can use Convert.ToXXX() directly, but for Guid, you can only use new Guid().

With the help of scripts, you can determine the type in the script. If it is Guid, it will return new Guid(); if it is other type, it will return ConvertToXXX();

About the basic application of Python script is what the answer to the question is shared here, I hope the above content can be of some help to everyone, if you still have a lot of doubts not solved, you can pay attention to the industry information channel to learn more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report