用于读取和创建riscure检查器跟踪集文件(.trs)的库
trsfile的Python项目详细描述
python中的inspector跟踪集.trs
文件支持
riscure检查器使用.trs
文件格式保存和读取磁盘上的跟踪。为了更好地帮助从第三方读取和写入跟踪集文件,riscure发布了这个python库。
快速启动
该库支持读取和写入^ {CD1>}文件,但它不支持(^ {EM1}$/EME>)支持修改现有的{{CD1>}文件。TraceSet
和Trace
类都模拟了list
的所有功能,所以切分您的心意吧!
安装
此库在PyPi上可用于python 3及更高版本。只需将trsfile
添加到requirements.txt
或通过命令行安装即可:
pip install trsfile
正在读取.trs
文件
importtrsfilewithtrsfile.open('trace-set.trs','r')astraces:# Show all headersforheader,valueintrs_file.get_headers().items():print(header,'=',value)print()# Iterate over the first 25 tracesfori,traceinenumerate(trs_file[0:25]):print('Trace {0:d} contains {1:d} samples'.format(i,len(trace)))print(' - minimum value in trace: {0:f}'.format(min(trace)))print(' - maximum value in trace: {0:f}'.format(max(trace)))
创建.trs
文件
importrandom,os,trsfilefromtrsfileimporttrs_open,Trace,SampleCoding,TracePadding,Headerwithtrs_open('trace-set.trs',# File name of the trace set'w',# Mode: r, w, x, a (default to x)# Zero or more options can be passed (supported options depend on the storage engine)engine='TrsEngine',# Optional: how the trace set is stored (defaults to TrsEngine)headers={# Optional: headers (see Header class)Header.LABEL_X:'Testing X',Header.LABEL_Y:'Testing Y',Header.DESCRIPTION:'Testing trace creation',},padding_mode=TracePadding.AUTO,# Optional: padding mode (defaults to TracePadding.AUTO)live_update=True# Optional: updates the TRS file for live preview (small performance hit)# 0 (False): Disabled (default)# 1 (True) : TRS file updated after every trace# N : TRS file is updated after N traces)astrs_file:# Extend the trace file with 100 traces with each 1000 samplestrs_file.extend([Trace(SampleCoding.FLOAT,[random.uniform(-255,255)for_inrange(0,1000)],data=os.urandom(16))for_inrange(0,100)])# Replace 5 traces (the slice [0:10:2]) with random length traces.# Because we are creating using the TracePadding.PAD mode, all traces# will be clipped or padded on the first trace lengthtrs_file[0:10:2]=[Trace(SampleCoding.FLOAT,[random.uniform(0,255)for_inrange(0,random.randrange(1000))],data=os.urandom(16),title='Clipped trace')for_inrange(0,5)]# Adding one Tracetrs_file.append(Trace(SampleCoding.FLOAT,[random.uniform(-255,255)for_inrange(0,1000)],data=os.urandom(16)))# We cannot delete traces with the TrsEngine, other engines do support this feature#del trs_file[40:50]# We can only change headers with a value that has the same length as the previous value# with the TrsEngine, other engines can support dynamically adding, deleting or changing# headers.#trs_file.update_header(Header.LABEL_X, 'Time')#trs_file.update_header(Header.LABEL_Y, 'Voltage')#trs_file.update_header(Header.DESCRIPTION, 'Traces created for some purpose!')print('Total length of new trace set: {0:d}'.format(len(trs_file)))
将TraceSet
从一种类型转换为另一种类型
importrandom,os,trsfilewith \ trsfile.open('trace-set',# Previously create trace set'r',# Read only modeengine='FileEngine'# Using the FileEngine)astraces, \ trsfile.open(# Note: TrsEngine is the default'trace-set.trs',# Name of the new trace set'w',# Write modeheaders=traces.get_headers()# Copy the headers)asnew_traces:new_traces.extend(traces)# Extend the new trace set with the# traces from the old trace set
文档
完整文档位于docs
文件夹中,在Read the Docs上有可读版本。
测试
库支持pythonunittest
模块,可以使用以下命令执行测试:
python -m unittest