How to convert my code from C# to Python?

Question:

How can I convert this code from C# to Python to be run on IronPython?

I don’t have any experience with Python.

using System;
using Baz;

namespace ConsoleApplication
{
  class Program
  {
    static void Main()
    {
        Portal foo = new Portal("Foo"); 
        Agent bar = new Agent("Bar");

        foo.Connect("127.0.0.1", 1234); 
        foo.Add(bar);

        bar.Ready += new Agent.ReadyHandler(bar_Ready);               
    }

    static void bar_Ready(object sender, string msg)
    {    
       Console.WriteLine(msg.body);  
    }
}
}
Asked By: Ali

||

Answers:

Answered By: fuentesjr

Instantiation doesn’t require a type definition. Methods called the same, assign delegates directly. The previous answer is absolutely right, you’ll need a lot more context in order to “convert” a C# application to Python; it’s more than just syntax.

foo = Portal("Foo")

bar = Agent("bar")

foo.Connect("ip", 1234)

foo.Add(bar)

bar.Ready = bar_Ready

def bar_Ready(sender, msg):

    print msg.body
Answered By: t3rse

Or if you’re feeling really lazy, there’s a C# to Python converter on developer fusion!

Answered By: James Crowley

In case someone else has this question SharpDevelop has a conversion utility to convert between C# and IronPython, VB.NET or Boo
http://community.sharpdevelop.net/blogs/mattward/archive/2009/05/11/ConvertingCSharpVBNetCodeToIronPython.aspx

Answered By: Automate
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.