无代码
No Code

原始链接: https://github.com/lemonyte/no-code

## 无代码:一个Python包,用于…什么也没有 “无代码”Python包可以将你的Python代码转换为功能上等效的“无代码”——本质上,一个仍然*运行*你的程序但看起来像是空代码的文件。 安装需要Python 3.10+和一个虚拟环境(使用`pip install git+https://github.com/lemonyte/no-code`)。核心命令`no_code <文件名>`将Python文件转换为其“无代码”等效版本,并在顶部标记为`# coding: no`。运行这个“无代码”文件会执行原始程序。 你可以使用`yes_code <文件名>`恢复。该包还包括函数`no_code.nothing()`和`no_code.something()`,用于在“无”和“有”状态之间转换数据。它也接受通过stdin的输入。 本质上,它是一个受类似JavaScript项目启发,展示了一种独特的代码表示方法的趣味工具。

一个黑客新闻的讨论围绕着 GitHub 项目“No Code”(github.com/lemonyte),它巧妙地允许用户“不提交任何更改”。该项目因其巧妙的概念而受到关注,一位评论员指出它的艺术价值而非技术难度。 几位用户指出它受到了 Kelsey Hightower 早期“nocode”仓库的启发,该仓库在其 issue 区包含了一些幽默的互动。然而,这个版本将 Python 转换为可运行的空白字符。该项目自动在云端运行,并使用零宽度 UTF 字符巧妙地编码代码,导致一些人最初怀疑它什么也没做。 讨论还涉及向此类项目贡献的挑战,即在不增加不必要的复杂性的情况下进行贡献,并且也分享了一个 Perl 的等效版本。最终,它被呈现为一个轻松有趣的个人项目。
相关文章

原文

The best way to write code is to write no code at all.

Writing code that does nothing is great, but sometimes you still want your code to do something. This package allows you to write nothing, and still have your code do something.

Tip

It is highly recommended to use a virtual environment for proper installation of the no.pth file.

python -m pip install git+https://github.com/lemonyte/no-code

Python 3.10 or a newer version is required.

This package is intended to be used as a command-line tool.

Important

Make sure you are in an activated virtual environment. Using uv run tends to break this package.

To turn your code into no code:

$ cat some_code.py
print("Hello, world! 😁")

$ no_code some_code.py > no_code.py

$ cat no_code.py
# coding: no
​‌‌‌​​​​​‌‌‌​​‌​​‌‌​‌​​‌​‌‌​‌‌‌​​‌‌‌​‌​​​​‌​‌​​​​​‌​​​‌​​‌​​‌​​​​‌‌​​‌​‌​‌‌​‌‌​​​‌‌​‌‌​​​‌‌​‌‌‌‌​​‌​‌‌​​​​‌​​​​​​‌‌‌​‌‌‌​‌‌​‌‌‌‌​‌‌‌​​‌​​‌‌​‌‌​​​‌‌​​‌​​​​‌​​​​‌​​‌​​​​​‌‌‌‌​​​​‌​​‌‌‌‌‌‌​​‌‌​​​‌​​​​​​‌​​‌​​​‌​​​‌​‌​​‌​​​​‌​‌​

You can then run no code as usual:

$ python no_code.py
Hello, world! 😁

If, for some reason, you need to turn no code back into Python code:

$ yes_code no_code.py > some_code.py

$ cat some_code.py
print("Hello, world! 😁")

If you get SyntaxError: invalid syntax, make sure the line # coding: no is present at the top of your script.

If you get SyntaxError: encoding problem: no, follow the steps below to make sure the no encoding is registered on startup:

  1. Run SITE_PACKAGES=$(python -c 'import sysconfig; print(sysconfig.get_path("purelib"))') to get the path to the Python site-packages directory.
  2. Run echo "import no_code" > "$SITE_PACKAGES/no.pth" to register the no encoding on startup.

Note

If using PowerShell, use $SITE_PACKAGES instead of SITE_PACKAGES to assign the variable.

This issue is commonly encountered when installing no-code into the user site-packages.

Not satisfied with no code? The package also provides two functions to easily convert between something and nothing.

  • no_code.nothing(): Transform something (a string or bytes-like object) into nothing.
  • no_code.something(): Transform nothing into something (a string or bytes-like object).

The commands no_code and yes_code also accept input from stdin if no file is provided.

$ echo "print(bool(1))" | no_code | yes_code | python
True

Inspired by Kelsey Hightower, based on INVISIBLE.js by Martin Kleppe.

MIT License

联系我们 contact @ memedata.com