2009-12-27

Using CPython modules from IronPython

I have IronPython 2.6 and Python 2.6.4 installed on a Windows box.

IronPython's interpreter ipy.exe is not installed on a directory on path, just unzipped in an arbitrary folder.
Python 2.6.4 is installed in C:\Python26 directory.

You can import pure CPython modules from IronPython as follows:

C:\Users\dude\Documents\SharpDevelop Projects\Faman5>ipy
IronPython 2.6 (2.6.10920.0) on .NET 2.0.50727.4927
Type "help", "copyright", "credits" or "license" for more information.
>>> # Try to import the os module.
>>> import os
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named os
>>> # OK, we are sure that the module is can not be imported.
>>> # Now we need to import the sys module to achive our goal.
>>> import sys
>>> # sys.path is the list of directories to look for when importing modules.
>>> sys.path
['.', '.', 'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5', 'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5\\Lib', 'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5\\DLLs', 'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5']
>>> # Add the Cpython path to the sys.path as follows:
>>> sys.path.append("C:\Python26\Lib")
>>> sys.path
['.', '.', 'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5', 'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5\\Lib', 'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5\\DLLs', 'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5', 'C:\\Python26\\Lib']
>>> # Now we are sure that 'C:\\Python26\\Lib' is added.
>>> # Let us try to import the os module again.
>>> import os
>>> os.getcwd()
'C:\\Users\\dude\\Documents\\SharpDevelop Projects\\Faman5'
>>> # Success!

2009-12-03

SNMP OID translator plugin for Vim

I have written an SNMP OID translator plugin for Vim.
By using this plugin, one can translate OIDs directly within Vim without exiting from the editor.
I find it useful for discovering the MIB files, and translating while reading them.

Note that you have to have Net-SNMP installed on your system.
http://www.net-snmp.org/

You can see the detais and download the plugin from the following link:

http://www.vim.org/scripts/script.php?script_id=2881


Here is a screenshot of the plugin.

FORTRAN valid line length plugin for Vim

I have written a Vim plugin to help my FORTRAN coding in Vim.

According to the file extension of a FORTRAN source file, this plugin sets a valid line length. Any characters occurring after this column are considered as error, and are matched so by highlighting.

This plugin is run automatically. But, if you want to use any other valid size for some reason, the commands are available.

You can download the plugin from the following link.
http://www.vim.org/scripts/script.php?script_id=2868

The following commands are available after installing this plugin.

:FORTRANLengthAccordingToExtension
Sets the valid line length according to the file extension.
This command is applied by default.
:FORTRANStandardLength72
The columns occurring after the 72nd character are marked.
:FORTRANCardImageLength80
The columns occurring after the 80th character are marked.
:FORTRANExtendedLength132
The columns occurring after the 132nd character are marked.
:FORTRANRemoveMatching
Removes the matching set by this plugin.

Here is the screenshot of the plugin. (Colorscheme is Molokai).




2009-06-29

Vim compiler plugin for FreeBASIC

I have uploaded a Vim compiler plugin for FreeBASIC.
You can download the fbc.vim at
http://www.vim.org/scripts/script.php?script_id=2691

For the following code:


the
:make
command produces an output as follows:


Traditonally, you can see the all error messages with
:cl

you can jump to next error with
:cn

and you can jump to the previous error message with
:cp


To install it, copy the script into your compiler folder. Assuming standart installations, it should be
C:\Program Files\Vim\vimfiles\compiler\
in Microsoft Windows, and
$HOME/.vim/compiler
in Linux operating systems.

In Vim, the compiler plugins are not automatically triggered unlike file type plugins. To set it whenever you need, use the command
1 :compiler fbc


To set it once and for all if a .bas file is edited, type the following into your VIMRC file:
1 autocmd BufNewFile,BufRead *.bas compiler fbc


If you need a syntax file for the upcoming FreeBASIC 0.21, you can download a syntax file at http://cubaxd.net/?en&p=syntaxfile.

2009-06-05

Delphi Pascal/Object Pascal editing in Vim

Here are a few Vim plugins for Pascal editing.

