def calcMissingValueMask(self, observations): """ Calculates a missing value mask using the observations. At the moment all gridpoints are masked at missing value where at least one value is missing :TODO: find a less strict solution. Maybe 10% available? DONE! :param observations: dict with all observations :return netcdf file with missing values mask 1/0 """ for year in self.decadals: tmp_array = FileHandler.openNetCDFFile(observations[year], 'var') tmp_array_1d = tmp_array.reshape((1, tmp_array.size)) try: master = np.concatenate((master, tmp_array_1d)) except NameError: orig_shape = tmp_array.shape master = tmp_array_1d # sum along axis sum_array = np.sum(master, axis=0) # missing_value_mask = np.zeros((1,tmp_array.size)) missing_value_mask = np.where(sum_array < 0.1*(len(self.decadals)*1e20), sum_array, 1e20) missing_value_mask = np.where(sum_array >= 0.1*(len(self.decadals)*1e20), missing_value_mask, 1) missing_value_mask = np.reshape(missing_value_mask, orig_shape) misval_new = FileHandler.saveToNetCDF(missing_value_mask, observations.values()[0], 'missing_value_new2') misval_new = cdo.timmean(input=misval_new, output=misval_new+'timmean') return misval_new
def _calcCrps(self, hindcast, observation, variance, tag): """ Method calculates the CRPS after the formula by Gneiting and Raferty (2007). :param hindcast: anomaly conditional bias corrected hindcast :param observation: anomaly observation :param variance: Variance or STD you want to test :param tag: name tag :return: crps file """ np.seterr('ignore') try: x = cdo.div(input=' '.join([cdo.sub(input=' '.join([observation, hindcast]), output=hindcast+'_min_hindcast' + tag, options='-f nc'), variance]), output=hindcast+tag+'_div_') x = FileHandler.openNetCDFFile(x, mode='var') except KeyError: raise VariableName, 'Could not find variablename "%s" in observation files.' % cdo.showname(input=hindcast)[0] xCopy = np.ma.masked_greater(x, 0.8e20) varVar = FileHandler.openNetCDFFile(variance, mode='var') x = x.squeeze() xCopy = xCopy.squeeze() crps = - varVar * (1/np.sqrt(np.pi) - 2. * stats.norm.pdf(x) - x * (2. * stats.norm.cdf(x) - 1.)) crps = np.ma.array(crps, mask=xCopy.mask, fill_value=1e20) crps = crps.filled(1e20) crpsFile = FileHandler.saveToNetCDF(crps, hindcast, 'crps'+tag) return crpsFile
def todoman(output_md, file_suffix, md): '''Get files, get ToDos in files then write them out >>> todoman('__test.md', 'py', False) >>> with open('__test.md') as f: ... any(['[ ] USED FOR TESTING' in line for line in f.readlines()]) True >>> todoman('__test.md', 'py', True) >>> with open('__test.md') as f: ... any(['* USED FOR TESTING' in line for line in f.readlines()]) True >>> todoman('__test.md', 'js', False) >>> with open('__test.md') as f: ... ln = len(f.readlines()) >>> ln == 1 True >>> try: ... os.remove('__test.md') ... except OSError: ... pass ''' handler = FileHandler() files = handler.get_files(file_suffix) todos = get_todos(files) handler.write_todos(todos, output_md, md)
def processMessage(self,message,FILE,timeout=350): FILE['queue_url'] = self.createUserQueue(FILE) DFH = FileHandler(FILE) DFH.prepare() #for x in range(0,3): #try: # message.change_visibility(VisibilityTimeout=timeout) DFH.process()
def setUp(self): unittest.TestCase.setUp(self) filename4 = 'Puzzle-4x4-0001.txt' filename9 = 'Puzzle-9x9-0001.txt' filename16 = 'Puzzle-16x16-0001-Inconsistant.txt' self.puzzle4x4 = FileHandler.create_puzzle(filename4) self.puzzle9x9 = FileHandler.create_puzzle(filename9) self.puzzle16x16 = FileHandler.create_puzzle(filename16) self.assertEqual(4, self.puzzle4x4.size) self.assertEqual(9, self.puzzle9x9.size) self.assertEqual(16, self.puzzle16x16.size)
def checkSignificance(self, input, result_file, q1=0.05, q2=0.95, check_value=0): """ Checks if a value is statistically significant different from zero Uses the bootstrapped files :param input: list with files or folder of bootstraps :param result_file: file to check :return: sig_lon,sig_lat lists with significant points """ if type(input) == list: bootstrap_files = input elif os.path.isdir(input): bootstrap_files = self.find_files.getAllFilesInFolder(input) else: raise WrongArgument, 'Input has to be a list of files or a folder' bootstrap_arrays = list() # load all files to arrays for b_file in bootstrap_files: tmp_var = FileHandler.openNetCDFFile(b_file) bootstrap_arrays.append(tmp_var['variable']) result = FileHandler.openNetCDFFile(result_file, mode='var') (imax, jmax) = np.shape(result) lon, lat = self.__getLonLat(result_file) sig_lon, sig_lat, sig_x, sig_y = list(), list(), list(), list() # loop array for i in range(0, imax): for j in range(0, jmax): test_sample = np.zeros(len(bootstrap_files)) k = 0 for item in bootstrap_arrays: if item[i, j] < 1e10 and item[i, j] > -1e10: # don't test missing values test_sample[k] = item[i, j] # construct testsample k += 1 test_mean = np.mean(test_sample) quant = self.__getQuantile(test_sample, q1, q2) # mark significant values if (quant[0] > check_value and quant[1] > check_value) or (quant[0] < check_value and quant[1] < check_value): if test_mean > -1e19 and test_mean < 1e19: # no missingvalue in result if result[i, j] < 1e10 and result[i, j] > -1e10: sig_lon.append(lon[j]) sig_lat.append(lat[i]) sig_x.append(j) sig_y.append(i) self.save_significance_mask(sig_y, sig_x, result_file) return sig_lon, sig_lat
def plotLeadtimeseriesSign(resultList, flag_list, plot_list): colors = ['green', 'blue', 'red'] def getFn(s): return s.split('/')[-1] fig = plt.figure(figsize=(17, 13)) for i, needle in enumerate(plot_list): plt.subplot(2, 1, i+1) for j, flag in enumerate(flag_list): files_to_plot = list() # for part in resultList: search_needle = flag+'_'+needle[0] files_to_plot += [s for s in resultList if search_needle in getFn(s)] labels = list() plot_values = list() min_plot_values = list() max_plot_values = list() for fn in sorted(files_to_plot, key=lambda x: int(getFn(x).split('_')[0]+getFn(x).split('_')[1])): tmp_label = getFn(fn).split('_') if tmp_label[0] == tmp_label[1]: tmp_label = tmp_label[0] else: tmp_label = tmp_label[0]+'-'+tmp_label[1] labels.append(tmp_label) tmp_values = FileHandler.openNetCDFFile(fn, 'var') plot_values.append(tmp_values) tmp_min_value = FileHandler.openNetCDFFile(fn+'_bootstrap_min_val', 'var') min_plot_values.append(N.abs(tmp_values-tmp_min_value)) tmp_max_value = FileHandler.openNetCDFFile(fn+'_bootstrap_max_val', 'var') max_plot_values.append(N.abs(tmp_values-tmp_max_value)) x_val = range(1, len(files_to_plot)+1) plt.errorbar(x_val, plot_values, yerr=[min_plot_values, max_plot_values], color=colors[j]) plt.scatter(x_val, plot_values, color=colors[j]) # get min and max plot values try: min_val = min(min_val, min(plot_values)) max_val = max(max_val, max(plot_values)) except: min_val = min(plot_values) max_val = max(plot_values) plt.axis([0, len(files_to_plot)+1, min(min_val, needle[2][0]), max(max_val, needle[2][1])]) plt.ylabel(needle[1]) plt.xlabel('Leadtimes') plt.xticks(x_val, labels) if i==0: plt.legend(flag_list, bbox_to_anchor=(0., 1.05, 1., .102), mode='expand', loc=3, ncol=1, borderaxespad=0.)
def save_significance_mask(self, sign_lon, sign_lat, result_file): """ Saves the significance mask in a netcdf file :param sign_lon,sign_lat: significant points :param result_file: File to overwrite """ result = FileHandler.openNetCDFFile(result_file, mode='var') res_shape = result.shape sign_array = np.zeros(res_shape) for i in range(0, len(sign_lon)): sign_array[sign_lon[i], sign_lat[i]] = 1 return FileHandler.saveToNetCDF(sign_array, result_file, '_significance_mask')
def plotField(fileName, vmin, vmax, colormap='goddard', output_folder='/', lonlatbox='-180,180,-90,90'): ''' Plot any field variable :param fileName: filepath :param vmin: min value for colorbar :param vmax: max value for colorbar ''' fig1 = plt.figure(figsize=(12,7),dpi=500) if(colormap == 'goddard'): my_cmap = mpl.colors.LinearSegmentedColormap('my_colormap',Plotter.colorDict2,256) else: my_cmap = plt.cm.RdBu_r file_values = FileHandler.openNetCDFFile(fileName) mVar = file_values['variable'] lon = file_values['lon'] lat = file_values['lat'] if lonlatbox is None: if lon[0] < 0: lonlatbox = '-180,180,-90,90' else: lonlatbox = '0,360,-90,90' lonlatbox = map(int,lonlatbox.split(',')) m = Basemap(llcrnrlon=lonlatbox[0],llcrnrlat=lonlatbox[2],urcrnrlon=lonlatbox[1],urcrnrlat=lonlatbox[3]) def divi(x): return float(x)/10 colorSteps = map(divi,range(int(vmin*10),int((vmax*10)+1),1))#(vmax-vmin)/2)) if vmax == 0.5: colorSteps = [-0.5, -0.45, -0.4, -0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5] if vmax == 0. and vmin == -0.5: colorSteps = [-0.5, -0.475, -0.45, -0.425, -0.4, -0.375, -0.35, -0.325, -0.3, -0.275, -0.25, -0.225, -0.2, -0.175, -0.15, -0.125, -0.1, -0.075, -0.05, -0.025, 0] if vmax == 2. and vmin == 0: colorSteps = [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9,0.95, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0] colorTicks = colorSteps[0::2] my_cmap.set_bad("grey") #set missing value color #maskedArray = N.ma.masked_greater(mVar, 0.8e20) #mask missing Values maskedArray = N.ma.masked_outside(mVar, -0.8e20, 0.8e20) #discrete colormap norm = mpl.colors.BoundaryNorm(colorSteps, my_cmap.N) cs = m.imshow(maskedArray, interpolation="nearest", cmap=my_cmap, norm=norm) cb = m.colorbar(cs,"right", size="5%", pad='5%' , ticks=colorTicks) m.drawcoastlines() m.drawparallels(N.arange(-90.,120.,30.),labels=[1,0,0,0]) # draw parallels m.drawmeridians(N.arange(0.,420.,60.),labels=[0,0,0,1]) # draw meridians plt.title(Plotter.__getTitle(fileName)) plt.text(lonlatbox[0]+(lon[1]-lon[0])/2, lonlatbox[2]+(lat[1]-lat[0])/2, 'MurCSS') #print lonlatbox[0]+lon[1]-lon[0] return m
def addCrossesXY(map, fn, marker='x', color='k', size=9): sign_mask = FileHandler.openNetCDFFile(fn, 'var') sign_mask = N.flipud(sign_mask) sig_x = list() sig_y = list() for (x, y), value in N.ndenumerate(sign_mask): if value == 1: sig_x.append(y) sig_y.append(x) map.scatter(sig_x, sig_y, size, marker=marker, color=color)
def set_up_text(self): ''' Sets up the text widget, scrollbar and configures tags ''' self.text = Text(self.root) self.text.pack(side=LEFT, fill=BOTH, expand=YES) self.filehandler = FileHandler(self.text) self.text.tag_config("sel", background="green", foreground="blue") scrollbar = Scrollbar(self.root, orient=VERTICAL, command=self.text.yview) scrollbar.pack(side=RIGHT, fill=Y) self.text["yscrollcommand"] = scrollbar.set
def checkSignificanceFldmean(self, input, result_file, q1=0.05, q2=0.95, check_value=0): """ """ if type(input) == list: bootstrap_files = input elif os.path.isdir(input): bootstrap_files = self.find_files.getAllFilesInFolder(input) else: raise WrongArgument, 'Input has to be a list of files or a folder' bootstrap_arrays = list() #load all files to arrays for b_file in bootstrap_files: tmp_var = FileHandler.openNetCDFFile(b_file) bootstrap_arrays.append(tmp_var['variable']) result = FileHandler.openNetCDFFile(result_file, mode='var') test_sample = np.zeros(len(bootstrap_files)) k = 0 for item in bootstrap_arrays: if item < 1e10 and item > -1e10: test_sample[k] = item # construct testsample k += 1 test_mean = np.mean(test_sample) quant = self.__getQuantile(test_sample, q1, q2) # mark significant values FileHandler.saveToNetCDF(quant[0], result_file, '_bootstrap_max_val') FileHandler.saveToNetCDF(quant[1], result_file, '_bootstrap_min_val') return quant[0], quant[1]
def initmetadata(self): start = timer() tracks = FileHandler.getallfiles() numCreated = 0 numUpdated = 0 numRemoved = 0 for filename in tracks: record = self.getrec(filename) # If the record retrieved doesn't exist, then create it if not record: self.createrec(filename) #print('Created:', filename) numCreated += 1 # If the file has been updated since the record was created, then update the record elif not FileHandler.getupdate(filename) == record['update']: self.reprec(filename) #print('Updated:', filename) numUpdated += 1 records = self.coll.find() for record in records: if not FileHandler.checkexists(record['filename']): # If the filename on an existing record doesn't exist, delete the record self.delrec(record['filename']) #print('Deleted:', record['filename']) numRemoved += 1 print('Number of records created:', numCreated) print('Number of records updated:', numUpdated) print('Number of records removed:', numRemoved) end = timer() print(end - start, 'seconds elapsed')
def printFileValue(self, filename, msg): """ Method for debugging. Prints a specific value of a given netCDF File. :param filename: :param msg: additional message to print out. Like the name of the variable """ variable = FileHandler.openNetCDFFile(filename, mode='var') print np.shape(variable) if len(np.shape(variable)) == 2: print 'Value of %s is: %s' % (msg, variable[16, 37]) else: print 'Value of %s is: %s' % (msg, variable[0, 16, 37])
def _computeValues(self, resultList): """ Implementation """ import re for res in resultList: #get Correlation regex = re.compile(".*(correlation).*") corr_list = [m.group(0) for l in res for m in [regex.search(l)] if m] #std ratio regex = re.compile(".*(std_ratio).*") std_list = [m.group(0) for l in res for m in [regex.search(l)] if m] #add to model values for i in range(0,len(std_list)): name = std_list[i].split('/')[-1] name = name.split('_') name = name[0] + name[1] + name[2] tmp_dict = dict(corr=FileHandler.openNetCDFFile(corr_list[i], 'var'), std=1/FileHandler.openNetCDFFile(std_list[i], 'var'), ref=1) self._modelValues[name] = tmp_dict
def addCrossesFile(map, fn, marker='x', color='k', size=9): sign_mask = FileHandler.openNetCDFFile(fn) lon = sign_mask['lon'] lon = lon + (lon[1]-lon[0])/2 lat = sign_mask['lat'] lat = lat + (lat[1]-lat[0])/2 sign_mask = sign_mask['variable'] sig_x = list() sig_y = list() for (x, y), value in N.ndenumerate(sign_mask): if value == 1: sig_x.append(lon[y]) sig_y.append(lat[x]) map.scatter(sig_x, sig_y, size, marker=marker, color=color)
def __init__(self, **kwargs): super(SceneMixer, self).__init__(**kwargs) self.__frameHandler = FrameHandler() self.sceneTime = 0.0 self.fadeTime = 0.0 self.__currentFrame = None # init playbackHandler self.__isInPlayback = False self.__playbackHandler = PlayBackHandler(bpm=60.0, updatesPerBeat=20) self.__playbackHandler.addIntervalUpdateCallback(self.playbackCallbackUpdate) self.fileHandler = FileHandler() self.__initFinished = True self.__isInFrameChange = False self.syncedIsSetTest = False
def plotVerticalProfile(fileName, vmin, vmax, colormap='goddard', lonlatbox=None): """ Plots vertical profile of zonal mean file """ file_values = FileHandler.openNetCDFFile(fileName, 'plev') data = N.flipud(file_values['variable']) lat = file_values['lat'] plev = N.flipud(file_values['plev']) fig, ax1 = plt.subplots(figsize=(12, 7), dpi=500) colorSteps = N.linspace(-1, 1, 21) if vmax == 0.5: colorSteps = [-0.5, -0.45, -0.4, -0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5] if vmax == 0. and vmin == -0.5: colorSteps = [-0.5, -0.475, -0.45, -0.425, -0.4, -0.375, -0.35, -0.325, -0.3, -0.275, -0.25, -0.225, -0.2, -0.175, -0.15, -0.125, -0.1, -0.075, -0.05, -0.025, 0] if vmax == 2. and vmin == 0: colorSteps = [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0] if vmin == -0.6: colorSteps = [-0.6, -0.55, -0.5, -0.45, -0.4, -0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6] if vmin == -0.1 and vmax == 0: colorSteps = [-0.1, -0.09, -0.08, -0.07, -0.06, -0.05, -0.04, -0.03, -0.02, -0.01, 0.0] if vmin == -0.2 and vmax == 0: colorSteps = [-0.2, -0.18, -0.16, -0.14, -0.12, -0.1, -0.08, -0.06, -0.04, -0.02, 0.0] my_cmap = plt.cm.RdBu_r my_cmap.set_bad("grey") # set missing value color maskedArray = N.ma.masked_outside(data, -0.8e20, 0.8e20) # discrete colormap norm = mpl.colors.BoundaryNorm(colorSteps, my_cmap.N) cs = ax1.imshow(maskedArray, interpolation="nearest", cmap=my_cmap, norm=norm) cbar = fig.colorbar(cs, ax=ax1, orientation='vertical', ticks=colorSteps[0::2]) ax1.set_xlabel('Latitude [degrees]') ax1.set_ylabel("Pressure [Pa]") plt.xticks(range(1, len(lat), 4), lat[1::4]) plt.yticks(range(0, len(plev), 2), plev[0::2]) plt.title(Plotter.__getTitle(fileName)) return ax1
def __init__(self): self.filehandler = FileHandler(config.inbox_file) self.inbox_contents = self.filehandler.parse_text_file() self.command_parser = None
def __init__(self): self.file_handler = FileHandler()
class Controller: def __init__(self): self.db_handler = DatabaseHandler() self.data = None self.filehandler = None self.graph = None def load(self, filename): """ Set the file that will create the filehandler object """ if path.exists(filename): self.filehandler = FileHandler(filename) self.filehandler.set_file_type() return True else: return False def validate(self): """ Read selected file """ # James' changes (13/03) result = self.filehandler.read() self.data = result print(result) def set_local(self, connection): """ Set the local database with a specified name :param connection: :return: """ self.db_handler.set_local(connection) self.db_handler.insert_local_dict(self.data) def set_remote(self, host, user, password, db): """ Set the remote database :param host, user, password, db: :return: """ self.db_handler.set_remote(host, user, password, db) self.db_handler.insert_remote_dict(self.data) def set_graph(self, graph_type, filename): print(graph_type) print(filename) self.graph = Graph() data = self.data self.graph.set_data(data, graph_type, filename) def get_local(self): self.data = self.db_handler.get_local() def get_remote(self): self.data = self.db_handler.get_remote() def set_criteria(self, criteria_1, criteria_2=None): self.graph.set_criteria(criteria_1, criteria_2) def set_keys(self, key_1, key_2=None): self.graph.set_keys(key_1, key_2) def draw(self, x, y, title): self.graph.draw(x, y, title) def check_data(self): if self.data is not None: return True return False # Controller shouldn't run doctests??? # if __name__ == "__main__": # c = Controller() # doctest.testmod()
class Main(QtGui.QMainWindow): # Initialize mainwindow def __init__(self): # Declare class variables self.encodings = [ ["Default", None], ["ASCII", "ascii"], ["ISO-8859-1", "latin_1"], ["ISO-8859-2", "iso8859_2"], ["ISO-8859-3", "iso8859_3"], ["ISO-8859-4", "iso8859_4"], ["ISO-8859-5", "iso8859_5"], ["ISO-8859-6", "iso8859_6"], ["ISO-8859-7", "iso8859_7"], ["ISO-8859-8", "iso8859_8"], ["ISO-8859-9", "iso8859_9"], ["ISO-8859-10", "iso8859_10"], ["ISO-8859-13", "iso8859_13"], ["ISO-8859-14", "iso8859_14"], ["ISO-8859-15", "iso8859_15"], ["KOI8-R", "koi8_r"], ["KOI8-U", "koi8_u"], ["UTF-8 with BOM", "utf_8_sig"], ["UTF-8 without BOM", "utf_8"], ["UTF-16BE", "utf_16_be"], ["UTF-16LE", "utf_16_le"], ["UTF-32BE", "utf_32_be"], ["UTF-32LE", "utf_32_le"], ["Windows-1250", "cp1250"], ["Windows-1251", "cp1251"], ["Windows-1252", "cp1252"], ["Windows-1253", "cp1253"], ["Windows-1254", "cp1254"], ["Windows-1255", "cp1255"], ["Windows-1256", "cp1256"], ["Windows-1257", "cp1257"], ["Windows-1258", "cp1258"] ] self.path = "" # Path for fix self.filelist = [] # File list for fix self.filetypes = ["srt", "txt"] # Accepted file types self.typefilter = "All supported files (*.srt *.txt)" self.typefilter += ";;SubRip subtitle file (*.srt)" self.typefilter += ";;Text file (*.txt)" # Create class instances self.data = DataHandler() self.file = FileHandler(self.filetypes) self.string = StringHandler() # Initialize top level window widget QtGui.QMainWindow.__init__(self) # This is always the same self.ui = Ui_MainWindow() self.ui.setupUi(self) # Connect signals, menu and toolbar self.ui.actionNewDatabase.triggered.connect(self.newDatabase) self.ui.actionOpenDatabase.triggered.connect(self.openDatabaseMsgBox) self.ui.actionAddFiles.triggered.connect(self.fAddFiles) self.ui.actionAddFolder.triggered.connect(self.fAddFolder) self.ui.actionRemoveFiles.triggered.connect(self.fRemoveFiles) self.ui.actionClearList.triggered.connect(self.fClearList) self.ui.actionStart.triggered.connect(self.fStart) self.ui.actionQuit.triggered.connect(self.quitApplication) self.ui.actionAbout.triggered.connect(self.aboutMessage) # Connect signals, buttons self.ui.btnFStart.clicked.connect(self.fStart) self.ui.btnFAddFiles.clicked.connect(self.fAddFiles) self.ui.btnFAddFolder.clicked.connect(self.fAddFolder) self.ui.btnFRemoveFiles.clicked.connect(self.fRemoveFiles) self.ui.btnFClearList.clicked.connect(self.fClearList) self.ui.btnLDeleteRow.clicked.connect(self.lDeleteRow) self.ui.btnLInsertRow.clicked.connect(self.lInsertRow) self.ui.btnLUpdateRow.clicked.connect(self.lUpdateRow) self.ui.txtCSearch.returnPressed.connect(self.refreshCCorrections) self.ui.btnCDeleteRow.clicked.connect(self.cDeleteRow) self.ui.btnCInsertRow.clicked.connect(self.cInsertRow) self.ui.btnCUpdateRow.clicked.connect(self.cUpdateRow) self.ui.btnLClearLog.clicked.connect(self.lClearLog) # Connect signals, comboboxes self.ui.cboCCorrectionMode.activated.connect(self.refreshCCorrections) self.ui.cboCList.activated.connect(self.refreshCCorrections) # Connect signals, tables self.ui.tblCCorrections.currentCellChanged[(int,int,int,int)].connect(\ self.selectCorrection) # Drag-and-drop events for file list self.ui.tblFFileList.dragEnterEvent = self.dragEnterEvent self.ui.tblFFileList.dragMoveEvent = self.dragEnterEvent self.ui.tblFFileList.dropEvent = self.dropEvent # Loop and set encodings to settings combobox for item in self.encodings: self.ui.cboSFileEncoding.addItem(item[0]) # Select the first tab from the tab widgets self.ui.tabMain.setCurrentIndex(0) # Icons self.setWindowIcon(QtGui.QIcon("icon.png")) self.ui.actionNewDatabase.setIcon(QtGui.QIcon("new_database.png")) self.ui.actionOpenDatabase.setIcon(QtGui.QIcon("open_database.png")) self.ui.actionAddFiles.setIcon(QtGui.QIcon("add_files.png")) self.ui.actionAddFolder.setIcon(QtGui.QIcon("add_folder.png")) self.ui.actionRemoveFiles.setIcon(QtGui.QIcon("remove.png")) self.ui.actionClearList.setIcon(QtGui.QIcon("clear.png")) self.ui.actionStart.setIcon(QtGui.QIcon("start.png")) self.ui.actionQuit.setIcon(QtGui.QIcon("quit.png")) self.ui.actionAbout.setIcon(QtGui.QIcon("about.png")) self.ui.tabMain.setTabIcon(0, QtGui.QIcon("fix.png")) self.ui.tabMain.setTabIcon(1, QtGui.QIcon("lists.png")) self.ui.tabMain.setTabIcon(2, QtGui.QIcon("corrections.png")) self.ui.tabMain.setTabIcon(3, QtGui.QIcon("log.png")) self.ui.tabMain.setTabIcon(4, QtGui.QIcon("settings.png")) self.ui.btnFAddFiles.setIcon(QtGui.QIcon("add_files.png")) self.ui.btnFAddFolder.setIcon(QtGui.QIcon("add_folder.png")) self.ui.btnFRemoveFiles.setIcon(QtGui.QIcon("remove.png")) self.ui.btnFClearList.setIcon(QtGui.QIcon("clear.png")) self.ui.btnFStart.setIcon(QtGui.QIcon("start.png")) self.ui.btnLDeleteRow.setIcon(QtGui.QIcon("delete_row.png")) self.ui.btnLInsertRow.setIcon(QtGui.QIcon("insert_row.png")) self.ui.btnLUpdateRow.setIcon(QtGui.QIcon("update_row.png")) self.ui.btnCDeleteRow.setIcon(QtGui.QIcon("delete_row.png")) self.ui.btnCInsertRow.setIcon(QtGui.QIcon("insert_row.png")) self.ui.btnCUpdateRow.setIcon(QtGui.QIcon("update_row.png")) self.ui.btnLClearLog.setIcon(QtGui.QIcon("clear.png")) # Check if command line arguments has files in it if sys.argv[1:]: self.loopItems(sys.argv[1:]) # Load settings self.loadSettings() #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #+ Settings #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ def loadSettings(self): try: settings = QtCore.QSettings("bulkware", "bwReplacer") if settings.contains("geometry"): # Window geometry self.restoreGeometry(settings.value("geometry")) if settings.contains("state"): # Window state self.restoreState(settings.value("state")) if settings.contains("database"): # Database dbpath = str(settings.value("database", type=str)) self.openDatabase(dbpath) if settings.contains("logfiles"): # Log files logfiles = settings.value("logfiles", type=bool) self.ui.chkLLogFiles.setChecked(logfiles) if settings.contains("logfixes"): # Log fixes logfixes = settings.value("logfixes", type=bool) self.ui.chkLLogFixes.setChecked(logfixes) if settings.contains("encoding"): # Encoding encindex = settings.value("encoding", type=int) self.ui.cboSFileEncoding.setCurrentIndex(encindex) if settings.contains("path"): # Fix, path self.path = settings.value("path", type=str) except: self.path = "" return False else: return True # Save settings when closing the application def closeEvent(self, event): settings = QtCore.QSettings("bulkware", "bwReplacer") settings.setValue("geometry", self.saveGeometry()) settings.setValue("state", self.saveState()) settings.setValue("database", str(self.data.database)) settings.setValue("logfiles", self.ui.chkLLogFiles.isChecked()) settings.setValue("logfixes", self.ui.chkLLogFixes.isChecked()) settings.setValue("encoding", \ int(self.ui.cboSFileEncoding.currentIndex())) settings.setValue("path", str(self.path)) #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #+ Actions #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # New database def newDatabase(self): # Dialog for database name name, ok = QtGui.QInputDialog.getText(self, "New database", "Enter name for database:") if not ok: return if name == "": msg = "Database name cannot be empty." self.ui.statusBar.showMessage(msg) QtGui.QMessageBox.critical(self, "Error", msg) return try: self.data.create_database(name) self.refreshLists() self.refreshLLists() self.refreshCCorrections() except: msg = "Unable to create database." self.ui.statusBar.showMessage(msg) QtGui.QMessageBox.critical(self, "Error", msg) return False else: msg = "Database created: " + self.data.database self.ui.statusBar.showMessage(msg) # Enable/disable widgets self.enabledisableWidgets() # Menu > File > Open database... (using message box) def openDatabaseMsgBox(self): # Get the database file using a dialog fp = QtGui.QFileDialog.getOpenFileName(self, "Open database", os.path.dirname(sys.argv[0]), "SQLite databases (*.db)") # If the filename was not provided if fp == "": return self.openDatabase(fp) # Open database def openDatabase(self, fp): # Try to open database if not self.data.open(fp): self.ui.statusBar.showMessage(self.data.error) QtGui.QMessageBox.critical(self, "Error", self.data.error) return # Refresh main window widgets self.refreshLists() self.refreshCCorrections() self.refreshLLists() # Enable/disable widgets self.enabledisableWidgets() # Show success msg msg = "Successfully opened database: " + fp self.ui.statusBar.showMessage(msg) self.logMessage(msg) # Menu > File > Quit def quitApplication(self): QtGui.QApplication.quit() # Menu > Help > About... def aboutMessage(self): msg = """<strong>bwReplacer</strong><br /> Version 1.3.0<br /> <br /> This is free software.<br /> Released under the General Public License.<br /> <br /> <a href="https://github.com/bulkware/bwreplacer">GitHub</a>""" QtGui.QMessageBox.about(self, "About", msg) # Add files def fAddFiles(self): # Get file list using a dialog items = QtGui.QFileDialog.getOpenFileNames(self, "Add files", self.path) # Check list for items if len(items) < 1: return # Loop items to main file list self.loopItems(items) # Add folder def fAddFolder(self): # Get folder using a dialog path = QtGui.QFileDialog.getExistingDirectory(self, "Add folder", self.path) # Check path if path == "": return # Check dir if not os.path.isdir(path): return # Create a list with full paths items = [] for item in os.listdir(path): items.append(os.path.join(path, item)) # Loop items to main file list self.loopItems(items) # Fix, loop items to main file list def loopItems(self, items): nonexist = 0 duplicates = 0 folders = 0 unallowed = 0 for item in items: # Check if item does not exist if not os.path.exists(item): nonexist += 1 continue # Check for duplicates if item in self.filelist: duplicates += 1 continue # Check for folders if os.path.isdir(item): folders += 1 continue # Check for extension ext = os.path.splitext(item)[1][1:].lower() if ext not in self.filetypes: unallowed += 1 continue # Check if file path is an existing regular file if not os.path.isfile(item): continue # Set the path from first file if item == items[0]: self.path = os.path.split(item)[0] # Add file self.filelist.append(item) # Check for files and sort them if len(self.filelist) > 0: self.filelist.sort() # Enable/disable widgets self.enabledisableWidgets() # Refresh file list table self.refreshFileList() # Count total adds and message user, if necessary total = nonexist total += duplicates total += folders total += unallowed if total > 0: msg = "%s items were skipped:\n" % (total) msg += "\n" msg += "%s doesn't exist\n" % (nonexist) msg += "%s duplicates\n" % (duplicates) msg += "%s folders\n" % (folders) msg += "%s unallowed extensions\n" % (unallowed) QtGui.QMessageBox.information(self, "Info", msg) # Fix, clear list def fClearList(self): self.filelist[:] = [] self.enabledisableWidgets() self.refreshFileList() # Fix, remove files def fRemoveFiles(self): # Check if file list is empty if len(self.filelist) < 1: msg = "No files in list." QtGui.QMessageBox.critical(self, "Error", msg) return # Check table's selected items if not self.ui.tblFFileList.selectedIndexes(): msg = "Please select file." QtGui.QMessageBox.critical(self, "Error", msg) return # Get indexes from table rows = [] for item in self.ui.tblFFileList.selectedIndexes(): index = int(item.row()) if index not in rows: rows.append(index) # Remove items in reverse order (so the indexes won't change) for index in sorted(rows, reverse=True): del self.filelist[index] # Check file list for files if len(self.filelist) < 1: self.enabledisableWidgets() # Refresh table self.refreshFileList() # Fix, start def fStart(self): # Check for database if not self.data.database: msg = "Database connection not established." QtGui.QMessageBox.critical(self, "Error", msg) return # Create a list of selected lists lists = [] for sel in range(self.ui.lstFLists.count()): if self.ui.lstFLists.item(sel).checkState(): lists.append(self.data.get_list(index=sel)[0]) # Check for lists if not lists: msg = "No lists defined." self.ui.statusBar.showMessage(msg) QtGui.QMessageBox.critical(self, "Error", msg) return # Get corrections corrections = self.data.get_corrections([1,2,3], lists) # Check for corrections if not corrections: msg = "No corrections to apply." self.ui.statusBar.showMessage(msg) QtGui.QMessageBox.critical(self, "Error", msg) return False # Enable/disable widgets self.enabledisableWidgets(disable=True) # Select the log tab self.ui.tabMain.setCurrentIndex(3) # Loop file list self.string.reset() for file in self.filelist: if self.ui.chkLLogFiles.isChecked(): msg = "Processing file: " + os.path.basename(file) self.logMessage(msg) ok, msg = self.fixFile(file, corrections) if ok: self.ui.statusBar.showMessage(msg) else: self.ui.statusBar.showMessage(msg) QtGui.QMessageBox.critical(self, "Error", msg) break if self.ui.chkLLogFiles.isChecked(): msg = "File processed successfully: " + os.path.basename(file) self.logMessage(msg) # Show statistics if processing went ok if ok: msg = "Processing finished. %s corrections were applied." % \ (self.string.count) self.ui.statusBar.showMessage(msg) self.logMessage(msg) # Enable/disable widgets self.enabledisableWidgets() # Fix file def fixFile(self, file, corrections): # Get the file encoding setting enc = self.encodings[self.ui.cboSFileEncoding.currentIndex()][1] # Open file if not self.file.open(file, enc): return False, self.file.error i = 1 lines = self.file.count() # Loop file's editable data for key, line in sorted(self.file.get_editdata().items()): oldline = line # Loop corrections for item in corrections: ok, line = self.string.replace(line, int(item[1]), \ str(item[4]), str(item[5]), int(item[3])) if not ok: return False, self.string.error # If line has changed... if line != oldline: # Set corrected line back to file self.file.set_line(key, line) # Log fix if self.ui.chkLLogFixes.isChecked(): msg = "Fixed row " + str(key) + ", " + oldline + " --> " + \ line self.logMessage(msg) # Update statusBar info msg = "Processing file: %s (line %s/%s)" \ % (os.path.basename(file), key, lines) self.ui.statusBar.showMessage(msg) # Update app QtGui.QApplication.processEvents() i += 1 # Check for empty lines at EOF removal option if self.ui.chkSRemoveEmpty.isChecked(): emptylines = True else: emptylines = False # Save file if not self.file.save(enc, emptylines): return False, self.file.error # Close file self.file.close() # All went well return True, "File processed successfully: " + file # Clear log def lClearLog(self): self.ui.lstLLog.clear() #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #+ Events #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # Drag def dragEnterEvent(self, event): if (event.type() == QtCore.QEvent.DragEnter): if event.mimeData().hasUrls(): event.accept() else: event.ignore() # Drop def dropEvent(self, event): if (event.type() == QtCore.QEvent.Drop): if event.mimeData().hasUrls(): # Make a list of items from drag-and-drop items = [] for i in event.mimeData().urls(): items.append(i.toLocalFile()) # Loop items to main file list self.loopItems(items) #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ #+ Widgets #+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ # Fix, refresh file list table def refreshFileList(self): # Clear widgets self.ui.tblFFileList.setColumnCount(0) self.ui.tblFFileList.setRowCount(0) self.ui.tblFFileList.clear() # Check if file list is empty if len(self.filelist) < 1: return False # Set columns and rows self.ui.tblFFileList.setColumnCount(3) self.ui.tblFFileList.setRowCount(len(self.filelist)) # Set header labels self.ui.tblFFileList.setHorizontalHeaderLabels(["File", "Size", ""]) # Populate table for i, file in enumerate(self.filelist): item = QtGui.QTableWidgetItem(os.path.basename(file)) item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable) item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignLeft) self.ui.tblFFileList.setItem(i, 0, item) size = functions.convert_bytes(os.path.getsize(file)) item = QtGui.QTableWidgetItem(size) item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable) item.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight) self.ui.tblFFileList.setItem(i, 1, item) item = QtGui.QTableWidgetItem() item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsSelectable) self.ui.tblFFileList.setItem(i, 2, item) # Resize columns to contents # setVisible lines are because of QTBUG-9352! self.ui.tblFFileList.setVisible(False) self.ui.tblFFileList.resizeColumnsToContents() self.ui.tblFFileList.setVisible(True) # Refresh widgets with lists def refreshLists(self): # Check for database if not self.data.database: return # Clear widgets self.ui.cboCList.clear() self.ui.lstFLists.clear() # Get lists lists = self.data.get_lists() # Check for lists if not lists: return # Set lists to widgets self.ui.cboCList.addItem("All") for list in lists: # Corrections self.ui.cboCList.addItem(list[2]) # Batch fix, list item = QtGui.QListWidgetItem(list[2]) item.setFlags(QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsUserCheckable) if list[1] == 1: item.setCheckState(QtCore.Qt.Checked) else: item.setCheckState(QtCore.Qt.Unchecked) self.ui.lstFLists.addItem(item) # Refresh lists table def refreshLLists(self): # Check for database if not self.data.database: return # Clear lists table self.ui.tblLLists.setColumnCount(0) self.ui.tblLLists.setRowCount(0) self.ui.tblLLists.clear() # Get lists rows = self.data.get_lists() # Check for lists if not rows: return # Set columns and rows for table self.ui.tblLLists.setColumnCount(4) self.ui.tblLLists.setRowCount(len(rows)) # Set header labels for table self.ui.tblLLists.setHorizontalHeaderLabels(["ID", "Selected", "Name", \ "Comment"]) # Populate table for i, row in enumerate(rows): for column in range(4): # Column 1 is the selected column if column == 1: item = QtGui.QCheckBox("Y/n") if row[column]: item.setCheckState(QtCore.Qt.Checked) else: item.setCheckState(QtCore.Qt.Unchecked) self.ui.tblLLists.setCellWidget(i, column, item) else: text = str(row[column]) item = QtGui.QTableWidgetItem(text) # ID column can't have any item flags if column == 0: item.setFlags(QtCore.Qt.NoItemFlags) self.ui.tblLLists.setItem(i, column, item) # Set horizontal header visibility to true (QT BUG?) self.ui.tblLLists.horizontalHeader().setVisible(True) # Resize table columns to contents # setVisible lines are because of QTBUG-9352! self.ui.tblLLists.setVisible(False) self.ui.tblLLists.resizeColumnsToContents() self.ui.tblLLists.setVisible(True) # Delete list def lDeleteRow(self): # Check for database if not self.data.database: msg = "Database connection not established." QtGui.QMessageBox.critical(self, "Error", msg) return # Check table's selected items if not self.ui.tblLLists.selectedIndexes(): msg = "Please select item from table." QtGui.QMessageBox.critical(self, "Error", msg) return # Get selected row row = int(self.ui.tblLLists.currentRow()) # Get id id = int(self.ui.tblLLists.item(row, 0).text()) # Delete list ok = self.data.delete(id, "lists") if ok: msg = "List deleted." self.ui.statusBar.showMessage(msg) else: self.ui.statusBar.showMessage(self.data.error) QtGui.QMessageBox.critical(self, "Error", self.data.error) return # Remove row from table self.ui.tblLLists.removeRow(row) # Refresh lists self.refreshLists() self.refreshLLists() # Refresh the corrections table self.refreshCCorrections() # Insert list def lInsertRow(self): # Check for database if not self.data.database: msg = "Database connection not established." QtGui.QMessageBox.critical(self, "Error", msg) return # Insert list ok = self.data.insert("lists", [0, "", ""]) if ok: msg = "List inserted." self.ui.statusBar.showMessage(msg) else: self.ui.statusBar.showMessage(self.data.error) QtGui.QMessageBox.critical(self, "Error", self.data.error) return # Refresh the lists table self.refreshLLists() # Get the last row from table and insert new row lastrow = self.ui.tblLLists.rowCount() - 1 # Select the name cell from the last row, which was inserted self.ui.tblLLists.setCurrentCell(lastrow, 3) self.ui.tblLLists.scrollToItem( self.ui.tblLLists.item(lastrow, 3), QtGui.QAbstractItemView.EnsureVisible) self.ui.tblLLists.setFocus() # Update list def lUpdateRow(self): # Check for database if not self.data.database: msg = "Database connection not established." QtGui.QMessageBox.critical(self, "Error", msg) return # Check table's selected items if not self.ui.tblLLists.selectedIndexes(): msg = "Please select item from table." QtGui.QMessageBox.critical(self, "Error", msg) return # Get selected row row = int(self.ui.tblLLists.currentRow()) # Get values id = int(self.ui.tblLLists.item(row, 0).text()) selected = int(self.ui.tblLLists.cellWidget(row, 1).checkState()) if selected > 1: selected = 1 # Tristate checkbox into 1 name = str(self.ui.tblLLists.item(row, 2).text()) comment = str(self.ui.tblLLists.item(row, 3).text()) # Check if name is empty if name == "": msg = "Name cannot be the empty." QtGui.QMessageBox.critical(self, "Error", msg) return # Update list ok = self.data.update("lists", [id, selected, name, comment]) if ok: msg = "List updated." self.ui.statusBar.showMessage(msg) else: self.ui.statusBar.showMessage(self.data.error) QtGui.QMessageBox.critical(self, "Error", self.data.error) # Refresh lists self.refreshLists() self.refreshLLists() # Refresh corrections table def refreshCCorrections(self): # Clear corrections table self.ui.tblCCorrections.setColumnCount(0) self.ui.tblCCorrections.setRowCount(0) self.ui.tblCCorrections.clear() # Check for database if not self.data.database: return # Get lists lists = self.data.get_lists() # Check for lists if not lists: return # Mode modeindex = int(self.ui.cboCCorrectionMode.currentIndex()) modeid = [] if modeindex > 0: modeid = [modeindex] # List listindex = int(self.ui.cboCList.currentIndex()) listid = [] if listindex > 0: listindex -= 1 # "All" option adds one listdata = self.data.get_list(index=listindex) listid = [int(listdata[0])] # Search search = str(self.ui.txtCSearch.text()) # Get corrections rows = self.data.get_corrections(modeid, listid, search) # Check for corrections if not rows: return # Set columns and rows for table self.ui.tblCCorrections.setColumnCount(7) self.ui.tblCCorrections.setRowCount(len(rows)) # Set header labels for table self.ui.tblCCorrections.setHorizontalHeaderLabels(["ID", "Mode", \ "List", "Variations", "Error", "Correction", "Comment"]) # Populate corrections table for i, row in enumerate(rows): for column in range(7): if column == 0: # ID text = str(row[0]) item = QtGui.QTableWidgetItem(text) item.setFlags(QtCore.Qt.NoItemFlags) elif column == 1: # Mode modeid = int(row[1]) modetext = str(self.data.correctionmodes[modeid]) item = QtGui.QTableWidgetItem(modetext) elif column == 2: # List listid = int(row[2]) listtext = str(self.data.get_list(id=listid)[2]) item = QtGui.QTableWidgetItem(listtext) elif column == 3: # Variations if row[column]: text = "Yes" else: text = "No" item = QtGui.QTableWidgetItem(text) else: text = str(row[column]) item = QtGui.QTableWidgetItem(text) self.ui.tblCCorrections.setItem(i, column, item) # Set horizontal header visibility to true (QT BUG?) self.ui.tblCCorrections.horizontalHeader().setVisible(True) # Resize table columns to contents # setVisible lines are because of QTBUG-9352! self.ui.tblCCorrections.setVisible(False) self.ui.tblCCorrections.resizeColumnsToContents() self.ui.tblCCorrections.setVisible(True) # Select correction def selectCorrection(self, row, col, row2, col2): # Check if row is selected or row is not row2 if row < 0 or row == row2: return # Get lists lists = self.data.get_lists() # Check for lists if not lists: return # Get ID and row data id = int(self.ui.tblCCorrections.item(row, 0).text()) rowdata = self.data.get_correction(id) # Mode item = QtGui.QComboBox() item.addItems(self.data.correctionmodes[1:]) item.setCurrentIndex(rowdata[1] - 1) self.ui.tblCCorrections.setCellWidget(row, 1, item) # List item = QtGui.QComboBox() for i, list in enumerate(lists): item.addItem(list[2]) if list[0] == rowdata[2]: selectedlist = i item.setCurrentIndex(selectedlist) self.ui.tblCCorrections.setCellWidget(row, 2, item) # Variations self.ui.tblCCorrections.setItem(row, 3, QtGui.QTableWidgetItem()) item = QtGui.QCheckBox("Y/n") if int(rowdata[3]): item.setCheckState(QtCore.Qt.Checked) else: item.setCheckState(QtCore.Qt.Unchecked) self.ui.tblCCorrections.setCellWidget(row, 3, item) # Reset old row if row != row2 and row2 > -1: # Get ID and row data id = int(self.ui.tblCCorrections.item(row2, 0).text()) rowdata = self.data.get_correction(id) # If there is no row data, the row was deleted if not rowdata: return # Mode modeid = int(rowdata[1]) modetext = str(self.data.correctionmodes[modeid]) self.ui.tblCCorrections.removeCellWidget(row2, 1) self.ui.tblCCorrections.setItem(row2, 1, QtGui.QTableWidgetItem(modetext)) # List listid = int(rowdata[2]) listtext = str(self.data.get_list(id=listid)[2]) self.ui.tblCCorrections.removeCellWidget(row2, 2) self.ui.tblCCorrections.setItem(row2, 2, QtGui.QTableWidgetItem(listtext)) # Variations self.ui.tblCCorrections.removeCellWidget(row2, 3) if int(rowdata[3]): text = "Yes" else: text = "No" item = QtGui.QTableWidgetItem(text) self.ui.tblCCorrections.setItem(row2, 3, item) # Delete correction def cDeleteRow(self): # Check for database if not self.data.database: msg = "Database connection not established." QtGui.QMessageBox.critical(self, "Error", msg) return # Check table's selected items if not self.ui.tblCCorrections.selectedIndexes(): msg = "Please select item from table." QtGui.QMessageBox.critical(self, "Error", msg) return # Get selected row row = int(self.ui.tblCCorrections.currentRow()) # Get ID id = int(self.ui.tblCCorrections.item(row, 0).text()) # Delete correction ok = self.data.delete(id, "corrections") if ok: msg = "Correction deleted, ID: %s" % (id) self.ui.statusBar.showMessage(msg) else: self.ui.statusBar.showMessage(self.data.error) QtGui.QMessageBox.critical(self, "Error", self.data.error) return # Remove row from table self.ui.tblCCorrections.removeRow(row) # Insert correction def cInsertRow(self): # Check for database if not self.data.database: msg = "Database connection not established." QtGui.QMessageBox.critical(self, "Error", msg) return # Get lists lists = self.data.get_lists() # Check for lists if not lists: msg = "No lists defined." self.ui.statusBar.showMessage(msg) QtGui.QMessageBox.critical(self, "Error", msg) return # Mode modeid = int(self.ui.cboCCorrectionMode.currentIndex()) + 1 modetext = str(self.data.correctionmodes[modeid]) # List sel = int(self.ui.cboCList.currentIndex()) listid = int(self.data.get_list(index=sel)[0]) # Insert correction ok = self.data.insert("corrections", [modeid, listid, 0, "", "", ""]) if ok: msg = "Row inserted." self.ui.statusBar.showMessage(msg) else: self.ui.statusBar.showMessage(self.data.error) QtGui.QMessageBox.critical(self, "Error", self.data.error) return # Refresh the corrections table self.refreshCCorrections() # Update correction def cUpdateRow(self): # Check for database if not self.data.database: msg = "Database connection not established." QtGui.QMessageBox.critical(self, "Error", msg) return # Check table's selected items if not self.ui.tblCCorrections.selectedIndexes(): msg = "Please select item from table." QtGui.QMessageBox.critical(self, "Error", msg) return # Get lists lists = self.data.get_lists() # Check for lists if not lists: return # Get selected row row = int(self.ui.tblCCorrections.currentRow()) # Get ID id = int(self.ui.tblCCorrections.item(row, 0).text()) # Mode modeid = int(self.ui.tblCCorrections.cellWidget(row, 1).currentIndex()) modeid += 1 modetext = str(self.data.correctionmodes[modeid]) # List sel = int(self.ui.tblCCorrections.cellWidget(row, 2).currentIndex()) listdata = self.data.get_list(index=sel) listid = int(listdata[0]) # Variations varia = int(self.ui.tblCCorrections.cellWidget(row, 3).checkState()) if varia > 1: varia = 1 # Tristate checkbox into 1 # Error, correction and comment error = str(self.ui.tblCCorrections.item(row, 4).text()) correction = str(self.ui.tblCCorrections.item(row, 5).text()) comment = str(self.ui.tblCCorrections.item(row, 6).text()) # Check if error is empty if error == "": msg = "Error cannot be the empty." QtGui.QMessageBox.critical(self, "Error", msg) return # Check if error and correction are the same if error == correction: msg = "Error and correction cannot be the same." QtGui.QMessageBox.critical(self, "Error", msg) return # If mode is RegEx, check for validity if modeid == 1: if not self.string.test(str(error), str(correction)): QtGui.QMessageBox.critical(self, "Error", self.string.error) return # Update correction ok = self.data.update("corrections", [id, modeid, listid, varia, error,\ correction, comment]) if ok: msg = "Correction updated." self.ui.statusBar.showMessage(msg) else: self.ui.statusBar.showMessage(self.data.error) QtGui.QMessageBox.critical(self, "Error", self.data.error) # Log message def logMessage(self, msg): self.ui.lstLLog.addItem(time.strftime("%I:%M:%S") + chr(32) + msg) self.ui.lstLLog.scrollToBottom() # Enable/disable widgets def enabledisableWidgets(self, disable=False): # If disable is True, we disable everything if disable: self.ui.actionRemoveFiles.setEnabled(False) self.ui.actionClearList.setEnabled(False) self.ui.actionStart.setEnabled(False) self.ui.btnFRemoveFiles.setEnabled(False) self.ui.btnFClearList.setEnabled(False) self.ui.btnFStart.setEnabled(False) self.ui.btnLDeleteRow.setEnabled(False) self.ui.btnLInsertRow.setEnabled(False) self.ui.btnLUpdateRow.setEnabled(False) self.ui.btnCDeleteRow.setEnabled(False) self.ui.btnCInsertRow.setEnabled(False) self.ui.btnCUpdateRow.setEnabled(False) return # Fix buttons if len(self.filelist) > 0: self.ui.actionRemoveFiles.setEnabled(True) self.ui.actionClearList.setEnabled(True) self.ui.actionStart.setEnabled(True) self.ui.btnFRemoveFiles.setEnabled(True) self.ui.btnFClearList.setEnabled(True) else: self.ui.actionRemoveFiles.setEnabled(False) self.ui.actionClearList.setEnabled(False) self.ui.actionStart.setEnabled(False) self.ui.btnFRemoveFiles.setEnabled(False) self.ui.btnFClearList.setEnabled(False) # Lists and corrections buttons if self.data.database: self.ui.btnLDeleteRow.setEnabled(True) self.ui.btnLInsertRow.setEnabled(True) self.ui.btnLUpdateRow.setEnabled(True) self.ui.btnCDeleteRow.setEnabled(True) self.ui.btnCInsertRow.setEnabled(True) self.ui.btnCUpdateRow.setEnabled(True) else: self.ui.btnLDeleteRow.setEnabled(False) self.ui.btnLInsertRow.setEnabled(False) self.ui.btnLUpdateRow.setEnabled(False) self.ui.btnCDeleteRow.setEnabled(False) self.ui.btnCInsertRow.setEnabled(False) self.ui.btnCUpdateRow.setEnabled(False) # Start-button if self.data.database and len(self.filelist) > 0: self.ui.btnFStart.setEnabled(True) else: self.ui.btnFStart.setEnabled(False)
def plotField(fileName, vmin, vmax, colormap='goddard', output_folder='/', lonlatbox=None, region='global'): """ Plot any field variable :param fileName: filepath :param vmin: min value for colorbar :param vmax: max value for colorbar """ fig, ax = plt.subplots(figsize=(12, 7), dpi=500) if colormap == 'goddard': my_cmap = mpl.colors.LinearSegmentedColormap('my_colormap', Plotter.colorDict2, 256) else: my_cmap = plt.cm.RdBu_r file_values = FileHandler.openNetCDFFile(fileName) mVar = file_values['variable'] lon = file_values['lon'] lat = file_values['lat'] step = (lon[1]-lon[0])/2. lon = N.insert(lon, lon.shape[0], lon[-1]+2*step) lon = N.insert(lon, lon.shape[0], lon[-1]+2*step) lat = N.insert(lat, lat.shape[0], lat[-1]+2*step) lat = N.insert(lat, lat.shape[0], lat[-1]+2*step) mVarF = mVar[:, 0:2] mVar = N.concatenate((mVar, mVarF), axis=1) mVarF = mVar[0:2, :] mVar = N.concatenate((mVar, mVarF), axis=0) def plus(lon): return lon-step if lonlatbox is None: if lon[0] < 0 and lon[1] < 0: lonlatbox = '-180,180,-90,90' else: lonlatbox = '0,360,-90,90' lon = N.array(map(plus, lon)) lat = N.array(map(plus, lat)) lonlatbox = map(int, lonlatbox.split(',')) # check which lat is greater if lonlatbox[2] > lonlatbox[3]: lonlatbox[2], lonlatbox[3] = lonlatbox[3], lonlatbox[2] # switch position on lat entries if lonlatbox[0] > lonlatbox[1]: lonlatbox[1] += 360 step = 0 if region == 'Arctic': m = Basemap(projection='npstere', boundinglat=lonlatbox[2], lon_0=0) parallels = N.arange(0., 90., 10.) meridians = N.arange(0., 360., 10.) elif region == 'Antarctica': m = Basemap(projection='spstere', boundinglat=lonlatbox[3], lon_0=0) parallels = N.arange(-90., 0., 10.) meridians = N.arange(0., 360., 10.) else: m = Basemap(llcrnrlon=lonlatbox[0], llcrnrlat=lonlatbox[2], urcrnrlon=lonlatbox[1], urcrnrlat=lonlatbox[3]) parallels = N.arange(-90., 120., 30.) meridians = N.arange(0., 420., 60.) def divi(x): return float(x)/10 colorSteps = map(divi, range(int(vmin*10), int((vmax*10)+1), 1)) if vmax == 0.5: colorSteps = [-0.5, -0.45, -0.4, -0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5] if vmax == 0. and vmin == -0.5: colorSteps = [-0.5, -0.475, -0.45, -0.425, -0.4, -0.375, -0.35, -0.325, -0.3, -0.275, -0.25, -0.225, -0.2, -0.175, -0.15, -0.125, -0.1, -0.075, -0.05, -0.025, 0] if vmax == 2. and vmin == 0: colorSteps = [0.5, 0.55, 0.6, 0.65, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95, 1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0] if vmin == -0.6: colorSteps = [-0.6, -0.55, -0.5, -0.45, -0.4,-0.35, -0.3, -0.25, -0.2, -0.15, -0.1, -0.05, 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.45, 0.5, 0.55, 0.6] if vmin == -0.1 and vmax == 0: colorSteps = [-0.1, -0.09, -0.08, -0.07, -0.06, -0.05, -0.04, -0.03, -0.02, -0.01, 0.0] if vmin == -0.2 and vmax == 0: colorSteps = [-0.2, -0.18, -0.16, -0.14, -0.12, -0.1, -0.08, -0.06, -0.04, -0.02, 0.0] colorTicks = colorSteps[0::2] my_cmap.set_bad("grey") # set missing value color maskedArray = N.ma.masked_outside(mVar, -0.8e20, 0.8e20) # discrete colormap norm = mpl.colors.BoundaryNorm(colorSteps, my_cmap.N) x, y = lon,lat x, y = N.meshgrid(lon, lat) cs = m.pcolormesh(x, y, maskedArray, cmap=my_cmap, norm=norm, linewidth=0, rasterized=True) cs.set_edgecolor('face') cb = m.colorbar(cs, "right", size="5%", pad='5%', ticks=colorTicks) m.drawcoastlines(ax=ax) m.drawparallels(parallels, labels=[1, 0, 0, 0]) # draw parallels m.drawmeridians(meridians, labels=[0, 0, 0, 1]) # draw meridians plt.title(Plotter.__getTitle(fileName)) plt.text(lonlatbox[0]+(lon[1]-lon[0])/2, lonlatbox[2]+(lat[1]-lat[0])/2, 'MurCSS') return m
def download_file(self, file_id, group_id): path = self._tg_bot.get_file(file_id) file_location = FileHandler.download(path.file_path, group_id, self._vectronconfig['webserver_directory']) return file_location
class SceneMixer(Widget, Serializable): currentTime = NumericProperty(0.0) sceneTime = NumericProperty(0.0) fadeTime = NumericProperty(0.0) sceneNumber = StringProperty(None) totalSceneTime = NumericProperty(0.0) bpm = NumericProperty(0.0) # Properties for effects color = ListProperty((1.0, 1.0, 1.0, 1.0)) glowMax = NumericProperty(100.0) glowMin = NumericProperty(0.0) glowInterval = NumericProperty(0.0) glowOffset = BooleanProperty(False) globalSceneTime = BooleanProperty(False) globalFadeTime = BooleanProperty(False) loopScenes = BooleanProperty(True) doNotClearEffectsOnNewFrame = BooleanProperty(True) text = StringProperty("") __initFinished = False def __init__(self, **kwargs): super(SceneMixer, self).__init__(**kwargs) self.__frameHandler = FrameHandler() self.sceneTime = 0.0 self.fadeTime = 0.0 self.__currentFrame = None # init playbackHandler self.__isInPlayback = False self.__playbackHandler = PlayBackHandler(bpm=60.0, updatesPerBeat=20) self.__playbackHandler.addIntervalUpdateCallback(self.playbackCallbackUpdate) self.fileHandler = FileHandler() self.__initFinished = True self.__isInFrameChange = False self.syncedIsSetTest = False def on_bpm(self, *args): self.__playbackHandler.setBpm(args[1]) def on_color(self, obj, color): self.__beforeSetEffect() self.__setColorEffect(self.__currentFrame) def on_text(self, obj, text): self.__beforeSetEffect() self.__setTextEffect(self.__currentFrame, text) def on_glowMax(self, obj, value): self.__beforeSetEffect() self.__setColorEffect(self.__currentFrame) def on_glowOffset(self, obj, value): self.__beforeSetEffect() self.__setColorEffect(self.__currentFrame) def on_glowMin(self, obj, value): self.__beforeSetEffect() self.__setColorEffect(self.__currentFrame) def on_glowInterval(self, obj, value): self.__beforeSetEffect() self.__setColorEffect(self.__currentFrame) def __beforeSetEffect(self): if self.__currentFrame is None: self.addScene() # COLOR EFFECT def __setColorEffect(self, frame=None): if not self.__isInFrameChange: colorEffectObj = frame.getEffect(EffectNames.COLOR_EFFECT) if colorEffectObj is None: colorEffectObj = ColorEffect() frame.addEffect(colorEffectObj) colorEffectObj.setKivyColor(self.color) colorEffectObj.setGlowMax(self.glowMax) colorEffectObj.setGlowMin(self.glowMin) colorEffectObj.setGlowInterval(self.glowInterval) colorEffectObj.setGlowOffset(self.glowOffset) # TEXT EFFECT def __setTextEffect(self, frame, text): textEffectObj = frame.getEffect(EffectNames.TEXT_EFFECT) if textEffectObj is None: textEffectObj = TextEffect() frame.addEffect(textEffectObj) textEffectObj.setText(text) def playbackCallbackUpdate(self, *args): self.currentTime = args[0] if self.currentTime >= self.sceneTime: atEnd = self.__frameHandler.isAtEndOfFrames() self.__playbackHandler.reset() if atEnd and self.loopScenes: self.gotoStartOfShow() elif atEnd: self.startStopPlayback() else: self.gotoNextScene() def on_sceneTime(self, object, value): if self.__initFinished: if not self.__isInFrameChange: if self.sceneTime < self.fadeTime: self.fadeTime = self.sceneTime self.__updateSceneAndFadeTimes() def on_fadeTime(self, object, value): if self.__initFinished: if not self.__isInFrameChange: if self.fadeTime > self.sceneTime: self.sceneTime = self.fadeTime self.__updateSceneAndFadeTimes() def addScene(self): self.__before_create_scene() sceneFrame = self.__createNewScene() result = (self.__frameHandler.addFrameAtEnd(sceneFrame)) self.__setDisplayValues(result) return result def insertSceneBefore(self): self.__before_create_scene() sceneFrame = self.__createNewScene() result = (self.__frameHandler.insertFrameBeforePointer(sceneFrame)) self.__setDisplayValues(result) return result def insertSceneAfter(self): self.__before_create_scene() sceneFrame = self.__createNewScene() result = (self.__frameHandler.insertFrameAfterPointer(sceneFrame)) self.__setDisplayValues(result) return result def __before_create_scene(self): if self.sceneTime == 0.0: self.sceneTime = 1.0 def __createNewScene(self): sceneFrame = SceneFrame() sceneFrame.setSceneTime(self.sceneTime) sceneFrame.setFadeTime(self.fadeTime) if self.doNotClearEffectsOnNewFrame: self.__setColorEffect(sceneFrame) self.__setTextEffect(sceneFrame, self.text) return sceneFrame def startStopPlayback(self, *args): self.__isInPlayback = not self.__isInPlayback if self.__isInPlayback: self.__resetPlayback() self.__playbackHandler.start() else: self.__playbackHandler.stop() self.__resetPlayback() def deleteScene(self): result = (self.__frameHandler.deleteCurrentFrame()) self.__setDisplayValues(result) return result def gotoStartOfShow(self): self.__resetPlayback() result = (self.__frameHandler.moveFramePointerToStart()) self.__setDisplayValues(result) return result def __resetPlayback(self): self.currentTime = 0.0 self.__playbackHandler.reset() def gotoEndOfShow(self): self.__resetPlayback() result = (self.__frameHandler.moveFramePointerToEnd()) self.__setDisplayValues(result) return result def gotoPrevScene(self): self.__resetPlayback() result = (self.__frameHandler.moveFramePointerDown()) self.__setDisplayValues(result) return result def gotoNextScene(self): self.__resetPlayback() result = (self.__frameHandler.moveFramePointerUp()) self.__setDisplayValues(result) return result def toggleClearValuesOnNewScene(self, value): self.doNotClearEffectsOnNewFrame = value def toggleGlobalSceneTime(self, value): self.globalSceneTime = value self.__updateSceneAndFadeTimes() def toggleGlobalFadeTime(self, value): self.globalFadeTime = value self.__updateSceneAndFadeTimes() def toggleLoopScenes(self, value): self.loopScenes = value # TODO RENAME OR REFACTOR def __setDisplayValues(self, currentFrameData): frame = currentFrameData[FrameHandler.FRAME_OBJ_IDX] self.__currentFrame = frame if frame is not None: self.__isInFrameChange = True self.sceneTime = frame.getSceneTime() self.fadeTime = frame.getFadeTime() self.__isInFrameChange = False framePos = currentFrameData[FrameHandler.FRAME_POS_IDX] numFrames = currentFrameData[FrameHandler.FRAME_NUM_IDX] self.sceneNumber = ("%d:%d" % (framePos, numFrames)) self.__setCurrentFrameEffects() self.totalSceneTime = self.__getTotalSceneTime() def __updateSceneAndFadeTimes(self): self.__updateCurrentFrame() if self.globalFadeTime: self.__setGlobalFadeTime() if self.globalSceneTime: self.__setGlobalSceneTime() self.totalSceneTime = self.__getTotalSceneTime() def __updateCurrentFrame(self): if self.__currentFrame is not None: if not self.__isInFrameChange: self.__currentFrame.setSceneTime(self.sceneTime) self.__currentFrame.setFadeTime(self.fadeTime) def __setCurrentFrameEffects(self): if self.__currentFrame is not None: # COLOR EFFECT # TODO SET COLOR WIDGET OFF IF NOT SET colorEffect = self.__currentFrame.getEffect(EffectNames.COLOR_EFFECT) self.__isInFrameChange = True self.color = colorEffect.getKivyColor() if colorEffect is not None else RGBA(0, 0, 0) self.on_color(None, self.color) self.glowMin = colorEffect.getGlowMin() if colorEffect is not None else 0.0 self.on_glowMin(None, self.glowMin) self.glowMax = colorEffect.getGlowMax() if colorEffect is not None else 100.0 self.on_glowMax(None, self.glowMax) self.glowInterval = colorEffect.getGlowInterval() if colorEffect is not None else 0.0 self.on_glowInterval(None, self.glowInterval) self.glowOffset = colorEffect.getGlowOffset() if colorEffect is not None else False self.on_glowOffset(None, self.glowOffset) # TEXT EFFECT textEffect = self.__currentFrame.getEffect(EffectNames.TEXT_EFFECT) self.text = textEffect.getText() if textEffect is not None else "" self.on_text(None, self.text) self.__isInFrameChange = False def __setGlobalSceneTime(self): self.__setSceneTimeForAllFrames(self.sceneTime) def __setGlobalFadeTime(self): self.__setFadeTimeForAllFrames(self.fadeTime) def __setSceneTimeForAllFrames(self, sceneTime): self.__frameHandler.forAllFramesDo(self.__iterFuncSetSceneTime, sceneTime) def __setFadeTimeForAllFrames(self, fadeTime): self.__frameHandler.forAllFramesDo(self.__iterFuncSetFadeTime, fadeTime) def __getTotalSceneTime(self): self.totalSceneTime = 0.0 self.__frameHandler.forAllFramesDo(self.__iterFuncCalcTotSceneTime) return self.totalSceneTime def __iterFuncSetSceneTime(self, sceneFrame, sceneTime): sceneFrame.setSceneTime(sceneTime) def __iterFuncSetFadeTime(self, sceneFrame, fadeTime): sceneFrame.setFadeTime(fadeTime) def __iterFuncCalcTotSceneTime(self, sceneFrame, *args): self.totalSceneTime += sceneFrame.getSceneTime() def serialize_to_dict(self): serializedDict = Serializable.serialize_to_dict(self) serializedDict = dict(serializedDict.items() + self.__frameHandler.serialize_to_dict().items()) return serializedDict def deserializer_from_dict(self, showData): return self.__frameHandler.deserializer_from_dict(showData) def loadShow(self): popup = Popups.yesNoPopUp(titleLabel="Clear current show and load new", text="Are you sure you want to delete the current show \n" "and load a new from disk?", yesCallback=self.__loadShow) popup.open() def __loadShow(self, deleteCurrent=True, *args): self.fileHandler.show_load(self.__onLoadSuccessCallback) if deleteCurrent: self.__clearCurrentShow() def __onLoadSuccessCallback(self, data): self.deserializer_from_dict(json.loads(data)) self.gotoStartOfShow() def saveShow(self): show = json.dumps(self.serialize_to_dict()) self.fileHandler.show_save(show) def appendShow(self): self.gotoEndOfShow() self.__loadShow(deleteCurrent=False) def insertShow(self): self.__loadShow(deleteCurrent=False) def clearShow(self): popup = Popups.yesNoPopUp(titleLabel="Clear current show", text="Are you sure you want to delete the current show?", yesCallback=self.__clearCurrentShow) popup.open() def __clearCurrentShow(self, *args): self.__playbackHandler.stop() self.__resetPlayback() result = (self.__frameHandler.deleteAllFrames()) self.__setDisplayValues(result) self.globalFadeTime = False self.globalSceneTime = False self.doNotClearEffectsOnNewFrame = False
def calcMissingValueMask(self, observations): ''' Calculates a missing value mask using the observations. At the moment all gridpoints are masked at missing value where at least one value is missing :TODO: find a less strict solution. Maybe 10% available? DONE! :param observations: dict with all observations :return netcdf file with missing values mask 1/0 ''' # for year in self.decadals: # # tmp_array = FileHandler.openNetCDFFile(observations[year], 'var') # tmp_array = np.expand_dims(tmp_array, axis=2) # try: # master = np.concatenate((master,tmp_array),axis=2) # except NameError: # master = tmp_array # missing_value_mask = np.zeros((master.shape[0],master.shape[1])) # #This loop is probably not the best idea # #TODO: Change to numpy function # for x in xrange(master.shape[0]): # for y in xrange(master.shape[1]): # missing_count=0 # for i in xrange(len(self.decadals)): # # if master[x,y,i] > 0.9e20: # missing_count+=1 # if missing_count > round(0.1*len(self.decadals)): # missing_value_mask[x,y] = 1e20 # else: # missing_value_mask[x,y] = 1 # #print missing_count # misval_new = FileHandler.saveToNetCDF(missing_value_mask, observations.values()[0], 'missing_value_new') for year in self.decadals: tmp_array = FileHandler.openNetCDFFile(observations[year], 'var') #tmp_array = np.expand_dims(tmp_array, axis=2) tmp_array_1d = tmp_array.reshape((1,tmp_array.size)) try: master = np.concatenate((master,tmp_array_1d)) except NameError: orig_shape = tmp_array.shape master = tmp_array_1d #sum along axis sum_array = np.sum(master,axis=0) #missing_value_mask = np.zeros((1,tmp_array.size)) missing_value_mask = np.where(sum_array<0.1*(len(self.decadals)*1e20),sum_array,1e20) missing_value_mask = np.where(sum_array>=0.1*(len(self.decadals)*1e20),missing_value_mask,1) #print missing_count missing_value_mask = np.reshape(missing_value_mask, orig_shape) misval_new = FileHandler.saveToNetCDF(missing_value_mask, observations.values()[0], 'missing_value_new2') misval_new = cdo.timmean(input=misval_new, output=misval_new+'timmean') # tmpList = list() # for year in self.decadals: # #Mark every gridpoint as missing, if it is missing once # tmpList.append(cdo.timavg(input=observations[year], output=self.tmpDir+'misvalmask_tmp'+str(year))) # #Mark only as missing if it is missing the whole year # print observations[year] # #tmpList.append(cdo.yearmean(input=observations[year], output=self.tmpDir+'misvalmask_tmp'+str(year))) # misval_tmp = cdo.mergetime(input=' '.join(tmpList), output=self.tmpDir+'misvalmask_tmp_all') # misval = cdo.timavg(input=misval_tmp, output=self.tmpDir+'misvalmask_all_years') # misval_old = cdo.setrtoc('-1000,1000,1', input=misval, output=self.tmpDir+'missingValueMask.nc') return misval_new
class FileHandlerTests(unittest.TestCase): def setUp(self): self.fh = FileHandler() def test_take_order(self): self.assertTrue(self.fh.take_order("Person", 5)) self.assertEqual({"Person": 5}, self.fh.orders) def test_take_second_order(self): self.assertTrue(self.fh.take_order("Person", 5)) self.assertTrue(self.fh.take_order("Person", 10)) self.assertEqual({"Person": 15}, self.fh.orders) def test_list_orders(self): self.assertTrue(self.fh.take_order("Person", 5)) self.assertEqual("Person - 5", self.fh.list_orders()) def test_fetch_files(self): self.assertEqual([], self.fh.fetch_files()) def test_save_file(self): self.assertTrue(self.fh.take_order("Person", 5)) self.assertTrue(self.fh.save_file()) files = self.fh.fetch_files() self.assertEqual(1, len(files)) def test_fetch_file_name(self): self.assertTrue(self.fh.save_file()) self.fh.list_files() self.assertEqual(self.fh.files[0], self.fh.fetch_file_name(0)) def test_fetch_history(self): self.assertTrue(self.fh.take_order("Person", 5)) self.assertEqual("take", self.fh.fetch_history()) def tearDown(self): for filename in glob("./orders_*"): remove(filename)
def test_load_4x4_puzzle(self): filename = 'Puzzle-4x4-0001.txt' puzzle = FileHandler.create_puzzle(filename) self.assertEqual(4,puzzle.size)
def test_load_16x16_puzzle(self): filename = 'Puzzle-16x16-0001-Inconsistant.txt' puzzle = FileHandler.create_puzzle(filename) self.assertEqual(16,puzzle.size)
def test_load_9x9_puzzle(self): filename = 'Puzzle-9x9-0001.txt' puzzle = FileHandler.create_puzzle(filename) self.assertEqual(9,puzzle.size)
def json2dict(filename): raw_data = FileHandler.read(filename) return json.loads(raw_data)
# -*- coding: utf-8 -*- import os import sys from ctypes import c_int8 import operator from new import classobj import configuration from filehandler import FileHandler from lists.pokemonnamelist import PokemonNameList from lists.movenamelist import MoveNameList from lists.itemnamelist import ItemNameList file_trainer = FileHandler() file_pkmns = FileHandler() def GetPokemonName(species): if species <= 493: return PokemonNameList[species].upper() else: return PokemonNameList[0].upper() def GetMoveName(move): if move <= 467: return MoveNameList[move].upper() else: return MoveNameList[0].upper()
# -*- coding: utf-8 -*- import os import sys from ctypes import c_int8 import operator from new import classobj import configuration from filehandler import FileHandler from lists.typelist import TypeList input_file = FileHandler() from lists.pokemonnamelist import PokemonNameList from lists.movenamelist import MoveNameList from lists.itemnamelist import ItemNameList from lists.abilitynamelist import AbilityNameList from lists.typelist import TypeList def GetCategoryName(type): if type == 0: return "Physical".upper() elif type == 1: return "Special".upper() elif type == 2: return "Status".upper() else: return str(type)
import json from argumentparser import ArgumentParser from filehandler import FileHandler from jsonmerger import JSONMerger def json2dict(filename): raw_data = FileHandler.read(filename) return json.loads(raw_data) if __name__ == '__main__': parser = ArgumentParser() args = parser.parse_arguments() students_data = json2dict(args['students_file']) rooms_data = json2dict(args['rooms_file']) rooms = JSONMerger.merge(students_data, rooms_data) serialized_data = args['output_format_serializer'].serialize(rooms) FileHandler.write(serialized_data, './output/rooms' + '.' + args['output_format'])
class CLI(): """docstring for CLI""" def __init__(self): self.cp = CommandParser() self.fh = FileHandler() self._init_callbacks() self._loop() def callback_create_attendance_file(self, arguments): if self.fh.create_attendance_file() is True: return "New file created and loaded: {}".format( self.fh.get_current_open_file()) elif self.fh.create_attendance_file() is False: return "You already have a file for today it is: {}".format( self.fh.get_current_open_file()) def callback_change_current_date(self, arguments): if self.fh.change_current_date(arguments) is True: return "Date changed to {}/{}/{}\nCurrent file saved & discarded.\nYou can create or load.".format( arguments[0], arguments[1], arguments[2]) def callback_add_attendance(self, arguments): try: student = "{} {}".format(arguments[0], arguments[1]) except IndexError: return "Error: Please enter student as FirstName LastName" if self.fh.is_student_attending(student) is True: return "{} is already attending!".format(student) elif self.fh.is_student_attending(student) is False: if self.fh.add_attendance(student) is True: return "{} is now attending!".format(student) elif self.fh.add_attendance(student) is False: return "Create or load a file before adding!" elif self.fh.is_student_attending(student) is None: return "{} is not in our records.".format(student) def callback_list_files(self, arguments): return self.fh.list_attendance_files() def callback_help(self, arguments): help_message = [ "Available commands are:", "* create", "* change_date", "* add", "* list", "* load", "* status", "* statistics" ] return "\n".join(help_message) def callback_load(self, arguments): try: file_id = arguments[0] except IndexError: return "Error: No ID given" if self.fh.load_attendance_file(file_id) is True: return "Loaded {}".format(self.fh.get_current_open_file()) elif self.fh.load_attendance_file(file_id) is False: return "Error: Invalid ID. Use list before using load." def callback_status(self, arguments): return self.fh.list_attending_students() def callback_statistics(self, arguments): return self.fh.get_statistics() def _init_callbacks(self): self.cp.on("create", self.callback_create_attendance_file) self.cp.on("change_date", self.callback_change_current_date) self.cp.on("add", self.callback_add_attendance) self.cp.on("list", self.callback_list_files) self.cp.on("help", self.callback_help) self.cp.on("load", self.callback_load) self.cp.on("status", self.callback_status) self.cp.on("statistics", self.callback_statistics) def _loop(self): while True: command = input("command>") self.cp.take_command(command)
def run(self, config): """Main application logic""" self.logger.debug("Scanning for files") fileHandler = FileHandler() # Find the image frames files = fileHandler.find_files(config) if len(files) == 0: raise Exception("No image files found") self.logger.debug("Done, found {} files".format(len(files))) files = fileHandler.filter_files(files, config) self.logger.debug("Filtered to {} files".format(len(files))) # And now we have to recalculate final FPS and duration fps, duration = self.get_fps_duration(files, config) self.logger.info( "Resulting file will have {fps:.2f} FPS and last {duration}" .format(**{ "fps": fps, "duration": str(datetime.timedelta(seconds=duration)) }) ) # Figure out the frame size for the video, picking the size of the # first image frameSize = media.ImageHandler().get_size(files[0]) self.logger.info( "Frames will be {width}x{height}".format(**{ "width": frameSize[0], "height": frameSize[1] }) ) # Start video handler video = media.VideoHandler( config["codec"], config["fps"], frameSize ) video.open(config["outFile"]) # Go through frames for i, file in enumerate(files): # Write video.write_frame(file) # Update user occasionally about our progress if i % math.floor(config["fps"]) == 0: duration = i / config["fps"] self.logger.info( "Encoded {duration}".format(**{ "duration": str(datetime.timedelta(seconds=duration)) }) )
def setup_file_handler(self): self.handler = FileHandler() self.file_queue = Queue() self.proc = Process(target = self.handler.handler_queue, args = (self.file_queue,)) self.proc.start()
def __init__(self): self.filehandler = FileHandler(config.task_file) self.task_list = self.filehandler.parse_file() self.current_task_index = 0
def loadFunction(self, filePath: str = ""): """ loads all files (.tsv .ini, .fasta) from a given directory. If .tsv file is not present the experimental design is filled with mzMl files If no .ini file is present default ini file is written and is loaded automatically """ if not filePath: dlg = QFileDialog(self) filePath = dlg.getExistingDirectory() self.loaded_dir = filePath self.sview.fillTable(filePath) if filePath: try: self.tsvfiles = glob.glob('*.tsv') if len(self.tsvfiles) > 1: QMessageBox.about( self, "Sorry!", "There are multiple '.tsv-'" "files in the specified folder. " "Please choose the one you intent " "to use.") dial = QFileDialog(self) newFilePath = dial.getOpenFileName(self, "Choose .tsv", filePath, "Tables (*.tsv)") if newFilePath[0] != '': newFile = newFilePath[0].split("/")[-1] self.tsvfiles = [newFile] else: QMessageBox.about( self, "Sorry!", "Nothing was choosen. " "Therefore no '.tsv'-file was " "loaded. ") self.tsvfiles = [] for file in self.tsvfiles: df = fh.importTable(self.tview, file) Tdf.setTable(self.tview, df) self.tview.drawTable() self.loaded_table = file self.tablefile_loaded = True except TypeError: "No '.tsv' or '.csv'-file could be loaded." if self.tablefile_loaded is False: try: self.tview.loadDir(filePath) self.tablefile_loaded = True except TypeError: print("Could not load '.mzMl'-files.") try: self.iniFiles = glob.glob('*.ini') if len(self.iniFiles) > 1: QMessageBox.about( self, "Sorry!", "There are multiple '.ini'-" "files in the specified folder. " "Please choose the one you intent " "to use.") dial = QFileDialog(self) newFilePath = dial.getOpenFileName(self, "Choose .ini", filePath, "Config (*.ini)") if newFilePath[0] != '': newFile = newFilePath[0].split("/")[-1] self.iniFiles = [newFile] else: QMessageBox.about( self, "Sorry!", "Nothing was choosen. " "Therefore no '.ini'-file was " "loaded. ") self.iniFiles = [] for file in self.iniFiles: self.cview.generateTreeModel(file) self.loaded_ini = file self.ini_loaded = True except TypeError: print("Could not load .ini file.") if self.ini_loaded is False: try: runcall = "ProteomicsLFQ " writeIniFile = "-write_ini " out = "Config.ini" command = (runcall + writeIniFile + out) os.chdir(filePath) os.system(command) iniFiles = glob.glob('*.ini') for file in iniFiles: self.cview.generateTreeModel(file) self.loaded_ini = file self.ini_loaded = True except TypeError: print("Could not write and load default '.ini'-file.") try: self.fastafiles = glob.glob('*fasta') if len(self.fastafiles) > 1: QMessageBox.about( self, "Sorry!", "There are multiple '.fasta'-" "files in the specified folder. " "Please choose the one you intent " "to use.") dial = QFileDialog(self) newFilePath = dial.getOpenFileName( self, "Choose .fasta", filePath, "Proteindata (*.fasta)") if newFilePath[0] != '': newFile = newFilePath[0].split("/")[-1] self.fastafiles = [newFile] else: QMessageBox.about( self, "Sorry!", "Nothing was choosen. " "Therefore, no '.fasta'-file " "was loaded. ") self.fastafiles = [] for file in self.fastafiles: self.fview.loadFile(file) self.loaded_fasta = file self.fasta_loaded = True except TypeError: print("Could not load '.fasta'-file.")
try: peerServerConfig = PeerServerConfigHandler() peerServerConfig.parseConfig() temp_dir = peerServerConfig.getTempDirPath() tracker_host = peerServerConfig.getTrackerHost() tracker_port = peerServerConfig.getTrackerPort() tracker_server_address = (tracker_host, tracker_port) peer_server_host = '' peer_server_port = peerServerConfig.getPeerServerPort() peer_server_address = (peer_server_host, peer_server_port) # port used by peer-server to communicate with tracker-server bind_port = peerServerConfig.getServerTrackerBindPort() filehandle = FileHandler() filehandle.create_dir(temp_dir) server = ThreadedPeerServer(peer_server_address) # register the server with tracker server.registerWithTracker(tracker_server_address, bind_port) # listen for download requests from client server.listen(temp_dir, peerServerConfig.getProxy()) except: print("Oops!", sys.exc_info(), "occured.") finally:
def download_file(self, file_id, group_id): path = self._tg_bot.get_file(file_id) file_location = FileHandler.download(path.file_path, group_id) return file_location
# -*- coding: utf-8 -*- import os import sys from ctypes import c_int8 import operator from new import classobj import configuration from filehandler import FileHandler from lists.typelist import TypeList input_file = FileHandler() def GetCategoryName(type): if type == 0: return "Physical".upper() elif type == 1: return "Special".upper() elif type == 2: return "Status".upper() else: return str(type) def GetContestName(type): if type == 0: return "Cool".upper() elif type == 1: return "Beautiful".upper() elif type == 4:
def __init__(self): # Declare class variables self.encodings = [ ["Default", None], ["ASCII", "ascii"], ["ISO-8859-1", "latin_1"], ["ISO-8859-2", "iso8859_2"], ["ISO-8859-3", "iso8859_3"], ["ISO-8859-4", "iso8859_4"], ["ISO-8859-5", "iso8859_5"], ["ISO-8859-6", "iso8859_6"], ["ISO-8859-7", "iso8859_7"], ["ISO-8859-8", "iso8859_8"], ["ISO-8859-9", "iso8859_9"], ["ISO-8859-10", "iso8859_10"], ["ISO-8859-13", "iso8859_13"], ["ISO-8859-14", "iso8859_14"], ["ISO-8859-15", "iso8859_15"], ["KOI8-R", "koi8_r"], ["KOI8-U", "koi8_u"], ["UTF-8 with BOM", "utf_8_sig"], ["UTF-8 without BOM", "utf_8"], ["UTF-16BE", "utf_16_be"], ["UTF-16LE", "utf_16_le"], ["UTF-32BE", "utf_32_be"], ["UTF-32LE", "utf_32_le"], ["Windows-1250", "cp1250"], ["Windows-1251", "cp1251"], ["Windows-1252", "cp1252"], ["Windows-1253", "cp1253"], ["Windows-1254", "cp1254"], ["Windows-1255", "cp1255"], ["Windows-1256", "cp1256"], ["Windows-1257", "cp1257"], ["Windows-1258", "cp1258"] ] self.path = "" # Path for fix self.filelist = [] # File list for fix self.filetypes = ["srt", "txt"] # Accepted file types self.typefilter = "All supported files (*.srt *.txt)" self.typefilter += ";;SubRip subtitle file (*.srt)" self.typefilter += ";;Text file (*.txt)" # Create class instances self.data = DataHandler() self.file = FileHandler(self.filetypes) self.string = StringHandler() # Initialize top level window widget QtGui.QMainWindow.__init__(self) # This is always the same self.ui = Ui_MainWindow() self.ui.setupUi(self) # Connect signals, menu and toolbar self.ui.actionNewDatabase.triggered.connect(self.newDatabase) self.ui.actionOpenDatabase.triggered.connect(self.openDatabaseMsgBox) self.ui.actionAddFiles.triggered.connect(self.fAddFiles) self.ui.actionAddFolder.triggered.connect(self.fAddFolder) self.ui.actionRemoveFiles.triggered.connect(self.fRemoveFiles) self.ui.actionClearList.triggered.connect(self.fClearList) self.ui.actionStart.triggered.connect(self.fStart) self.ui.actionQuit.triggered.connect(self.quitApplication) self.ui.actionAbout.triggered.connect(self.aboutMessage) # Connect signals, buttons self.ui.btnFStart.clicked.connect(self.fStart) self.ui.btnFAddFiles.clicked.connect(self.fAddFiles) self.ui.btnFAddFolder.clicked.connect(self.fAddFolder) self.ui.btnFRemoveFiles.clicked.connect(self.fRemoveFiles) self.ui.btnFClearList.clicked.connect(self.fClearList) self.ui.btnLDeleteRow.clicked.connect(self.lDeleteRow) self.ui.btnLInsertRow.clicked.connect(self.lInsertRow) self.ui.btnLUpdateRow.clicked.connect(self.lUpdateRow) self.ui.txtCSearch.returnPressed.connect(self.refreshCCorrections) self.ui.btnCDeleteRow.clicked.connect(self.cDeleteRow) self.ui.btnCInsertRow.clicked.connect(self.cInsertRow) self.ui.btnCUpdateRow.clicked.connect(self.cUpdateRow) self.ui.btnLClearLog.clicked.connect(self.lClearLog) # Connect signals, comboboxes self.ui.cboCCorrectionMode.activated.connect(self.refreshCCorrections) self.ui.cboCList.activated.connect(self.refreshCCorrections) # Connect signals, tables self.ui.tblCCorrections.currentCellChanged[(int,int,int,int)].connect(\ self.selectCorrection) # Drag-and-drop events for file list self.ui.tblFFileList.dragEnterEvent = self.dragEnterEvent self.ui.tblFFileList.dragMoveEvent = self.dragEnterEvent self.ui.tblFFileList.dropEvent = self.dropEvent # Loop and set encodings to settings combobox for item in self.encodings: self.ui.cboSFileEncoding.addItem(item[0]) # Select the first tab from the tab widgets self.ui.tabMain.setCurrentIndex(0) # Icons self.setWindowIcon(QtGui.QIcon("icon.png")) self.ui.actionNewDatabase.setIcon(QtGui.QIcon("new_database.png")) self.ui.actionOpenDatabase.setIcon(QtGui.QIcon("open_database.png")) self.ui.actionAddFiles.setIcon(QtGui.QIcon("add_files.png")) self.ui.actionAddFolder.setIcon(QtGui.QIcon("add_folder.png")) self.ui.actionRemoveFiles.setIcon(QtGui.QIcon("remove.png")) self.ui.actionClearList.setIcon(QtGui.QIcon("clear.png")) self.ui.actionStart.setIcon(QtGui.QIcon("start.png")) self.ui.actionQuit.setIcon(QtGui.QIcon("quit.png")) self.ui.actionAbout.setIcon(QtGui.QIcon("about.png")) self.ui.tabMain.setTabIcon(0, QtGui.QIcon("fix.png")) self.ui.tabMain.setTabIcon(1, QtGui.QIcon("lists.png")) self.ui.tabMain.setTabIcon(2, QtGui.QIcon("corrections.png")) self.ui.tabMain.setTabIcon(3, QtGui.QIcon("log.png")) self.ui.tabMain.setTabIcon(4, QtGui.QIcon("settings.png")) self.ui.btnFAddFiles.setIcon(QtGui.QIcon("add_files.png")) self.ui.btnFAddFolder.setIcon(QtGui.QIcon("add_folder.png")) self.ui.btnFRemoveFiles.setIcon(QtGui.QIcon("remove.png")) self.ui.btnFClearList.setIcon(QtGui.QIcon("clear.png")) self.ui.btnFStart.setIcon(QtGui.QIcon("start.png")) self.ui.btnLDeleteRow.setIcon(QtGui.QIcon("delete_row.png")) self.ui.btnLInsertRow.setIcon(QtGui.QIcon("insert_row.png")) self.ui.btnLUpdateRow.setIcon(QtGui.QIcon("update_row.png")) self.ui.btnCDeleteRow.setIcon(QtGui.QIcon("delete_row.png")) self.ui.btnCInsertRow.setIcon(QtGui.QIcon("insert_row.png")) self.ui.btnCUpdateRow.setIcon(QtGui.QIcon("update_row.png")) self.ui.btnLClearLog.setIcon(QtGui.QIcon("clear.png")) # Check if command line arguments has files in it if sys.argv[1:]: self.loopItems(sys.argv[1:]) # Load settings self.loadSettings()
def __init__(self): self.cp = CommandParser() self.fh = FileHandler() self._init_callbacks() self._loop()
class Inbox(): """ The inbox for unprocessed tasks """ def __init__(self): self.filehandler = FileHandler(config.inbox_file) self.inbox_contents = self.filehandler.parse_text_file() self.command_parser = None ############################################################################ # Inbox commands ############################################################################ def add_to_inbox(self, description): """ Add something to the inbox """ self.inbox_contents.append(description) def display(self, remaining_command): """ Display the contents of the inbox on the screen """ for line_number, line in enumerate(self.inbox_contents): print(line_number, line.rstrip()) return remaining_command def process_item(self, position): """ Process a single inbox item """ previous_mode = self.command_parser.get_current_mode() self.command_parser.switch_to_main_mode() while True: try: command = input(self.command_parser.get_prompt()) self.command_parser.breakout(command) except StopIteration: # Item processed break self.command_parser.set_mode(previous_mode) def process_all(self, remaining_command): """ Process the inbox, from top (oldest) to bottom (newest) """ for line_number, line in enumerate(self.inbox_contents): print("Item to process: " + line.rstrip()) self.process_item(line_number) self.inbox_contents = [] return remaining_command ############################################################################ def close(self): """ Closes the inbox by writing current state to file """ self.filehandler.write_to_text_file(self.inbox_contents)
class TkView(object): ''' TkView represents the main application window ''' def __init__(self): ''' Constructor: Expects no arguments ''' self.root = Tk() self.root.wm_title(APP_NAME) self.set_up_menu_bar() self.set_up_text() def set_up_menu_bar(self): ''' Sets up the main menu bar and submenus ''' menubar = Menu(self.root) filemenu = Menu(menubar, tearoff=0) filemenu.add_command(label="Open", command=self.open_file) filemenu.add_command(label="Save", command=self.save_file) filemenu.add_command(label="Save as", command=self.save_as_file) filemenu.add_separator() filemenu.add_command(label="Exit", command=self.root.quit) helpmenu = Menu(menubar, tearoff=0) helpmenu.add_command(label="About", command=self.about) menubar.add_cascade(label="File", menu=filemenu) menubar.add_cascade(label="Help", menu=helpmenu) self.root.config(menu=menubar) def set_up_text(self): ''' Sets up the text widget, scrollbar and configures tags ''' self.text = Text(self.root) self.text.pack(side=LEFT, fill=BOTH, expand=YES) self.filehandler = FileHandler(self.text) self.text.tag_config("sel", background="green", foreground="blue") scrollbar = Scrollbar(self.root, orient=VERTICAL, command=self.text.yview) scrollbar.pack(side=RIGHT, fill=Y) self.text["yscrollcommand"] = scrollbar.set def update_title_bar(self): ''' Updates title bar with file information ''' if self.filehandler.filename: new_title_string = "{} ({})".format(APP_NAME, self.filehandler.filename) self.root.wm_title(new_title_string) def open_file(self): ''' Opens a new file and saves the reference ''' self.filehandler.open_file() self.update_title_bar() def save_as_file(self): ''' Similar to save, but saves file under a new name or type ''' self.filehandler.save_as() self.update_title_bar() def save_file(self): ''' Saves a file and saves reference ''' self.filehandler.save_file() self.update_title_bar() def about(self): ''' Opens information about the application and author ''' new_pop_up("About Notepyad", "Notepyad: a simple notepad-like editor\n\n" + "Wrote by Corey Prophitt <*****@*****.**>") def run(self): ''' Begins main loop ''' self.root.mainloop()
import discord import json import asyncio from discord.ext import commands from filehandler import FileHandler from jsonhandler import JsonHandler jh = JsonHandler() fh = FileHandler() class ShopsStockAdd(commands.Cog): def __init__(self, bot): self.bot = bot self.load_data() def load_data(self): self.worlditems = fh.load_file('worlditems') # self.currency = fh.load_file('currency') self.shops = fh.load_file('shops') def s_s_t(self): return jh.show_shop_titles() def s_s_sid(self): return jh.show_shop_stockid() def s_wi_t(self): return jh.show_worlditem_titles()
def setUp(self): self.fh = FileHandler()
def setUp(self): # Arrange self._uut = FileHandler()
from itertools import chain from flask import Flask, render_template, url_for, request, redirect, send_file from blockchain import Blockchain from transaction import Transaction from zipfile38 import ZipFile from filehandler import FileHandler blockchain = Blockchain() filer = FileHandler() loggedin = None app = Flask(__name__) csr_cache = {} user_returned_tokens = {} ROOTCA = blockchain.createMiner() @app.route("/getcert") def getcert(): return render_template("getcert.html") @app.route("/getcert1", methods=["POST"]) def getcert1(): name = request.form.get('name') if name is not None: for block in reversed(blockchain.chain): if block.crt == 'revoke': break if name == block.domain: if filer.file_exists( filer.path_join(app.root_path, "certificates",
def poly_1(x, a, b): return a * x + b def poly_2(x, a, b, c): return a * x**2 + b * x + c def poly_3(x, a, b, c, d): return a * x**3 + b * x**2 + c * x + d if __name__ == "__main__": fh = FileHandler() config = fh.readConfig('config') # for key in config: # print(key) dh = DataHandler() sic_plot_type = dh.parse_plt_type(config[1]['sic_color'], config[1]) si_plot_type = dh.parse_plt_type(config[1]['si_color'], config[1]) csv_path = './' csv_path = csv_path + config[1]['filename'] catch = fh.readCSV(csv_path, 'str', True, ';') sic_data, si_data = dh.parse_csv_data(catch) #print(sic_data)
train_dir = Path(TRAIN_DIR) train_dir.mkdir(parents=True, exist_ok=True) if len(list(train_dir.glob('run_*'))) == 0: run_num = 1 else: last_path = sorted(list(train_dir.glob('run_*')))[-1] last_run = int(str(last_path).split("_")[-1]) run_num = last_run + 1 dir_name = 'run_{:02d}'.format(run_num) run_dir = train_dir / dir_name run_dir.mkdir() #Split training and test data fh = FileHandler(DATA_DIR, IMAGE_DIR, XML_DIR, train_size=0.8) fh.split_data() #Create Tf-Records for input data that will be used for training train_data = TFRecord(data_dir = DATA_DIR, xml_dir = str(Path(DATA_DIR, 'train')), image_dir = str(Path(DATA_DIR, 'train')), write_label_map = True ) test_data = TFRecord(data_dir = DATA_DIR, xml_dir = str(Path(DATA_DIR, 'test')), image_dir = str(Path(DATA_DIR, 'test')), write_label_map = False )
#!/usr/bin/env python import os import sys from proto import Proto from filehandler import FileHandler import time def display_man(): print("[+] IcmpFT - ICMP File Transfer") print("[+] by spl0i7 https://www.ketansingh.me") print("[+] Usage "+sys.argv[0]+" <file> <source ip> <destination ip>") proto=Proto() fh=FileHandler() start_time=time.time() max_mtu=1436 #our default MTU if(len(sys.argv) == 4): file=sys.argv[1] saddr=sys.argv[2] daddr=sys.argv[3] print("Size : "+str(os.path.getsize(file))+" bytes") pointer=max_mtu fh.send_file(file, 1436, saddr, daddr) time=time.time()-start_time print("Time : "+str(time)) speed=os.path.getsize(file)/float(time) print("Speed : "+str(speed/(1024*1024))+" MB/s") else : display_man()
class ProjectManager(): """ A class that handles a list of projects in aggregate Args: project_list (list): a list of all the current projects """ def __init__(self, task_manager=None): self.filehandler = FileHandler(config.project_file) self.project_list = self.filehandler.parse_file() self.current_project_index = None # It is possible for the project manager to perform some basic function # without a task manager, but it's not expected most of the time self.task_manager = task_manager def add_project(self, description): """ Adds a project to the manager using the description """ new_project = Project(description=description) self.project_list.append(new_project) self.current_project_index = -1 self.display_current_project() def display_current_project(self, with_tasks=False): """ Displays the current project """ self.project_list[self.current_project_index].display() if with_tasks: table = PrettyTable(['Index'] + TASK_FIELDS) for task_index, task in enumerate(self.task_manager.task_list): if (self.project_list[self.current_project_index].description in task.projects): table.add_row([task_index] + task.attributes_as_list()) print(table) def display_all_projects(self, with_tasks=False): """ Outputs a table showing all available projects, sorted by index Args: with_tasks (bool): whether to show the tasks under each project Returns: None. """ if with_tasks: table = PrettyTable( ['Project Index'] + ['P {}'.format(field) for field in PROJECT_FIELDS] + ['Task Index'] + ['T {}'.format(field) for field in TASK_FIELDS]) for project_index, project in enumerate(self.project_list): table_row = [project_index] + project.attributes_as_list() for task_index, task in enumerate(self.task_manager.task_list): if project.description in task.projects: table_row += [task_index] + task.attributes_as_list() table.add_row(table_row) else: table = PrettyTable(['Index'] + PROJECT_FIELDS + ['State']) for index, project in enumerate(self.project_list): table.add_row([index] + project.attributes_as_list() + [project.state]) print(table) def close(self): """ Closes the project manager by writing the current state to file """ self.filehandler.write_to_file(self.project_list)
from filehandler import FileHandler filename = "Puzzle-9x9-0305.txt" # filename = 'Puzzle-16x16-0001-Inconsistant.txt' # filename = 'Puzzle-25x25-0001-Inconsistant.txt' # filename = 'Puzzle-9x9-0101.txt' # filename = 'Puzzle-9x9-0203.txt' # filename = 'Puzzle-9x9-0000-Unsolvable.txt' puzzle = FileHandler.create_puzzle(filename) puzzle.solve()
class TaskManager(): """ A class that handles a list of tasks in aggregate Args: task_list (list): a list of all the current tasks """ def __init__(self): self.filehandler = FileHandler(config.task_file) self.task_list = self.filehandler.parse_file() self.current_task_index = 0 def add_task(self, description): """ Adds a task to the manager using the description """ new_task = Task(description=description) self.task_list.append(new_task) self.current_task_index = len(self.task_list) - 1 self.display_current_task() def display_task_by_index(self, task_index): """ Displays a given task, along with all subtasks """ table = PrettyTable(['Index'] + TASK_FIELDS) table.align['Index'] = "l" task = self.return_task_with_index(task_index) table.add_row([task_index] + task.attributes_as_list()) self.__add_subtasks_to_table(str(task_index), task.subtasks, table) print(table) def __add_subtasks_to_table(self, index_prefix, unique_id_string, table): """ Adds the tasks on the unique_id_string (a comma separated list) to the table, recursively allowing the presentation of subtasks """ if unique_id_string == 'None': return None unique_id_list = unique_id_string.split(',') for unique_id in unique_id_list: task_index = self.return_index_for_unique_id(unique_id) task = self.return_task_with_index(task_index) if task.state == 'closed': continue table.add_row([index_prefix + "-" + str(task_index)] + task.attributes_as_list()) self.__add_subtasks_to_table(index_prefix + "-" + str(task_index), task.subtasks, table) return None def return_index_for_unique_id(self, unique_id): """ Finds the current index of the task with the specified unique ID """ for task_index, task in enumerate(self.task_list): if task.unique_id == unique_id: return task_index return None def display_current_task(self): """ Displays the current task """ self.display_task_by_index(self.current_task_index) def display_all_tasks(self): """ Outputs a table showing all available tasks, sorted by index Args: None. Returns: None. """ index_list = [] for index, task in enumerate(self.task_list): if task.state != 'closed': index_list.append(index) self.display_list_of_tasks_by_index(index_list) def display_list_of_tasks_by_index(self, index_list): """ Displays the tasks with the indices specified by the list Args: index_list (list): a list of task indices to display Returns: None. """ for index, _ in enumerate(self.task_list): if index in index_list: self.display_task_by_index(index) def modify_attribute_current_task(self, attribute, value): """ Sets the attribute on the currently active task to value. Args: attribute (str): a string specifying the attribute to modify value: the new value Returns: None. """ setattr(self.task_list[self.current_task_index], attribute, value) def return_task_with_index(self, index): """ Returns the task with the specified index """ return self.task_list[int(index)] def set_current_task(self, index): """ Sets the current task index """ self.current_task_index = int(index) def filter(self, only_active=True): """ Returns a filtered list of task indices Args: only_active (bool): only return those tasks which aren't blocked Returns: None. """ filtered_index_list = [] for index, task in enumerate(self.task_list): if (task.blocked_until == 'None' and task.state != 'closed'): filtered_index_list.append(index) return filtered_index_list def close(self): """ Closes the task manager by writing the current state to file """ self.filehandler.write_to_file(self.task_list)