# pytocs
[![Join the chat at https://gitter.im/uxmal/pytocs](https://badges.gitter.im/uxmal/pytocs/Lobby.svg)](https://gitter.im/uxmal/pytocs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
Converts Python source to C#
pytocs is a command line tool I wrote as a hobby project to convert Python source code to C#.
I've uploaded it here in case someone finds it useful.
## How to run pytocs
Just git clone the project, and use Visual Studio or MSBuild to compile the `pytocs.sln` file. If you're unable or unwilling to build pytocs from source, the latest continuous integration build
is available at [appveyor.com](https://ci.appveyor.com/project/uxmal/pytocs/build/artifacts).
## 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:
```Python
# Some code below
def hello():
print "Hello World";
```
Translates to:
```C#
public static class hello {
public static object hello() {
Console.WriteLine("Hello World");
}
}
```
A more ambitious sample:
```Python
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, generating linQ output.
def apply_map(mapfn, filterfn):
return [mapfn(n) for n in lst if filterfn]
```
Translates to:
```C#
using System;
using System.Linq;
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 new 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 (from n in lst
where filterfn
select mapfn(n)).ToList();
}
}
}
```
## 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.
没有合适的资源?快使用搜索试试~ 我知道了~
Converts Python source to C#.zip
共250个文件
cs:201个
csproj:7个
bat:6个
需积分: 5 0 下载量 95 浏览量
2023-12-31
11:12:26
上传
评论
收藏 725KB ZIP 举报
温馨提示
Converts Python source to C#
资源推荐
资源详情
资源评论
收起资源包目录
Converts Python source to C#.zip (250个子文件)
FolderConverterTab.axaml 2KB
SnippetConverterTab.axaml 1KB
MainWindow.axaml 737B
App.axaml 513B
version.bat 399B
release.bat 363B
pytocs-gui.bat 25B
pytocs.bat 21B
pytocs-gui.bat 18B
pytocs.bat 14B
nuget.config 190B
App.config 178B
packages.config 134B
Builtins.cs 121KB
Parser.cs 94KB
TypeCollector.cs 52KB
Lexer.cs 44KB
Exp.cs 38KB
StatementTranslatorTests.cs 36KB
ExpTranslator.cs 34KB
TorchUtil.cs 33KB
StatementTranslator.cs 32KB
Analyzer.cs 28KB
ParserTests.cs 26KB
CSharpExpressionWriter.cs 26KB
ExpTranslatorTests.cs 25KB
CodeGenerator.cs 23KB
NameScope.cs 21KB
AnalyzerTests.cs 20KB
LexerTests.cs 17KB
IntrinsicTranslator.cs 16KB
Statement.cs 14KB
LocalVariableGenerator.cs 14KB
ModuleTranslatorTests.cs 14KB
CSharpTypeWriter.cs 13KB
Resources2.Designer.cs 13KB
Resources.Designer.cs 13KB
ParserAcceptanceTests.cs 11KB
PyStatementWriter.cs 11KB
netstandard.cs 11KB
TypeReferenceTranslator.cs 10KB
TypePrinter.cs 9KB
FakeFileSystem.cs 8KB
CSharpStatementWriter.cs 8KB
Program.cs 8KB
MethodGenerator.cs 7KB
LocalVariableGeneratorTests.cs 7KB
FunType.cs 7KB
CommentFilter.cs 7KB
UnionType.cs 7KB
ModuleTranslator.cs 7KB
Binding.cs 7KB
IExpVisitor.cs 6KB
IFileSystem.cs 6KB
Translator.cs 6KB
AstCache.cs 6KB
ExpNameDiscovery.cs 6KB
Resources.Designer.cs 5KB
FolderConverterTab.axaml.cs 5KB
IndentingTextWriter.cs 5KB
struct.cs 4KB
Progress.cs 4KB
ICodeExpressionVisitor.cs 4KB
FunctionDef.cs 4KB
ClassType.cs 4KB
SymbolGenerator.cs 4KB
ConversionUtils.cs 4KB
Token.cs 4KB
CommentFilterTests.cs 4KB
ILogger.cs 3KB
DataTypeFactory.cs 3KB
SnippetConverterTab.axaml.cs 3KB
ListType.cs 3KB
TupleType.cs 3KB
DataType.cs 3KB
IStatementVisitor.cs 3KB
DirectoryWalker.cs 3KB
CSharpCodeProvider.cs 3KB
DictType.cs 3KB
Argument.cs 3KB
Struct.Tests.cs 3KB
ICodeStatementVisitor.cs 3KB
ConstructorGenerator.cs 2KB
CSharpUnitWriter.cs 2KB
StructTranslator.cs 2KB
CodeParameterDeclarationExpression.cs 2KB
CodeTypeReference.cs 2KB
Parameter.cs 2KB
ModuleType.cs 2KB
DelegateLogger.cs 2KB
LambdaBodyGenerator.cs 2KB
DictionaryUtils.cs 2KB
Op.cs 2KB
CodeTryCatchFinallyStatement.cs 2KB
TypeStack.cs 2KB
InstanceType.cs 2KB
CodeCatchClause.cs 2KB
Statistics.cs 2KB
AliasedExp.cs 2KB
CodeConditionStatement.cs 2KB
共 250 条
- 1
- 2
- 3
资源评论
暮苍梧~
- 粉丝: 41
- 资源: 258
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功