将你输入的任何话自动转化成代码的 Python 小玩具,适合吐槽、装 X、示爱(误)

2017-11-21 16:40:41 +08:00
 MarioLuisGarcia

Coding environment: Python3.6

import unicodedata as ud

code_doc = """
import unicodedata as ud

init_str = ''
for unicode_name in %r:
    unicode = ud.lookup(unicode_name)
    init_str += unicode

print(init_str)
"""

def easy_coding(string):
    """将你输入的任何句子(字符串)转化成更 fancy 的 python 代码!"""
    name_list = []
    for i in string:
        name_list.append(ud.name(i))

    print(code_doc % name_list)

使用方法:

将源代码复制到空白 py 文件并用 Python3 运行后,调用 easy_coding 函数,并将你想要转化的字符串作为参数传进去。
easy_coding 将自动打印一段可再次 implicitly 打印该字符串的代码。将该打印出来的代码复制到另一个空白 py 文件并用 Python3 运行后,可以打印出你刚才输入的字符串。 (^__^)

#示例:
In [4]: easy_coding('兽人永不搞基!')

import unicodedata as ud
init_str = ''
for unicode_name in ['CJK UNIFIED IDEOGRAPH-517D', 'CJK UNIFIED IDEOGRAPH-4EBA', 'CJK UNIFIED IDEOGRAPH-6C38', 'CJK UNIFIED IDEOGRAPH-4E0D', 'CJK UNIFIED IDEOGRAPH-641E', 'CJK UNIFIED IDEOGRAPH-57FA', 'FULLWIDTH EXCLAMATION MARK']:
    unicode = ud.lookup(unicode_name)
    init_str += unicode

print(init_str)
2835 次点击
所在节点    分享创造
6 条回复
cy97cool
2017-11-21 16:57:49 +08:00
感谢分享,感觉 ctf 的 misc 题又有得出了
MarioLuisGarcia
2017-11-21 18:29:45 +08:00
@cy97cool ctf 的 misc 题是什么东东啊?
jadec0der
2017-11-21 18:44:27 +08:00
就是那种给个线索找谜底的黑客竞赛
xubeiyan
2017-11-21 20:23:04 +08:00
不如直接生成某个 py 文件……而且丝毫不考虑 cmd 下换行的问题……(摊手
MarioLuisGarcia
2017-11-22 08:26:08 +08:00
@xubeiyan 好建议,受教啦!
goodryb
2017-11-22 10:43:30 +08:00
所以这和 print("兽人永不搞基!") 有啥区别

类似于自己编译自己解释?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/408313

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX