Archive for the ‘IronPython’ Category

  1. Hello, Connector/NET(IronPython) World!

    Posted on 4月 7th, 2013 by cx20

    Connector/NET(IronPython)

    Connector/NET は、.NET ベースの MySQL 接続用 API である。
    以下は IronPython による Connector/NET ライブラリを使用した MySQL への接続例となっている。

    ソースコード(IronPython + Connector/NET + MySQL)

    import clr
    clr.AddReference("System.Data")
    clr.AddReference("MySql.Data")
    from System import *
    from MySql.Data.MySqlClient import *
     
    conStr = "server=localhost;user id=root;password=P@ssW0rd"
    sqlStr = "SELECT 'Hello, Connector/NET World!' AS Message"
     
    con = MySqlConnection(conStr)
    cmd = MySqlCommand(sqlStr, con)
    con.Open()
     
    reader = cmd.ExecuteReader()
    while reader.Read():
        Console.WriteLine( reader.GetName(0) )
        Console.WriteLine( "---------------------" )
        Console.WriteLine( reader.GetValue(0) )
     
    reader.Close()
    con.Close()

    実行方法

    C:¥> ipy Hello.py

    実行結果

    MESSAGE
    ---------------------
    Hello, Connector/NET World!
  2. Hello, ODP.NET(IronPython) World!

    Posted on 2月 12th, 2013 by cx20

    ODP.NET(IronPython)

    ODP.NET(Oracle Data Provider for .NET)は、.NET ベースの Oracle Database 接続用 API である。ODAC(Oracle Data Access Component)と呼ばれるパッケージに含まれる。
    .NET 環境での Oracle Database 用データプロバイダとしては、マイクロソフト社が提供する「Microsoft Oracle Client」とオラクル社が提供する「ODP.NET」があるが、現在、「Microsoft Oracle Client」はマイクロソフト社自身が非推奨としており、今後は ODP.NET の使用が推奨されている。

    データプロバイダ 説明
    System.Data.OracleClient .NET Framework Data Provider for Oracle
    Oracle.DataAccess.Client Oracle Data Provider for .NET

    ソースコード(IronPython + ODP.NET + Oracle)

    import clr
    clr.AddReference("System.Data")
    clr.AddReference("Oracle.DataAccess")
    from System import *
    from Oracle.DataAccess.Client import *
     
    conStr = "Data Source=ORCL;User ID=scott;Password=tiger"
    sqlStr = "SELECT 'Hello, ODP.NET World!' AS Message FROM DUAL"
     
    con = OracleConnection(conStr)
    cmd = OracleCommand(sqlStr, con)
    con.Open()
     
    reader = cmd.ExecuteReader()
    while reader.Read():
        Console.WriteLine( reader.GetName(0) )
        Console.WriteLine( "---------------------" )
        Console.WriteLine( reader.GetValue(0) )
     
    reader.Close()
    con.Close()

    コンパイル方法

    C:¥> ipy Hello.py

    実行結果

    MESSAGE
    ---------------------
    Hello, ODP.NET World!
  3. Hello, ADO.NET(IronPython) World!

    Posted on 2月 11th, 2013 by cx20

    ADO.NET(IronPython)

    ADO.NET(ActiveX Data Objects .NET)は、ADO の後継で .NET ベースの DBMS 接続用 API である。
    .NET データプロバイダを介することで様々な DBMS への接続が可能となっている。
    .NET Framework 標準で使用できる .NET データプロバイダとしては、以下のようなプロバイダがある。

    データプロバイダ名 説明
    System.Data.SqlClient Microsoft SQL Server
    System.Data.OleDb OLE DB
    System.Data.Odbc ODBC
    System.Data.OracleClient Oracle

    ソースコード(IronPython + ADO.NET + OLE DB + Jet データベース)

    import clr
    clr.AddReference("System.Data")
    from System import *
    from System.Data.OleDb import *
     
    conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=hello.mdb;"
    sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
    con = OleDbConnection(conStr)
    cmd = OleDbCommand(sqlStr, con)
    con.Open()
     
    reader = cmd.ExecuteReader()
    while reader.Read():
        Console.WriteLine( reader.GetName(0) )
        Console.WriteLine( "---------------------" )
        Console.WriteLine( reader.GetValue(0) )
     
    reader.Close()
    con.Close()

    ソースコード(IronPython + ADO.NET + OLE DB + ACE データベース)

    import clr
    clr.AddReference("System.Data")
    from System import *
    from System.Data.OleDb import *
     
    conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=hello.accdb;"
    sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
    con = OleDbConnection(conStr)
    cmd = OleDbCommand(sqlStr, con)
    con.Open()
     
    reader = cmd.ExecuteReader()
    while reader.Read():
        Console.WriteLine( reader.GetName(0) )
        Console.WriteLine( "---------------------" )
        Console.WriteLine( reader.GetValue(0) )
     
    reader.Close()
    con.Close()

    ソースコード(IronPython + ADO.NET + SQL Server)

    import clr
    clr.AddReference("System.Data")
    from System import *
    from System.Data.SqlClient import *
     
    conStr = "SERVER=(local);DATABASE=master;UID=sa;PWD=P@ssW0rd;"
    sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
    con = SqlConnection(conStr)
    cmd = SqlCommand(sqlStr, con)
    con.Open()
     
    reader = cmd.ExecuteReader()
    while reader.Read():
        Console.WriteLine( reader.GetName(0) )
        Console.WriteLine( "---------------------" )
        Console.WriteLine( reader.GetValue(0) )
     
    reader.Close()
    con.Close()

    ソースコード(IronPython + ADO.NET + Oracle)

    import clr
    clr.AddReference("System.Data")
    clr.AddReference("System.Data.OracleClient")
    from System import *
    from System.Data.OracleClient import *
     
    conStr = "Data Source=ORCL;User ID=scott;Password=tiger"
    sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message FROM DUAL"
     
    con = OracleConnection(conStr)
    cmd = OracleCommand(sqlStr, con)
    con.Open()
     
    reader = cmd.ExecuteReader()
    while reader.Read():
        Console.WriteLine( reader.GetName(0) )
        Console.WriteLine( "---------------------" )
        Console.WriteLine( reader.GetValue(0) )
     
    reader.Close()
    con.Close()

    ソースコード(IronPython + ADO.NET + ODBC + MySQL)

    import clr
    clr.AddReference("System.Data")
    from System import *
    from System.Data.Odbc import *
     
    conStr = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;UID=root;PWD=P@ssW0rd"
    sqlStr = "SELECT 'Hello, ADO.NET World!' AS Message"
     
    con = OdbcConnection(conStr)
    cmd = OdbcCommand(sqlStr, con)
    con.Open()
     
    reader = cmd.ExecuteReader()
    while reader.Read():
        Console.WriteLine( reader.GetName(0) )
        Console.WriteLine( "---------------------" )
        Console.WriteLine( reader.GetValue(0) )
     
    reader.Close()
    con.Close()

    実行方法(64bit OLE DB 使用時)

    C:¥> ipy64 Hello.py

    実行方法(上記以外)

    C:¥> ipy Hello.py

    実行結果

    Message
    ---------------------
    Hello, ADO.NET World!
  4. Hello, COM(IronPython) World!

    Posted on 2月 1st, 2013 by cx20

    COM(IronPython)

    COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
    COM を用いて開発された部品であれば言語を問わず利用することができる。
    以下は IronPython による COM クライアントの例となっている。

    ソースコード(実行時バインディング)

    import System
    from System.Reflection import BindingFlags
    from System.Runtime.InteropServices import Marshal
     
    objType = System.Type.GetTypeFromProgID("Shell.Application")
    shell = System.Activator.CreateInstance(objType)
    param = System.Array[System.Object]([ 0, "Hello, COM(IronPython) World!", 0, 36 ])
    folder = shell.GetType().InvokeMember( "BrowseForFolder", BindingFlags.InvokeMethod, None, shell, param )
    if folder != None:
        Marshal.ReleaseComObject(folder)
    Marshal.ReleaseComObject(shell)

    コンパイル方法(実行時バインディング)

    C:¥> ipy Hello.py

    実行結果

    +----------------------------------------+
    |Browse For Folder                    [X]|
    +----------------------------------------+
    | Hello, COM(IronPython) Wolrd!          |
    |                                        |
    | +------------------------------------+ |
    | |[Windows]                           | |
    | | +[addins]                          | |
    | | +[AppCompat]                       | |
    | | +[AppPatch]                        | |
    | | +[assembly]                        | |
    | |     :                              | |
    | |     :                              | |
    | |     :                              | |
    | +------------------------------------+ |
    | [Make New Folder]    [  OK  ] [Cancel] |
    +----------------------------------------+
  5. Hello, Win32 API(IronPython) World!

    Posted on 6月 18th, 2012 by cx20

    Win32 API(IronPython)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は IronPython にて clrtype ライブラリを使用した Win32 API の呼出し例である。

    ソースコード

    import clr
    import clrtype
    import System
    from System.Reflection import BindingFlags
     
    class Win32(object):
        __metaclass__ = clrtype.ClrClass
     
        from System.Runtime.InteropServices import DllImportAttribute
        DllImport = clrtype.attribute(DllImportAttribute)
     
        @staticmethod
        @DllImport("user32.dll")
        @clrtype.accepts(System.IntPtr, System.String, System.String, System.UInt32)
        @clrtype.returns(System.Int32)
        def MessageBox(hwnd, text, caption, type): raise RuntimeError("Runtime Error")
     
    Win32.MessageBox(System.IntPtr.Zero, "Hello, Win32 API(IronPython) World!", "Hello, World!", 0)

    実行方法

    C:¥> ipy hello.py

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(IronPython) World!
    ---------------------------
    OK   
    ---------------------------
  6. Hello, Windows Forms(IronPython) World!

    Posted on 6月 8th, 2012 by cx20

    Windows Forms(IronPython)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は IronPython における Windows フォーム の例となっている。

    ソースコード

    import clr
    clr.AddReference("System.Windows.Forms")
    clr.AddReference("System.Drawing")
    from System.Windows import Forms
    from System import Drawing
     
    class HelloForm(Forms.Form):
        def __init__(self):
            self.Size = Drawing.Size( 640, 480 )
            self.Text = 'Hello, World!'
            label1 = Forms.Label()
            label1.Size = Drawing.Size( 320, 20 )
            label1.Text = 'Hello, Windows Forms(IronPython) World!'
            self.Controls.Add( label1 )
     
    if __name__ == '__main__':
        form = HelloForm()
        Forms.Application.Run(form)

    実行方法

    C:¥> ipy Hello.py

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(IronPython) World!   |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  7. Hello, IronPython World!

    Posted on 1月 20th, 2012 by cx20

    IronPython

    IronPython はオブジェクト指向スクリプト言語 Python の .NET 実装である。
    Python の構文が使える他、.NET Framework のライブラリが利用できる。
    名前の由来は「料理の鉄人(The Battle of Iron Chef)」から。CPython vs IronPython をかけたシャレらしい。
    Jython(Python の Java 実装)の作者、Jim Hugunin 氏によって開発された。
    C# 同様に、UNIX 環境向けの .NET Framework 互換プロジェクト「Mono」により他の OS でも動作させることができる。

    ソースコード

    #!/usr/bin/env ipy
    print "Hello, IronPython World!"

    ソースコード(.NET ライブラリを使用した場合)

    #!/usr/bin/env ipy
    from System import *
    Console.WriteLine("Hello, IronPython World!")

    実行方法(スクリプトファイルを指定して実行)

    $ ipy hello.py

    実行方法(実行権限を付与して実行)

    $ chmod +x hello.py
    $ ./hello.py

    実行結果

    Hello, IronPython World!