def remove_value(dic, values): if isiterable(values): return {_key: dic[_key] for _key in dic if dic[_key] not in values} else: return {_key: dic[_key] for _key in dic if dic[_key] != values}
def find(pattern, startdir=os.curdir, regex_match=False): def ismatch(filename, pattern, regex_match): if regex_match and re.fullmatch(string=filename, pattern=pattern) is not None: return True elif fnmatch.fnmatch(name, pattern): return True else: return False for (thisDir, subsHere, filesHere) in os.walk(startdir): for name in subsHere + filesHere: match_success = False if isiterable(pattern): for each_pattern in pattern: if ismatch(name, each_pattern, regex_match): match_success = True break else: if ismatch(name, pattern, regex_match): match_success = True if match_success: fullpath = os.path.join(thisDir, name) yield fullpath
def remove_key(dic, keys): if isiterable(keys): return {_key: dic[_key] for _key in dic if _key not in keys} else: return {_key: dic[_key] for _key in dic if _key != keys}
def test_isiterable(): assert not var.isiterable('abc') assert not var.isiterable(b'abc') assert not var.isiterable(bytearray(b'abc')) assert var.isiterable('abc', but_str_bytes=False) assert var.isiterable(['a', 'b', 'c']) assert var.isiterable(('a', 'b', 'c'))
def set_section(self, section_name, iterable_obj): """section_name shouldn't start with `_` """ if iterable_obj is None: pass elif not isiterable(iterable_obj): try: list(iterable_obj) except TypeError as ex: err_msg = ex.__str__() raise TypeError(err_msg) if section_name.startswith('_'): raise ReservedSectionNameError("`%s` shouldn't start with `_` " % section_name) self._section_dict[section_name] = iterable_obj
def find_wrapper(start_dir, pattern): if not isiterable(pattern): pattern = [pattern] command_runner = CommandRunner() if iswin(): cmd = 'where /R "{start_dir}" {pattern}'.format(start_dir=start_dir, pattern=' '.join(pattern)) else: cmd = 'find {start_dir} {pattern}'.format( start_dir=start_dir, pattern=' '.join(['-name "%s"' % each_pattern for each_pattern in pattern]) ) for line in command_runner.run(cmd): if os.path.exists(line): yield line
def find_wrapper(start_dir, pattern): if not isiterable(pattern): pattern = [pattern] command_runner = CommandRunner() if iswin(): cmd = 'where /R "{start_dir}" {pattern}'.format( start_dir=start_dir, pattern=' '.join(pattern)) else: cmd = 'find {start_dir} {pattern}'.format( start_dir=start_dir, pattern=' '.join( ['-name "%s"' % each_pattern for each_pattern in pattern])) for line in command_runner.run(cmd): if os.path.exists(line): yield line