简化自修改代码

2024-03-28 11:31:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一个在Python2.7上运行的自修改代码,代码有点大,我想知道是否有办法让代码更简单一点,我考虑过了,但没有找到任何答案。。。如果有人能帮我一把,我会很感激的,但不管怎样,这是密码

import sys
x = ['num = 0\n', 'from threading import *\n', 'import sys\n', 'def change(b, line=1):\n', '\tline -= 1\n', '\tf = open(sys.argv[0])\n', '\tg = f.readlines()\n', '\tlines = []\n', '\tfor i in g:\n', '\t\tlines.append(i)\n', '\tlines[line] = b+"\\n"\n', "\tf = open(sys.argv[0], 'w')\n", '\tfor each in lines:\n', '\t\tf.write(each)\n', '\tf.close()\n', '\t\n', 'def addline(b):\n', '\tf = open(sys.argv[0])\n', '\tg = f.readlines()\n', '\tlines = []\n', '\tfor i in g:\n', '\t\tlines.append(i)\n', '\tlines.append("\\n"+b)\n', "\tf = open(sys.argv[0], 'w')\n", '\tfor each in lines:\n', '\t\tf.write(each)\n', '\tf.close()\n', '\t\n', 'def delline(b = 10):\n', '\tf = open(sys.argv[0])\n', '\tg = f.readlines()\n', '\tlines = []\n', '\tfor i in g:\n', '\t\tlines.append(i)\n', '\tleng = len(lines) - b\n', '\tlines = lines[:leng]\n', "\tf = open(sys.argv[0], 'w')\n", '\tfor each in lines:\n', '\t\tf.write(each)\n', '\tf.close()\n', '\t\t\n', 'def showfile():\n', '\tcheck = num\n', '\tf = open(sys.argv[0])\n', '\tg = f.readlines()\n', '\twhile num != check + 1:\n', '\t\tfor i in g:\n', '\t\t\tprint i\n', '\t\n', '\t\n', 'if num == 0:\n', '\tThread(target=showfile).start()\n', '\tf = open("values.py", \'w\')\n', '\twriter = [\'linestowrite1 = [\\\'import os\\\\n\\\', \\\'from time import *\\\\n\\\',\\\'os.system("cls")\\\\n\\\', \\\'print("hello...")\\\\n\\\', \\\'sleep(2)\\\\n\\\', \\\'os.system("cls")\\\\n\\\', \\\'print("I am a program that is able to evolve by itself")\\\\n\\\', \\\'sleep(4)\\\\n\\\', \\\'os.system("cls")\\\\n\\\', \\\'print("everytime someone opens me, I evolve")\\\\n\\\', \\\'sleep(2)\\\\n\\\', \\\'os.system("cls")\\\\n\\\',\\\'change("num = 2")\\\\n\\\' \\\'raw_input("press ENTER to exit")\\\\n\\\']\\n\', \'linestowrite2 = [\\\'from time import *\\\\n\\\', \\\'f = open(sys.argv[0])\\\\n\\\', \\\'g = f.readlines()\\\\n\\\', \\\'leng = len(g)\\\\n\\\', \\\'sleep(1)\\\\n\\\', \\\'print("CURRENT LENGTH : "+str(leng))\\\\n\\\', \\\'sleep(2)\\\\n\\\', \\\'print("Extending length to : "+str(leng+20))\\\\n\\\', \\\'sleep(3)\\\\n\\\', \\\'change("num = 5")\\\\n\\\', \\\'\\\\t\\\']\']\n', '\tfor i in writer:\n', '\t\tf.write(i)\n', '\tf.close()\n', '\timport values\n', '\ttowrite = values.linestowrite1\n', '\tfor i in towrite:\n', '\t\taddline(i)\n', '\tchange("num = 1")\n', '\tnum += 1\n', 'elif num == 2:\n', '\tThread(target=showfile).start()\n', '\tdelline(26)\n', '\tchange("num = 3")\n', '\tnum += 1\n', '\thorse69\n', 'elif num == 3:\n', '\tThread(target=showfile).start()\n', '\timport values\n', '\ttowrite = values.linestowrite2\n', '\tfor i in towrite:\n', '\t\taddline(i)\n', '\tchange("num = 4")\n', '\tnum += 1\n', 'elif num == 5:\n', '\tThread(target=showfile).start()\n', '\tdelline(21)\n', '\tchange("num = 6")\n', '\tnum += 1\n', '\thorse69\n', 'elif num == 6:\n', '\tprint("EVOLUTION COMPLETE")\n', '\traw_input()\n']
f = open(sys.argv[0], 'w')
for i in x:
    f.write(i)
