Skip to main content

CurlyPython

用大括号风格写 Python 的奇妙小工具

这是什么?

一个让 Python 支持大括号语法的转换工具:

这样写 Python

def main() {
    for i in range(5) {
        if (i % 2 == 0) {
            print(f"{i} is even");
        } else {
            print(f"{i} is odd");
        }
    }
}

class Calculator {
    def __init__(self, value = 0) {
        self._value = value;
    }
    
    def add(self, x) {
        self._value += x;
        return self;
    }
    
    staticmethod def helper() {
        return "I'm a static method";
    }

    property def value(self) {
        return self._value;
    }
}

甚至这样

def main() {for i in range(5) {if (i % 2 == 0) {print(f"{i} is even");} else {print(f"{i} is odd");}}}class Calculator {def __init__(self, value = 0) {self.value = value or 0}def add(self, x) {self.value += x;return self;}staticmethod def helper() {return "I'm a static method";}}

特色功能

  • ✅ 大括号自动转缩进
  • ✅ 静态方法支持
  • ✅ 装饰器支持
  • ✅ 增量赋值支持
  • 🚧 更多语法糖陆续添加...

注意

这只是一个趣味项目:

  • 不是真正的编译器
  • 不追求完整语法兼容
  • 核心是让 Python 支持大括号写法

适合:

  • 喜欢大括号的程序员
  • 想换个风格写 Python

安装使用

# 运行文件
curpy my_script.curpy

# 解析为 python
curpy my_script.curpy -o my_script.py

语法对照

CurlyPython Python
def foo() { ... } def foo(): ...
if (x) { ... } if x: ...
class Bar { ... } class Bar: ...
decorator class/def @decorator + class/def
i++ / i-- i += 1 / i -= 1
else if / elif elif
  • 注意 : 所有defclass前的内容都会被解析为装饰器,支持多重装饰器
  • 注意 : 语法不支持列表推导式字典推导式

扩展功能

如果在使用时加上-E参数,则开启扩展功能

curpy my_script.curpy -E

扩展功能语法

CurlyPython Python
static def @staticmethod
virtual def @abstractmethod
struct class @dataclass
MyClass::method() MyClass.method()
  • 注意 : 解析器会自动处理导入

示例

class MyClass() {
    virtual static def hello() {
        print("Hello, world!");
    }
}

转换后

from abc import abstractmethod

class MyClass() :
    @abstractmethod
    @staticmethod
    def hello() :
        print("Hello, world!")

Release files for curlypython 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for curlypython 0.1.0
File Size Uploaded
curlypython-0.1.0.tar.gz 18.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for curlypython 0.1.0
File Interpreter ABI Platform
curlypython-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 38.9 kB

Release files / curlypython-0.1.0.tar.gz

Download URL curlypython-0.1.0.tar.gz
Size 18.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9a2eb647c0d444a7e7c49899094483c581f8ad6a7f6d038704dcce2945b54345
BLAKE2b-256 checksum
How to use checksums
81479069d3ea8cbb7a3f1aeb73d2ebac35ee44090b8b4ed22be5d3d151aedb4d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release files / curlypython-0.1.0-py3-none-any.whl

Download URL curlypython-0.1.0-py3-none-any.whl
Size 20.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8e4d36a52ff5bd439730c6ea4eacebcd342e11d443937e9c1a964baee5ae4268
BLAKE2b-256 checksum
How to use checksums
7a0aa277e3fa1cb66ff839a469421ddeebc8bf6fc89122e4ee29a5d9e272f81f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page