自动化助手

dracodes的Python项目详细描述


PyPI

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

卢卡斯·瓦加斯·诺罗尼亚

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java实现双锁并发队列   java如何更改SpringWS 2.4.4版或更高版本中maxOccurs的默认限制?   java Selenium RC如何处理动态行?   java何时引入Javac StringBuilder/StringBuffer优化?   java mediaplayer在R.raw Android上失败   java JPA2:不区分大小写,就像在任何地方匹配一样   如何从JDK15中提取java预览类文件?   java与mySQL删除查询不一致   从Shutdownow()返回的java列表<Runnable>无法转换为提交的Runnable   java如何从回调接口获取对象值?   java如何获得视图的高度,在xml中可见性和高度定义为wrap_内容?   postgresql Mybatis在启动时遇到“由以下原因引起:java.net.UnknownHostException:localhost”,为什么?   带有实例调用的方法调用的java intellij格式化程序换行   由于特定的第三方库,java Proguard产生运行时错误   动画JAVA JPanel同时滑出和滑入