Giter Site home page Giter Site logo

pytocs's Introduction

pytocs

Converts Python source to C#

pytocs is a hobby project I wrote to convert Python source code to C#. I've uploaded it here in case someone finds it useful.

Examples

To convert a Python file, hand it to pytocs:

pytocs foo.py

To convert all files in a directory (recursively), use the -r parameter:

pytocs -r 

The following python fragment:

# Some code below
def hello():
   print "Hello World";

Translates to:

public static class hello {

    public static object hello() {
	    Console.WriteLine("Hello World");
    }
}

A more ambitious sample:

class MyClass:
    # member function calling other function
    def calc_sum(self, x, y):
       return self.frobulate('+', x, y)

    # arithmetic and exceptions
    def frobulate(self, op, x, y):
        if op == '+':
            return x + y
        elif op == '-':
            return x - y
        else:
            raise ValueError("Unexpected argument %s" % op)

    # static method using for..in and enumerate, with tuple comprehension
    def walk_list(lst):
        for (i,strg) in lst.iterate():
            print "index: %d strg: %s\n" % (i, strg)
 
    # list comprehension
    def apply_map(mapfn, filterfn):
        return [mapfn(n) for n in lst if filterfn]

Translates to:

using System;

public static class readme {
    
    public class MyClass {
        
        // member function calling other function
        public virtual object calc_sum(object x, object y) {
            return this.frobulate("+", x, y);
        }
        
        // arithmetic and exceptions

        public virtual object frobulate(object op, object x, object y) {
            if (op == "+") {
                return x + y;
            } else if (op == "-") {
                return x - y;
            } else {
                throw ValueError(String.Format("Unexpected argument %s", op));
            }
        }
        
        // static method using for..in and enumerate, with tuple comprehension
        public static object walk_list(object lst) {
            foreach (var _tup_1 in lst.iterate()) {
                var i = _tup_1.Item1;
                var strg = _tup_1.Item2;
                Console.WriteLine(String.Format("index: %d strg: %s\n", i, strg));
            }
        }
        
        // list comprehension
        public static object apply_map(object mapfn, object filterfn) {
            return lst.Where(n => filterfn).Select(n => mapfn(n));
        }
    }
}

Roadmap

The next big items on the list are:

  • Take the types that are inferred and apply them to the code to get away from everything being object.
  • Place local variable declarations at the statement that dominates all definitions of said variables.

pytocs's People

Contributors

f1x3d avatar spolezhaev avatar uxmal avatar yuhan0 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.