V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
MarioLuisGarcia
V2EX  ›  分享创造

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

  •  
  •   MarioLuisGarcia · 2017-11-21 16:40:41 +08:00 · 2824 次点击
    这是一个创建于 2340 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    类似于自己编译自己解释?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5357 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 06:45 · PVG 14:45 · LAX 23:45 · JFK 02:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.