Showing posts with label visual studio. Show all posts
Showing posts with label visual studio. Show all posts

2009-06-04

IronPython and C#, Hosting/Embedding, Part I

As IronPython improves, the way to embed it in other applications is changed too. It is constantly becoming easier and more general. When the C# 4.0 is released, it will be even more easier and useful.

I have written a simple hosting example for the current stable version. I have used IronPython 2.0.1, Visual Studio 2005 SP1, and .NET Framework 2.0 SP2. This example has not been tested with IronPython 2.6 Beta 1 nor IronPython 2.6 for .NET 4.0 Beta 1.

The example code can be seen as follows:

1  namespace IronPythonHostingExample
2  {
3      using System;
4      using IronPython.Hosting;
5      using Microsoft.Scripting.Hosting;
6
7      public class IronPythonStatementHostingExample
8      {
9          public static void ShowDemo()
10         {
11             ScriptRuntimeSetup setup = Python.CreateRuntimeSetup(null);
12             ScriptRuntime runtime = new ScriptRuntime(setup);
13             ScriptEngine engine = Python.GetEngine(runtime);
14             ScriptScope scope = engine.CreateScope();
15             ScriptSource source;
16             string sourceCode = string.Empty;
17             sourceCode += "def factorial(n):" + Environment.NewLine;
18             sourceCode += "    f = 1" + Environment.NewLine;
19             sourceCode += "    i = 1" + Environment.NewLine;
20             sourceCode += "    if (i > n):" + Environment.NewLine;
21             sourceCode += "        return -1" + Environment.NewLine;
22             sourceCode += "    while (i < n):" + Environment.NewLine;
23             sourceCode += "        f = f * i" + Environment.NewLine;
24             sourceCode += "        i = i + 1" + Environment.NewLine;
25             sourceCode += "    return f" + Environment.NewLine;
26             sourceCode += "def add_two_numbers(n1, n2):" + Environment.NewLine;
27             sourceCode += "    return n1+n2" + Environment.NewLine;
28             sourceCode += "for i in range(5):" + Environment.NewLine;
29             sourceCode += "    print i" + Environment.NewLine;
30             sourceCode += "print factorial(5)" + Environment.NewLine;
31             sourceCode += "print factorial(-4)" + Environment.NewLine;
32             sourceCode += "print add_two_numbers(1, 2)" + Environment.NewLine;
33             source = scope.Engine.CreateScriptSourceFromString(sourceCode, Microsoft.Scripting.SourceCodeKind.Statements);
34             source.Execute(scope);
35         }
36     }
37 }



To compile it as a project, you need the following DLLs to be added to your project:
The DLL files can be found in the directory
"C:\Program Files\IronPython 2.0.1".

2009-05-16

Vim and Visual Studio

There is a VIM script, visual_studio.vim which can be found at http://code.google.com/p/vim-visual-studio/. It uses the Python Extensions for Windows to integrate VIM and Visual Studio. It can get the files, solutions and its projects from Visual Studio. You can directly compile the solution from within VIM and you can obviously use the "quickfix". It is not an emulation mode for Visual Studio unlike ViEmu, you will be using VIM again.

The page on vim.org says that you need to have Python 2.4. On my Windows box, I have Visual Studio 2008 and GVIM 7.2, which has been compiled with Python 2.4 support. But, the Python interpreter I am using is Python 2.5, which I keep compatible with the Python interpreter on a production machine. As a result, I had no chance to uninstall Python 2.5 for Python 2.4 and no intention to uninstall GVIM 7.2 for GVIM 7.0.

So, I have found Vim binaries compiled for Python 2.5 and Python 2.6. I have downloaded the zip file and overwritten the executables in my Vim directory. If I type

:version

in Vim, I get the following screens:




Which shows that I have finally a Python 2.5 compatible Vim.

By the way, do not forget to install Python Extensions for Windows if you did not installed yet.

If we make a summary
- We have Visual Studio 2008
- We have Python 2.5
- We have Python Extensions for Windows
- We have GVIM 7.2
- We have Python 2.5 binaries for GVIM
- We have the visual_studio.vim

And finally, I have been able to see and use the following menu: