From a6299aa1355640ebb28f18e3c00df05d4d974834 Mon Sep 17 00:00:00 2001 From: ratal Date: Tue, 31 Mar 2015 21:54:24 +0100 Subject: [PATCH] General update regarding raise calls that I misunderstood. --- .gitignore | 19 +++++++++++++++++++ mdf3reader.py | 33 +++++++++++++++++---------------- mdf4reader.py | 12 ++++++------ mdfinfo3.py | 3 +-- mdfinfo4.py | 3 +-- mdfreader.py | 30 ++++++++++-------------------- mdfreaderui.py | 9 +++------ setup.py | 2 +- 8 files changed, 58 insertions(+), 53 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..69fb7d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +.eric6project/ +_eric6project/ +.eric5project/ +_eric5project/ +.eric4project/ +_eric4project/ +.ropeproject/ +_ropeproject/ +.directory/ +*.pyc +*.pyo +*.orig +*.bak +*.rej +*~ +cur/ +tmp/ +__pycache__/ +*.DS_Store diff --git a/mdf3reader.py b/mdf3reader.py index 624947f..67dd5b3 100644 --- a/mdf3reader.py +++ b/mdf3reader.py @@ -19,10 +19,11 @@ -------------- PythonVersion : float Python version currently running, needed for compatibility of both python 2.6+ and 3.2+ - + mdf3reader module -------------------------- """ + from numpy import average, right_shift, bitwise_and, all, diff, max, min, interp from numpy import asarray, zeros, recarray, array, reshape from numpy.core.records import fromfile @@ -30,12 +31,12 @@ from sys import platform, version_info from time import strftime, time from struct import pack, Struct -from io import open # for python 3 and 2 consistency +from io import open # for python 3 and 2 consistency from mdfinfo3 import info3 -PythonVersion=version_info -PythonVersion=PythonVersion[0] +PythonVersion = version_info +PythonVersion = PythonVersion[0] -def processDataBlocks(Q, buf, info, dataGroup, channelList, multiProc ): +def processDataBlocks(Q, buf, info, dataGroup, channelList, multiProc): """Put raw data from buf to a dict L and processes nested nBit channels Parameters @@ -66,7 +67,7 @@ def processDataBlocks(Q, buf, info, dataGroup, channelList, multiProc ): allChannel=True else: allChannel=False - ## Processes Bits, metadata + # Processes Bits, metadata for recordID in buf.keys(): for chan in buf[recordID]['record']: channelName=chan.name @@ -74,20 +75,20 @@ def processDataBlocks(Q, buf, info, dataGroup, channelList, multiProc ): recordName=buf[recordID]['record'].recordToChannelMatching[channelName] # in case record is used for several channels temp = buf[recordID]['data'].__getattribute__( str(recordName)+'_title') # extract channel vector - if chan.channelType ==1: # master channel + if chan.channelType ==1: # master channel channelName = 'master' + str( dataGroup ) # Process concatenated bits inside uint8 if not chan.bitCount//8.0==chan.bitCount/8.0: # if channel data do not use complete bytes mask = int(pow(2, chan.bitCount+1)-1) # masks isBitUnit8 - if chan.signalDataType in (0,1, 9, 10, 13, 14): # integers - temp = right_shift(temp, chan.bitOffset) + if chan.signalDataType in (0, 1, 9, 10, 13, 14): # integers + temp = right_shift(temp, chan.bitOffset) temp = bitwise_and(temp, mask ) L[channelName] = temp else: # should not happen print('bit count and offset not applied to correct data type') L[channelName] = temp - else: #data using full bytes + else: # data using full bytes L[channelName] = temp if multiProc: @@ -194,7 +195,7 @@ def logConv(data, conv): # 8 Logarithmic ----------- converted data to physical value """ - if conv['P4'] == 0 and conv['P1'] != 0 and conv['P2'] != 0: + if conv['P4'] == 0 and conv['P1'] != 0 and conv['P2'] != 0: return log(((data - conv['P7'] ) * conv['P6'] - conv['P3'] ) / conv['P1'] ) / conv['P2'] elif conv['P1'] == 0 and conv['P4'] != 0 and conv['P5'] != 0: return log((conv['P3'] / (data - conv['P7'] ) - conv['P6'] ) / conv['P4'] ) / conv['P5'] @@ -700,8 +701,7 @@ def read3( self, fileName=None, info=None, multiProc=False, channelList=None, co try: fid = open(self.fileName, 'rb') except IOError: - print('Can not find file'+self.fileName) - raise + raise Exception('Can not find file '+self.fileName) # Look for the biggest group to process first, to reduce processing time when mutiprocessed dataGroupList = dict.fromkeys(list(range( info['HDBlock']['numberOfDataGroups']))) @@ -803,7 +803,8 @@ def getChannelData3(self, channelName): if channelName in self: return self.convert3(channelName) else: - raise('Channel not in dictionary') + raise KeyError('Channel not in dictionary') + return channelName def convert3(self, channelName): """converts specific channel from raw to physical data according to CCBlock information @@ -1086,8 +1087,8 @@ def writePointer(f, pointer, value): elif data.dtype.kind in ['S', 'U']: dataType = 7 else: - print('Not recognized dtype') - raise + raise Exception('Not recognized dtype') + return data.dtype if not data.dtype.kind in ['S', 'U']: dataTypeList += data.dtype.char else: diff --git a/mdf4reader.py b/mdf4reader.py index 7ed1049..4e67031 100644 --- a/mdf4reader.py +++ b/mdf4reader.py @@ -110,7 +110,7 @@ def DATABlock(record, parent_block, channelList=None, sortedFlag=True): try: from zlib import decompress except: - raise('zlib module not found or error while uncompressing') + raise ImportError('zlib module not found or error while uncompressing') if PythonVersion>3: parent_block['data']=decompress(parent_block['data']) # decompress data else: @@ -403,7 +403,7 @@ def load(self, record, zip=None, nameList=None, sortedFlag=True): # reads sorted temps['data']=self.fid.read( temps['dz_data_length'] ) temps['data']=DATABlock(record, parent_block=temps, channelList=nameList, sortedFlag=sortedFlag) else: - raise('unknown data block') + raise Exception('unknown data block') return temps['data'] def readRecord(self, recordID, buf, channelList=None): @@ -965,8 +965,7 @@ def read4( self, fileName=None, info = None, multiProc = False, channelList=None try: fid = open( self.fileName, 'rb' ) except IOError: - print('Can not find file'+self.fileName) - raise + raise Exception('Can not find file '+self.fileName) if self.multiProc: # prepare multiprocessing of dataGroups @@ -1109,7 +1108,8 @@ def getChannelData4(self, channelName): if channelName in self: return convertChannelData4(self[channelName], channelName, self.convert_tables)[channelName] else: - raise('Channel not in dictionary') + raise KeyError('Channel not in dictionary') + return channelName def convertChannel4(self, channelName): """converts specific channel from raw to physical data according to CCBlock information @@ -1497,7 +1497,7 @@ def valueToValueTableWOInterpConv(vect, cc_val): try: from scipy import interpolate except: - raise('Please install scipy to convert channel') + raise ImportError('Please install scipy to convert channel') f = interpolate.interp1d( intVal, physVal , kind='nearest', bounds_error=False) # nearest return f(vect) # fill with Nan out of bounds while should be bounds else: diff --git a/mdfinfo3.py b/mdfinfo3.py index b3bc8b1..c4dec5e 100644 --- a/mdfinfo3.py +++ b/mdfinfo3.py @@ -80,8 +80,7 @@ def __init__(self, fileName=None, fid=None, filterChannelNames = False): try: fid = open( self.fileName, 'rb' ) except IOError: - print('Can not find file'+self.fileName) - raise + raise IOError('Can not find file '+self.fileName) self.readinfo3( fid ) elif fileName == None and fid!=None: self.readinfo3(fid) diff --git a/mdfinfo4.py b/mdfinfo4.py index 781f975..26008e9 100644 --- a/mdfinfo4.py +++ b/mdfinfo4.py @@ -761,8 +761,7 @@ def __init__(self, fileName=None, fid=None): try: fid = open( self.fileName, 'rb' ) except IOError: - print('Can not find file '+self.fileName) - raise + raise IOError('Can not find file '+self.fileName) self.readinfo( fid ) # Close the file fid.close() diff --git a/mdfreader.py b/mdfreader.py index 2adcba2..4d47a50 100644 --- a/mdfreader.py +++ b/mdfreader.py @@ -138,8 +138,7 @@ def readinfo( self, fileName = None, filterChannelNames=False ): try: fid = open( self.fileName, 'rb' ) except IOError: - print('Can not find file'+self.fileName) - raise + raise Exception('Can not find file '+self.fileName) # read Identifier block fid.seek(28) VersionNumber = unpack( '0: try: @@ -804,8 +799,7 @@ def exportToMatlab( self, filename = None ): try: from scipy.io import savemat except: - print( 'scipy module not found' ) - raise + raise ImportError( 'scipy module not found' ) if filename == None: filename = splitext(self.fileName)[0] filename = filename + '.mat' @@ -846,8 +840,7 @@ def exportToExcel( self , filename = None ): else: import xlwt3 as xlwt except: - print( 'xlwt module missing' ) - raise + raise ImportError( 'xlwt module missing' ) if filename == None: filename = filename = splitext(self.fileName)[0] filename = filename + '.xls' @@ -918,8 +911,7 @@ def exportToXlsx(self, filename=None): try: import openpyxl except: - print('Module openpyxl missing') - raise + raise ImportError('Module openpyxl missing') if filename == None: filename = splitext(self.fileName)[0] filename = filename + '.xlsx' @@ -1027,8 +1019,7 @@ def mergeMdf(self, mdfClass): self.convertAllChannel() # make sure all channels are converted unionedList=list(mdfClass.keys()) and list(self.keys()) if not len(list(self.masterChannelList.keys()))==1: - print( 'Data not resampled') - raise + raise Exception( 'Data not resampled') initialTimeSize=len(self.getChannelData('master')) for channel in unionedList: data = self.getChannelData(channel) @@ -1067,8 +1058,7 @@ def convertToPandas(self, sampling=None): try: import pandas as pd except: - print('Module pandas missing') - raise + raise ImportError('Module pandas missing') if sampling is not None: self.resample(sampling) datetimeInfo=datetime64(self.date.replace(':','-')+'T'+self.time) diff --git a/mdfreaderui.py b/mdfreaderui.py index 1cb7dbe..46cc071 100644 --- a/mdfreaderui.py +++ b/mdfreaderui.py @@ -97,8 +97,7 @@ def on_Convert_clicked(self): if not len(self.resampleValue.text())==0: resampleFlag=True else: - print('Empty field for resampling') - raise + raise Exception('Empty field for resampling') else: resampleFlag=False args=[(str(self.FileList.takeItem(0).text()),channelList,resampleFlag,resampleValue,convertFlag,convertSelection) for i in range(self.FileList.count())] @@ -112,8 +111,7 @@ def on_Convert_clicked(self): # import first file fileNameList=[] if len(self.resampleValue.text())==0: - print('Wrong value for re-sampling') - raise + raise Exception('Wrong value for re-sampling') convertFlag=False convertSelection=self.convertSelection resampleValue=float(self.resampleValue.text()) @@ -326,8 +324,7 @@ def processMDFstar(args): return processMDF(*args) except : print('Error, following file might be corrupted : '+args[0]) # Shows fileName and parameters to help finding corrupted files - print('Please re-try by removing this file from the list and restart mdfconverter to kill processes and clean memory') - raise # produce error and stops all processes + raise Exception('Please re-try by removing this file from the list and restart mdfconverter to kill processes and clean memory') def convertChannelList(channelList): if PythonVersion<3: diff --git a/setup.py b/setup.py index 1a03b7e..7743e00 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/development.html#single-sourcing-the-version - version='0.0.5', + version='0.0.6', description='A Measured Data Format file parser', long_description=long_description,