Compiler
fpc.vim : "Free Pascal Compiler" compiler plugin
It can already be bundled in your Vim. If it is not, you can download it. After opening a .pas file, type
:compiler fpc
Compiler plugins are not loaded automatically in Vim. You'll have to load them explicitly.

Syntax
Syntax file for Borlands Delphi/Kylix
Vim 7 is already bundled with Pascal syntax plugin. But Object Pascal is more than that. To colorize the keywords of Object Pascal, you can use this plugin which is derived from the original Pascal syntax plugin.

Note that although this plugin is a syntax file, if you look in the script, you will see that it also contains color codes. That is why you will not see the color scheme you selected if you have this plugin. To overcome this issue, you can delete the following lines from the delphi.vim :


261 hi pascalObject          ctermfg=white         guifg=white         gui=italic
262 hi pascalFunction        ctermfg=LightRed      guifg=Orange        gui=NONE
263 hi pascalStatement       ctermfg=Green         guifg=Green         gui=NONE
264 hi pascalNumber          ctermfg=LightBlue     guifg=LightBlue     gui=NONE
265 hi pascalByte            ctermfg=LightRed      guifg=LightRed      gui=bold
266 hi pascalString          ctermfg=LightBlue     guifg=LightBlue     gui=NONE
267 hi pascalMatrixDelimiter ctermfg=lightred      guifg=lightred      gui=NONE
268 hi pascalConditional     ctermfg=Green         guifg=Green         gui=NONE
269 hi pascalConstant        ctermfg=white         guifg=white         gui=NONE
270 hi pascalModifier        ctermfg=Green         guifg=Green         gui=NONE
271 hi pascalType            ctermfg=white         guifg=white         gui=NONE
272 hi pascalStructure       ctermfg=Green         guifg=Green         gui=NONE
273 hi pascalRepeat          ctermfg=Green         guifg=Green         gui=NONE
274 hi pascalOperator        ctermfg=Green         guifg=Green         gui=NONE
275 hi pascalAssignment      ctermfg=Magenta       guifg=Magenta       gui=NONE
276 hi pascalComment         ctermfg=LightBlue     guifg=#00A0A0       gui=italic
277 hi pascalLabel           ctermfg=Green         guifg=Green         gui=NONE
278 hi pascalDelimiter       ctermfg=Yellow        guifg=Yellow        gui=NONE
279 hi pascalComparator      ctermfg=Yellow        guifg=Yellow        gui=NONE

I should remind you that the colors are not bad though. Delete them if you want to use the color scheme selected; otherwise leave it as it is, it just works.
If you are curious, here is the screenshot of the colors provided by the syntax plugin:


Also the author of this plugin provides some code to be put on your .vimrc and it is quite useful.
This is the code to be put on your .vimrc:
 1 " Pascal / Delphi
 2 if (1==1) "change to 1==0 to use original syntax
 3   au BufNewFile,BufRead *.pas,*.PAS set ft=delphi
 4 else
 5   au BufNewFile,BufRead *.pas,*.PAS set ft=pascal
 6 endif
 7 " Delphi project file
 8 au BufNewFile,BufRead *.dpr,*.DPR set ft=delphi
 9 " Delphi form file
10 au BufNewFile,BufRead *.dfm,*.DFM set ft=delphi
11 au BufNewFile,BufRead *.xfm,*.XFM set ft=delphi
12 " Delphi package file
13 au BufNewFile,BufRead *.dpk,*.DPK set ft=delphi
14 " Delphi .DOF file = INI file for MSDOS
15 au BufNewFile,BufRead *.dof,*.DOF set ft=dosini
16 au BufNewFile,BufRead *.kof,*.KOF set ft=dosini
17 au BufNewFile,BufRead *.dsk,*.DSK set ft=dosini
18 au BufNewFile,BufRead *.desk,*.DESK set ft=dosini
19 au BufNewFile,BufRead *.dti,*.DTI set ft=dosini
20 " Delphi .BPG = Makefile
21 au BufNewFile,BufRead *.bpg,*.BPG set ft=make|setlocal makeprg=make\ -f\ %

Color Schemes
Delphi color scheme

Turbo color scheme

Borland color scheme

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: