自动化助手
dracodes的Python项目详细描述
dracode
加快脚本编写速度的单行解决方案
安装:
pip3 install dracodes
示例:
写入文件:
aString='A normal phrase'aList=['jooj','cleyton','not foo','not bar']aListOfNumbers=list(range(10))aMixedList=['hello',123456789,['nested1','nested2'],{'1':2}]aDictionary={'key1':1,'key2':'valu2','key3':[3]}withopen('path/out.txt')asoutFile:outFile.write(f'{aString}\n')foriteminaList:outFile.write(f'{line}\n')foriteminaListOfNumbers:outFile.write(f'{str(line)}\n')foriteminaMixedList:ifisinstance(item,str):outFile.write(f'{line}\n')ifisistance(item,list):forsubIteminitem:# Ok, we get it
懒惰的解决方案:
fromdracodesimportsaveOutputoutput=tuple(aString,aList,aListOfNumbers,aMixedList,aDictionary)saveOutput('path/out.txt',output)# by default in 'write' mode, but It is possible to set 'append' mode
遍历文件中的行:
而不是:
withopen('path/file.txt')asfile:forlineinfile:# ...
您可以使用iterFile
:
fromdracodesimportiterFileforlineiniterFile('path/file.txt'):# ...
缩进文档:
text="""class Jooj(object): def __init__(self, message): self.message def notBar(self): print('Not a Bar') def notFoo(self): print('Not a Foo')"""doc=IndentedDocument(text)forlineindoc:print(f'{line} | Depth: {line.depth}')
输出:
class Jooj(object): | Depth: 1
def __init__(self, message): | Depth: 2
self.message | Depth: 3
| Depth: 3
def notBar(self): | Depth: 2
print('Not a Bar') | Depth: 3
| Depth: 3
def notFoo(self): | Depth: 2
print('Not a Foo') | Depth: 3
卢卡斯·瓦加斯·诺罗尼亚