From 6decedbf5baebaa836b1023651f40f77c9ff00bd Mon Sep 17 00:00:00 2001 From: JAdeSnoo Date: Mon, 4 Jul 2022 11:49:47 +0200 Subject: [PATCH] verbose printing Include verbose flag (default= True) in spc.File class to specify whether the format string should be printed. --- spc/spc.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/spc/spc.py b/spc/spc.py index 0baadca..932c732 100644 --- a/spc/spc.py +++ b/spc/spc.py @@ -49,7 +49,18 @@ class File: # CONSTRUCTOR # ------------------------------------------------------------------------ - def __init__(self, filename): + def __init__(self, filename, verbose= True): + + # specify if the format string should be printed + if verbose: + def verboseprint(*args): + # Print each argument separately so caller doesn't need to + # stuff everything to be printed into a single string + for arg in args: + print(arg) + else: + verboseprint = lambda *a: None # do-nothing function + # load entire into memory temporarly with open(filename, "rb") as fin: content = fin.read() @@ -156,7 +167,7 @@ def __init__(self, filename): # no x values are given, but they can be generated self.dat_fmt = 'gx-y' - print('{}({})'.format(self.dat_fmt, self.fnsub)) + verboseprint('{}({})'.format(self.dat_fmt, self.fnsub)) sub_pos = self.head_siz @@ -252,7 +263,7 @@ def __init__(self, filename): # -------------------------------------------- elif self.fversn == b'\x4c': # new MSB 1st - print("New MSB 1st, yet to be implemented") + verboseprint("New MSB 1st, yet to be implemented") pass # To be implemented # -------------------------------------------- @@ -360,7 +371,7 @@ def __init__(self, filename): # assuming it can't have separate x values self.dat_fmt = 'gx-y' - print('{}({})'.format(self.dat_fmt, self.fnsub)) + verboseprint('{}({})'.format(self.dat_fmt, self.fnsub)) self.fxtype = ord(self.fxtype) self.fytype = ord(self.fytype) @@ -372,7 +383,7 @@ def __init__(self, filename): # SHIMADZU # -------------------------------------------- elif self.fversn == b'\xcf': - print("Highly experimental format, may not work ") + verboseprint("Highly experimental format, may not work ") raw_data = content[10240:] # data starts here (maybe every time) # spacing between y and x data is atleast 0 bytes s_32 = chr(int('0', 2)) * 32 @@ -387,7 +398,7 @@ def __init__(self, filename): self.x = struct.unpack(('<' + dat_siz * 'd').encode('utf8'), raw_data[i:i + dat_len]) else: - print("File type %s not supported yet. Please add issue. " + verboseprint("File type %s not supported yet. Please add issue. " % hex(ord(self.fversn))) self.content = content