一套常用工具
sine.utils的Python项目详细描述
正弦函数
常用工具集
工具
类事件管理器
提供事件发送和监听。 使用哈希键标识事件。
deff(key,data):printdatamanager=EventManager()manager.start()# start listenmanager.addListener(key='evnet_key',listener=f)manager.sendEvent(key='evnet_key','print hello')# other methodsmanager.removeListener(key='evnet_key',f)manager.stop()# stop listenmanager.clear()# clear event
类路径
方便以链式方式连接文件路径:
s=Path('.')s=s.join('a','b').join('..')# s == 'a'
它使用os.path.join
,并始终使用os.path.normpath
规范化路径。
因为os.path.join
加入'a', '..'
到'a/..'
模块属性
以面向行的格式读/写.properties文件key=value
每行{em1}$通过函数。
代码引用了Java 1.6中的java.util.Properties
。
常用用法
文件I/O:
# inputproperties=load(file)# or update existing dictionaryload(file,properties)# outputstore(file,properties)
自定义输入:
class LineReader
读取一个键值数据。它跳过所有注释行,
空行、前导空白和处理多行数据。loadSingle(string)
将上面给出的每一段数据读入键值。forlineinLineReader(file):# each key-value line has no line seperatorkey,value=loadSingle(line)# do something
自定义输出:
storeComments(writable, comments, linesep=os.linesep))
写注释(接受多行),可以指定行结束符。storeSingle(writable, key, value, sep='=', linesep=os.linesep)
写一个键值,可以指定分隔符和行结束符。storeComments(file,'this is a comment')storeSingle(file,'key','value')
文件格式
通常每一行是注释行或键值对。
主要功能:
- 按
=
、:
、,
\t
之一分隔键和值
- 忽略行首或
=
或:
周围的空格 - 注释行以
#
或!
开头
- 按
\uxxxx
转义Unicode
- 通过添加
\
来转义特殊字符
其他:
- 数据行以^{
结尾}放弃换行符
与Java的区别:
- store方法不会写入datetime注释
线程类
deffunc(a,b,stop_event):while1:ifstop_event.is_set():break# do your workthread=StoppableThread(target=func,args=('a','b'))thread.start()# ...thread.stop(-1)# stop and join forever# thread.stopped() == Truethread=ReStartableThread(target=func,args=('a','b'),event_name='stop_event')# can specify the parameter's namethread.start()# ...thread.stop(1)# stop and join for 1 second# ...thread.start()# ...thread.stop()thread.join()# ...
正弦存储
简介
基于字符串键为字符串数据库提供简单可靠的持久性。
更新时,数据更改将像日志记录一样附加到文件中。 数据以csv格式存储。 您可以随时可靠地压缩数据。
示例
# common usestorage=getStorage('./data.csv')author=storage.setdefault('author','sine')storage['author']='Sine'delstorage['author']forkinstorage.keys():print(k,storage[k])# compress datastorage.compress()
更改日志
V0.1.0,2019年7月21日
从现有包中收集:
- 正弦路径-v0.1.3
- 正弦螺纹-v0.1.7
- 正弦事件-v0.0.2
- 正弦特性-v0.1.1
以及新的“储藏室”。
新闻:
- eventmanager:更改传递的参数,包括键(就像调用sendEvent一样)
- 线程:修复关于args追加的问题