f.close()

提前谢谢你


Tags: inimportclosetfsyssleepopennum
1条回答
网友
1楼 · 发布于 2024-03-28 11:31:16

不需要列表,只需使用字符串:

import sys

newcode = 'num = 0\nfrom threading import *\nimport sys\ndef change(b, line=1):\n\tline -= 1\n\tf = open(sys.argv[0])\n\tg = f.readlines()\n\tlines = []\n\tfor i in g:\n\t\tlines.append(i)\n\tlines[line] = b+"\\n"\n\tf = open(sys.argv[0], \'w\')\n\tfor each in lines:\n\t\tf.write(each)\n\tf.close()\n\t\ndef addline(b):\n\tf = open(sys.argv[0])\n\tg = f.readlines()\n\tlines = []\n\tfor i in g:\n\t\tlines.append(i)\n\tlines.append("\\n"+b)\n\tf = open(sys.argv[0], \'w\')\n\tfor each in lines:\n\t\tf.write(each)\n\tf.close()\n\t\ndef delline(b = 10):\n\tf = open(sys.argv[0])\n\tg = f.readlines()\n\tlines = []\n\tfor i in g:\n\t\tlines.append(i)\n\tleng = len(lines) - b\n\tlines = lines[:leng]\n\tf = open(sys.argv[0], \'w\')\n\tfor each in lines:\n\t\tf.write(each)\n\tf.close()\n\t\t\ndef showfile():\n\tcheck = num\n\tf = open(sys.argv[0])\n\tg = f.readlines()\n\twhile num != check + 1:\n\t\tfor i in g:\n\t\t\tprint i\n\t\n\t\nif num == 0:\n\tThread(target=showfile).start()\n\tf = open("values.py", \'w\')\n\twriter = [\'linestowrite1 = [\\\'import os\\\\n\\\', \\\'from time import *\\\\n\\\',\\\'os.system("cls")\\\\n\\\', \\\'print("hello...")\\\\n\\\', \\\'sleep(2)\\\\n\\\', \\\'os.system("cls")\\\\n\\\', \\\'print("I am a program that is able to evolve by itself")\\\\n\\\', \\\'sleep(4)\\\\n\\\', \\\'os.system("cls")\\\\n\\\', \\\'print("everytime someone opens me, I evolve")\\\\n\\\', \\\'sleep(2)\\\\n\\\', \\\'os.system("cls")\\\\n\\\',\\\'change("num = 2")\\\\n\\\' \\\'raw_input("press ENTER to exit")\\\\n\\\']\\n\', \'linestowrite2 = [\\\'from time import *\\\\n\\\', \\\'f = open(sys.argv[0])\\\\n\\\', \\\'g = f.readlines()\\\\n\\\', \\\'leng = len(g)\\\\n\\\', \\\'sleep(1)\\\\n\\\', \\\'print("CURRENT LENGTH : "+str(leng))\\\\n\\\', \\\'sleep(2)\\\\n\\\', \\\'print("Extending length to : "+str(leng+20))\\\\n\\\', \\\'sleep(3)\\\\n\\\', \\\'change("num = 5")\\\\n\\\', \\\'\\\\t\\\']\']\n\tfor i in writer:\n\t\tf.write(i)\n\tf.close()\n\timport values\n\ttowrite = values.linestowrite1\n\tfor i in towrite:\n\t\taddline(i)\n\tchange("num = 1")\n\tnum += 1\nelif num == 2:\n\tThread(target=showfile).start()\n\tdelline(26)\n\tchange("num = 3")\n\tnum += 1\n\thorse69\nelif num == 3:\n\tThread(target=showfile).start()\n\timport values\n\ttowrite = values.linestowrite2\n\tfor i in towrite:\n\t\taddline(i)\n\tchange("num = 4")\n\tnum += 1\nelif num == 5:\n\tThread(target=showfile).start()\n\tdelline(21)\n\tchange("num = 6")\n\tnum += 1\n\thorse69\nelif num == 6:\n\tprint("EVOLUTION COMPLETE")\n\traw_input()\n'

with open(sys.argv[0], 'w') as f:
    f.write(newcode)

对内部代码应用相同的原则可能有助于缩短代码。在

相关问题 更多 >