def test_exclude(self): """Test exclude parameter.""" self.assertTrue(fnmatch.fnmatch('name', '*', exclude='test')) self.assertTrue(fnmatch.fnmatch(b'name', b'*', exclude=b'test')) self.assertFalse(fnmatch.fnmatch('test', '*', exclude='test')) self.assertFalse(fnmatch.fnmatch(b'test', b'*', exclude=b'test'))
def test_default_compile(self): """Test default with exclusion.""" self.assertTrue(fnmatch.fnmatch('name', '!test', flags=fnmatch.N | fnmatch.A)) self.assertTrue(fnmatch.fnmatch(b'name', b'!test', flags=fnmatch.N | fnmatch.A)) self.assertFalse(fnmatch.fnmatch('test', '!test', flags=fnmatch.N | fnmatch.A)) self.assertFalse(fnmatch.fnmatch(b'test', b'!test', flags=fnmatch.N | fnmatch.A))
def generate_file_path(dirpath, filename, ext=['.cap', '.txt', '.log'], exclude=['_V', '_Data_Workshop']): """ Function which searches for the location of the given file and returns the full path to the file. Args: dirpath - parent directory path under which to search filename - the name of the file to search for ext - file endings to search for exclude - optional list which allows for excluding certain directories from the search Returns: fpath - the file path to the filename from the current working directory. """ # Check if the input file name has an extension already # If it does, parse it for input into the search algo if '.' in filename: check = filename.split('.') filename = check[0] ext = ['.'+check[1]] for root, dirs, files in os.walk(dirpath): dirs[:] = [d for d in dirs if d not in exclude] for fname in files: if fnmatch.fnmatch(fname, [filename+'*'+x for x in ext]): fpath = os.path.join(root, fname) return fpath
def _walk_match(obj, paths, values, default): for path in paths: value = get(obj, path) if value is not None: return fnmatch(value, values) return default
def select_video(self): """ Extract a list of videos to process by comparing the repository of videos and the repository of the reports in order to take only the videos that are not labelled yet """ if isinstance(self.folder_labels, str) and isinstance( self.folder_videos, str): # List of reports labels = list() for path, subdir, files in os.walk(self.folder_labels): for name in files: if fnmatch(name, '*.csv'): labels.append(name) # List of videos videos = list() for path, subdir, files in os.walk(self.folder_videos): path = Path(path) for name in files: if fnmatch(name, ['*.' + v for v in FORMAT_VIDEOS]): path_vid = path / name if LabelTool.create_label_report_name( path_vid) not in labels: videos.append(path_vid) # List Box selection ch = Tk() scrollbar = Scrollbar(ch) scrollbar.pack(side=RIGHT, fill=Y) listbox = Listbox(ch, width=100, height=50, yscrollcommand=scrollbar.set) for v in videos: listbox.insert(END, v) listbox.pack(side=LEFT, fill=BOTH) button = Button(ch, text='print', command=lambda: ch.quit()) button.pack() ch.mainloop() self.filename = Path(listbox.get(ACTIVE)) ch.destroy()
def check_escape(self, arg, expected, unix=None, raw_chars=True): """Verify escapes.""" flags = 0 if unix is False: flags = fnmatch.FORCEWIN elif unix is True: flags = fnmatch.FORCEUNIX self.assertEqual(fnmatch.escape(arg), expected) self.assertEqual(fnmatch.escape(os.fsencode(arg)), os.fsencode(expected)) self.assertTrue(fnmatch.fnmatch(arg, fnmatch.escape(arg), flags=flags))
def test_exclude_mix(self): """ Test exclusion flags mixed with exclusion parameter. If both are given, flags are ignored. """ self.assertTrue(fnmatch.fnmatch('name', '*', exclude='test', flags=fnmatch.N | fnmatch.A)) self.assertTrue(fnmatch.fnmatch(b'name', b'*', exclude=b'test', flags=fnmatch.N | fnmatch.A)) self.assertFalse(fnmatch.fnmatch('test', '*', exclude='test', flags=fnmatch.N | fnmatch.A)) self.assertFalse(fnmatch.fnmatch(b'test', b'*', exclude=b'test', flags=fnmatch.N | fnmatch.A)) self.assertTrue(fnmatch.fnmatch('name', ['*', '!name'], exclude='test', flags=fnmatch.N | fnmatch.A)) self.assertFalse(fnmatch.fnmatch('test', ['*', '!name'], exclude='test', flags=fnmatch.N | fnmatch.A)) self.assertTrue(fnmatch.fnmatch('!name', ['*', '!name'], exclude='test', flags=fnmatch.N | fnmatch.A))
def load_asset_management(instrument, filepath): """ Loads the calibration csv files from a local repository containing the asset management information. Args: instrument - a pandas dataframe with the asset tracking information for a specific instrument. filepath - the directory path pointing to where the csv files are stored. Raises: TypeError - if the instrument input is not a pandas dataframe Returns: csv_dict - a dictionary with keys of the UIDs from the instrument dataframe which correspond to lists of the relevant calibration csv files """ # Check that the input is a pandas DataFrame if type(instrument) != pd.core.frame.DataFrame: raise TypeError() uids = sorted(list(set(instrument['UID']))) csv_dict = {} for uid in uids: # Get a specified uid from the instrument dataframe instrument['UID_match'] = instrument['UID'].apply( lambda x: True if uid in x else False) instrument[instrument['UID_match'] == True] # Now, get all the csvs from asset management for a particular UID csv_files = [] for file in os.listdir(filepath): if fnmatch.fnmatch(file, '*' + uid + '*'): csv_files.append(file) else: pass # Update the dictionary storing the asset management files for each UID if len(csv_files) > 0: csv_dict.update({uid: csv_files}) else: pass return csv_dict
def _generate_file_tree(self) -> dict: """ Load all file tree from Backblaze B2. This operation can be expensive if they are many elements in the bucket. """ files = {} for entry in self._ls(): info: FileVersionInfo = entry[0] # Check if the file is a directory and create it if fnmatch(info.file_name, "*/.bzEmpty"): name = info.file_name[:-8] # Remove ".bzEmpty" files[name] = Folder(info.id_, name, info.upload_timestamp) # Otherwise, it's a file. Create it too. else: files[info.file_name] = File(info.id_, info.file_name, info.upload_timestamp, info.size, info.content_type) return files
def test_match_set(self): """Test `set` matching.""" self.assertTrue(fnmatch.fnmatch('a', set(['a'])))
def test_limit_fnmatch(self): """Test expansion limit of `fnmatch`.""" with self.assertRaises(_wcparse.PatternLimitException): fnmatch.fnmatch('name', '{1..11}', flags=fnmatch.BRACE, limit=10)
def select_files(files, pat): flags = wcfnmatch.NEGATE | wcfnmatch.NEGATEALL selected = [f for f in files if wcfnmatch.fnmatch(f, pat, flags=flags)] return selected
def has_perm(self, perm): return fnmatch.fnmatch(perm, self.perms)
df.drop(df.loc[df['status']=='live'].index, inplace=True) df.drop(df.loc[df['status']=='canceled'].index, inplace=True) df.drop(df.loc[df['status']=='suspended'].index, inplace=True) ## Spliting the column into two: new = df["location"].str.split(", ", n = 1, expand = True) df["city"]= new[0] df["state"]= new[1] df.drop(columns =["location"], inplace = True) print(df.shape) ##Filtering states for each in df.state: if (fnmatch.fnmatch(str(each),'??')) == False: df.drop(df.loc[df['state']== each].index, inplace=True) print(df.shape) ## Checking for missing values: print(df.isnull().sum()) df = df.dropna() print(df.isnull().sum()) print(df.shape) df.to_csv('Final_123.csv') ## Finding Main Category, Sub Category and number of states: print(len(df.category.unique()), "Main categories\n")
def test_match_tuple(self): """Test `tuple` matching.""" self.assertTrue(fnmatch.fnmatch('a', tuple(['a'])))
def test_match_list(self): """Test `list` matching.""" self.assertTrue(fnmatch.fnmatch('a', ['a']))
def match(self, category, pattern): """Match the category.""" return fnmatch.fnmatch(category, fnmatch.fnsplit(pattern, flags=self.FNMATCH_FLAGS), flags=self.FNMATCH_FLAGS)