一个能够让你使用大括号写Python代码的Python库
Project description
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 |
- 注意 : 所有
def和class前的内容都会被解析为装饰器,支持多重装饰器 - 注意 : 语法不支持列表推导式及字典推导式
扩展功能
如果在使用时加上-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!")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
curlypython-0.1.0.tar.gz
(18.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file curlypython-0.1.0.tar.gz.
File metadata
- Download URL: curlypython-0.1.0.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9a2eb647c0d444a7e7c49899094483c581f8ad6a7f6d038704dcce2945b54345
|
|
| MD5 |
2c41620ace59bb72c596b90d24ddb6c6
|
|
| BLAKE2b-256 |
81479069d3ea8cbb7a3f1aeb73d2ebac35ee44090b8b4ed22be5d3d151aedb4d
|
File details
Details for the file curlypython-0.1.0-py3-none-any.whl.
File metadata
- Download URL: curlypython-0.1.0-py3-none-any.whl
- Upload date:
- Size: 20.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8e4d36a52ff5bd439730c6ea4eacebcd342e11d443937e9c1a964baee5ae4268
|
|
| MD5 |
be1765edbd530b189abd419311535b7e
|
|
| BLAKE2b-256 |
7a0aa277e3fa1cb66ff839a469421ddeebc8bf6fc89122e4ee29a5d9e272f81f
|