def test_file_does_not_have_windows_attributes(self): actual = list(readdir(test_tree["root dir"])) normal_file = [f for f in actual if f.name == "normal-file"] # There should be exactly one of these self.assertEqual(len(normal_file), 1) normal_file = normal_file[0] # None of these should exist; they are Windows-only with self.assertRaises(AttributeError): normal_file.attributes with self.assertRaises(AttributeError): normal_file.creation_time with self.assertRaises(AttributeError): normal_file.modification_time with self.assertRaises(AttributeError): normal_file.access_time with self.assertRaises(AttributeError): normal_file.size with self.assertRaises(AttributeError): normal_file.short_name
def _ProcessNode(self): """Process a node in the directory tree. If the node is another directory, enumerate its contents and add it to the list of nodes to be processed in the future.""" filename, filetype = self.items.pop() try: # If the filesystem supports readdir d_type, then we will know if the node is # a file or a directory without doing any extra work. If it does not, we have # to do a stat. if filetype == 0: s = os.lstat(filename) if stat.S_ISDIR(s.st_mode): filetype = 4 else: filetype = 8 # If we a directory, enumerate its contents and add them to the list of nodes # to be processed. if filetype == 4: for node in readdir.readdir(filename): if not node.d_name in (".", ".."): fullname = os.path.join(filename, node.d_name) self.items.appendleft((fullname, node.d_type)) # Call the processing functions on the directory or file. self.ProcessDir(filename) else: self.ProcessFile(filename) except OSError as error: print "cannot access `%s':" % filename, print os.strerror(error.errno) return ()
def test_readdir_with_normal_1_or_3_file_glob(self): actual = list(readdir(test_tree["root dir"], glob="normal-[13]-file")) actual.sort() expected = list(entry for entry in test_tree["contents: normal-?-file"] if entry.name != "normal-2-file") expected.sort() self.assertEqual(actual, expected)
def _ProcessNode(self): """Process a node in the directory tree. If the node is another directory, enumerate its contents and add it to the list of nodes to be processed in the future.""" filename, filetype = self.items.pop() try: # If the filesystem supports readdir d_type, then we will know if the node is # a file or a directory without doing any extra work. If it does not, we have # to do a stat. if filetype == 0: s = safestat.safestat(filename) if stat.S_ISDIR(s.st_mode): filetype = readdir.dirent.DT_DIR else: filetype = readdir.dirent.DT_REG # If we a directory, enumerate its contents and add them to the list of nodes # to be processed. if filetype == readdir.dirent.DT_DIR: for node in readdir.readdir(filename): if not node.d_name in (".",".."): fullname = os.path.join(filename, node.d_name) self.items.appendleft((fullname, node.d_type)) # Call the processing functions on the directory or file. self.ProcessDir(filename) else: self.ProcessFile(filename) except OSError as error: print "cannot access `%s':" % filename, print os.strerror(error.errno) return()
def test_recursive_readdir(self): actual = list(readdir(test_tree["root dir"], recursive=True)) actual.sort() expected = (list(test_tree["contents"]) + list(test_tree["contents: extra recursive"])) expected.sort() self.assertEqual(actual, expected)
def test_file_has_specified_attributes(self): actual = list(readdir(test_tree["root dir"])) normal_file = [f for f in actual if f.name == "normal-file"] # There should be exactly one of these self.assertEqual(len(normal_file), 1) normal_file = normal_file[0] # Make sure all fields exist normal_file.name normal_file.kind normal_file.inode
def output_for_path(path): # If it's a file, we just list it. If it's a directory, we have to # print out the contents. if os.path.isdir(path): dircontents = readdir.readdir(path) for entry in dircontents: print entry.to_json() elif os.path.lexists(path): print path else: msg = "%s: cannot access %s: No such file or directory" sys.stderr.write(msg % (sys.argv[0], path))
def fastwalk(sourcedir, onerror=None, topdown=True): """Improved version of os.walk: generates a tuple of (sourcedir,[dirs], [files]). This version tries to use readdir to avoid expensive stat operations on lustre.""" dirlist = [] filelist = [] try: entries = readdir.readdir(sourcedir) except Exception as err: if onerror is not None: onerror(err) return for entry in entries: name = entry.d_name filetype = entry.d_type if not name in (".", ".."): if filetype == readdir.dirent.DT_UNKNOWN: fullname = os.path.join(sourcedir, name) mode = safestat.safestat(fullname).st_mode if stat.S_ISDIR(mode): filetype = readdir.dirent.DT_DIR else: filetype = readdir.dirent.DT_REG if filetype == readdir.dirent.DT_DIR: dirlist.append(name) else: filelist.append(name) if topdown: yield sourcedir, dirlist, filelist for d in dirlist: fullname = os.path.join(sourcedir, d) for entries in fastwalk(fullname, onerror, topdown): yield entries if not topdown: yield sourcedir, dirlist, filelist
def test_basic_readdir(self): actual = list(readdir(test_tree["root dir"])) actual.sort() expected = list(test_tree["contents"]) expected.sort() self.assertEqual(actual, expected)
if sheets.cell_value(rowx=0, colx=i).replace(' ', '') == str: return i i += 1 def getcol(sheets, ncol, cols): i = 1 j = 0 while i < sheets.nrows: cols.append(sheets.cell_value(rowx=i, colx=ncol)) # print("j="+str(j)+",value="+str(cols[j])) i += 1 j += 1 #__booklist__ booklist = [] path = "D:/Tony/python" readdir.readdir(path, booklist) for file_name in booklist: fullpath = path + "/" + file_name __mybook__ = xlrd.open_workbook(fullpath) __mysheet__ = __mybook__.sheet_by_index(0) __cols1__ = [] __cols2__ = [] readsheet(__mysheet__, __cols1__, __cols2__) wirtetocsv(__cols1__, __cols2__, "选修17-18下", file_name, path) print(booklist)
def test_readdir_with_normal_star_glob(self): actual = list(readdir(test_tree["root dir"], glob="normal*")) actual.sort() expected = list(test_tree["contents: normal*"]) expected.sort() self.assertEqual(actual, expected)
def main(argv): if len(argv) < 2: print('Usage: python3 %s <directory name>' % (argv[0])) exit(0) filels = readdir.readdir(argv[1]) if len(filels) == 0: print("No file in directory") exit(0) exps = [] periods = [] rmses = [] rsquares = [] rsquare_damps = [] peakDiffSquares = [] outputs = [] dates = [] params_full = [] for file in filels: expList = data.read(file) for i in range(len(expList)): x = np.array(expList[i].processData()[:, 0]) y_data = np.array(expList[i].processData()[:, 1]) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(x, y_data, 'b-') params = analysis.fit_hirota(x, y_data) params_full.append(params) if params is None: print("Curve_fit failed for experiemnt " + str(i)) rmses.append(-1) rsquares.append(-1) rsquare_damps.append(-1) peakDiffSquares.append(-1) exp = file.split('/')[-1][:-4] + "_" + str(i) exps.append(exp) dates.append(expList[i].date) periods.append(-1) outputs.append(False) fig.savefig(exp, dpi=600) continue y_exp = analysis.obj_func_Hirota(x, params[0], params[1], params[2], params[3], params[4], params[5]) ax.plot(x, y_exp, 'r-') # # green residual # ax.plot(x, y_data-y_exp, 'g-') ax.text(0.5, 0.95, "Period = %.2f" % params[4], horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, fontsize=8) rmse = accuracy.rmse(y_data, y_exp) rsquare = accuracy.rsquare(y_data, y_exp) rsquare_damp = accuracy.rsquare_damp(y_data, y_exp, x, params[2]) try: peakDiffSquare = accuracy.peakDiffSquare( x, y_data, y_exp, params[3], params[4]) ax.text( 0.5, 0.85, "RMSE = %.4f R^2 = %.4f R^2d = %.4f\n PeakSquareDiff = %.4f" % (rmse, rsquare, rsquare_damp, peakDiffSquare), horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, fontsize=6) except: print( "Unable to compute peakDiffSquare for experiment index " + str(i)) ax.text(0.5, 0.85, "RMSE = %.4f R^2 = %.4f R^2d = %.4f" % (rmse, rsquare, rsquare_damp), horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, fontsize=6) peakDiffSquare = -1 output = accuracy.criteria(params[4], rsquare, rsquare_damp) outputs.append(output) ax.text(0.8, 0.78, output, horizontalalignment='center', verticalalignment='center', transform=ax.transAxes, fontsize=12) rmses.append(rmse) rsquares.append(rsquare) rsquare_damps.append(rsquare_damp) peakDiffSquares.append(peakDiffSquare) exp = file.split('/')[-1][:-4] + "_" + str(i) exps.append(exp) dates.append(expList[i].date) periods.append(params[4]) fig.savefig(exp, dpi=600) plt.close(fig) # plt.show() results = np.matrix([rmses, rsquares, rsquare_damps, peakDiffSquares]).T fp = open('results.csv', 'w') fp.write('exp, period, rmse, rsquare, rsqare_damp, peakDiffSquare\n') for i in range(results.shape[0]): fp.write( str(exps[i]) + ',' + str(periods[i]) + ',' + str(results[i, 0]) + ',' + str(results[i, 1]) + ',' + str(results[i, 2]) + ',' + str(results[i, 3]) + '\n') fp.close() fp = open('results_good.csv', 'w') fp.write('exp, date, baseline, amplitude, damp, phase, period, trend\n') for i in range(results.shape[0]): if outputs[i] is True: fp.write( str(exps[i]) + ',' + str(dates[i]) + ',' + str(params_full[i][0]) + ',' + str(params_full[i][1]) + ',' + str(params_full[i][2]) + ',' + str(params_full[i][3]) + ',' + str(params_full[i][4]) + ',' + str(params_full[i][5]) + '\n') fp.close()