本文整理汇总了Python中six.moves.filterfalse函数的典型用法代码示例。如果您正苦于以下问题:Python filterfalse函数的具体用法?Python filterfalse怎么用?Python filterfalse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了filterfalse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _assert_file
def _assert_file(self, obj, fn_tested, fn_correct, compression=None, file_filter=lambda _: False):
"""Compare files."""
open_kwargs = {}
if compression is None:
open_fn = open
# by default, open() will open files as text and return str
# objects, but we need bytes objects
open_kwargs['mode'] = 'rb'
elif compression == 'gzip':
open_fn = gzip.open
elif compression == 'zip':
open_fn = zipfile.ZipFile.open
else:
raise ValueError("Unsupported compression format.")
output = os.path.join(settings.FLOW_EXECUTOR['DATA_DIR'], str(obj.pk), fn_tested)
with open_fn(output, **open_kwargs) as output_file:
output_contents = b"".join([line for line in filterfalse(file_filter, output_file)])
output_hash = hashlib.sha256(output_contents).hexdigest()
correct_path = os.path.join(self.files_path, fn_correct)
if not os.path.isfile(correct_path):
shutil.copyfile(output, correct_path)
self.fail(msg="Output file {} missing so it was created.".format(fn_correct))
with open_fn(correct_path, **open_kwargs) as correct_file:
correct_contents = b"".join([line for line in filterfalse(file_filter, correct_file)])
correct_hash = hashlib.sha256(correct_contents).hexdigest()
self.assertEqual(correct_hash, output_hash,
msg="File contents hash mismatch: {} != {}".format(
correct_hash, output_hash) + self._debug_info(obj))
示例2: __iter__
def __iter__(self):
if self.cache_location is not None:
filenames = iglob(self.filename('*'))
keys = map(lambda f: os.path.splitext(os.path.basename(f))[0], filenames)
new_keys = filterfalse(lambda key: key in self._cache.keys(), keys)
return chain(iterkeys(self._cache), new_keys)
else:
return iterkeys(self._cache)
示例3: _process_pbs_nodefile
def _process_pbs_nodefile(pbs_nodefile, hostname):
with open(pbs_nodefile) as in_file:
nodelist = in_file.read().splitlines()
slave_compute_node_list = [
node for node, _ in groupby(filterfalse(lambda x: x == hostname,
nodelist))
]
return slave_compute_node_list
示例4: unique
def unique(iterable, filterfalse=filterfalse):
"""
Return only unique elements from the sequence.
"""
seen = set()
add = seen.add
for element in filterfalse(seen.__contains__, iterable):
add(element)
yield element
示例5: partition
def partition(pred, iterable):
"""
Partition the iterable into two disjoint entries based
on the predicate.
@return: Tuple (iterable1, iterable2)
"""
iter1, iter2 = itertools.tee(iterable)
return filterfalse(pred, iter1), filter(pred, iter2)
示例6: args_to_string
def args_to_string(*args, **kwargs):
invalid = tuple(filterfalse(repr_is_constructor,
chain(args, kwargs.values())))
if invalid:
raise ValueError
return ', '.join(chain((repr(a) for a in args),
('{0}={1}'.format(str(k), repr(v))
for k, v in kwargs.items())))
示例7: load_rule_set
def load_rule_set(rules_file_name):
with open(os.path.join(os.path.dirname(__file__), data_dir_name, "rules_{}.dat".format(rules_file_name))) as rules_file:
reader = csv.reader(filterfalse(lambda x: re.match(r"^\s*(?:#|$)", x), rules_file), delimiter="\t") # SMARTS and name, tab-seperated
rule_set = [{"n": n, "SMARTS": x[0], "rxn": AllChem.ReactionFromSmarts(x[0]), "name": x[1]} for n, x in enumerate(reader, 1)]
return rule_set
示例8: ls_moduledirs
def ls_moduledirs(path, private=True, full=True):
""" lists all dirs which are python modules in path """
dir_list = ls_dirs(path)
module_dir_iter = filter(is_module_dir, dir_list)
if not private:
module_dir_iter = filterfalse(is_private_module, module_dir_iter)
if not full:
module_dir_iter = map(basename, module_dir_iter)
return list(module_dir_iter)
示例9: to_datetime
def to_datetime(content, dt_format=None, warn=False):
"""Parses and formats strings into datetimes.
Args:
content (str): The string to parse.
dt_format (str): Date format passed to `strftime()`
(default: None).
warn (bool): raise error if content can't be safely converted
(default: False)
Returns:
obj: The datetime object or formatted datetime string.
See also:
`tabutils.process.type_cast`
Examples:
>>> fmt = '%Y-%m-%d %H:%M:%S'
>>> to_datetime('5/4/82 2:00 pm')
datetime.datetime(1982, 5, 4, 14, 0)
>>> to_datetime('5/4/82 10:00', fmt) == '1982-05-04 10:00:00'
True
>>> to_datetime('2/32/82 12:15', fmt) == '1982-02-28 12:15:00'
True
>>> to_datetime('spam')
datetime.datetime(9999, 12, 31, 0, 0)
>>> to_datetime('spam', warn=True)
Traceback (most recent call last):
ValueError: Invalid datetime value: `spam`.
Returns:
datetime
"""
bad_nums = map(str, range(29, 33))
good_nums = map(str, range(31, 27, -1))
try:
bad_num = next(x for x in bad_nums if x in content)
except StopIteration:
options = [content]
else:
possibilities = (content.replace(bad_num, x) for x in good_nums)
options = it.chain([content], possibilities)
# Fix impossible dates, e.g., 2/31/15
results = filterfalse(lambda x: x[1], map(_to_datetime, options))
value = next(results)[0]
if warn and value == DEFAULT_DATETIME:
raise ValueError("Invalid datetime value: `%s`." % content)
else:
datetime = value.strftime(dt_format) if dt_format else value
return datetime
示例10: ls_modulefiles
def ls_modulefiles(path, private=True, full=True, noext=False):
module_file_list = ls(path, '*.py')
module_file_iter = iter(module_file_list)
if not private:
module_file_iter = filterfalse(is_private_module, module_file_iter)
if not full:
module_file_iter = map(basename, module_file_iter)
if noext:
module_file_iter = (splitext(path)[0] for path in module_file_iter)
return list(module_file_iter)
示例11: _candidate_dirs
def _candidate_dirs(base_path):
"""
Return all dirs in base_path that might be packages.
"""
has_dot = lambda name: '.' in name
for root, dirs, files in os.walk(base_path, followlinks=True):
# Exclude directories that contain a period, as they cannot be
# packages. Mutate the list to avoid traversal.
dirs[:] = filterfalse(has_dot, dirs)
for dir in dirs:
yield os.path.relpath(os.path.join(root, dir), base_path)
示例12: equivalentMessage
def equivalentMessage(self, msg1, msg2):
msg1_count = count_messages(msg1)
msg2_count = count_messages(msg2)
if msg1_count != msg2_count:
return False
# Do some basic payload checking
msg1_msgs = [m for m in msg1.walk()]
msg1_msgs = [m for m in filterfalse(ud.is_skippable, msg1_msgs)]
msg2_msgs = [m for m in msg2.walk()]
msg2_msgs = [m for m in filterfalse(ud.is_skippable, msg2_msgs)]
for i in range(0, len(msg2_msgs)):
m1_msg = msg1_msgs[i]
m2_msg = msg2_msgs[i]
if m1_msg.get_charset() != m2_msg.get_charset():
return False
if m1_msg.is_multipart() != m2_msg.is_multipart():
return False
m1_py = m1_msg.get_payload(decode=True)
m2_py = m2_msg.get_payload(decode=True)
if m1_py != m2_py:
return False
return True
示例13: unique_everseen
def unique_everseen(iterable, key=None):
"List unique elements, preserving order. Remember all elements ever seen."
# http://docs.python.org/2/library/itertools.html
seen = set()
seen_add = seen.add
if key is None:
for element in filterfalse(seen.__contains__, iterable):
seen_add(element)
yield element
else:
for element in iterable:
k = key(element)
if k not in seen:
seen_add(k)
yield element
示例14: partition
def partition(pred, iterable):
"""
Returns a 2-tuple of iterables derived from the input iterable.
The first yields the items that have ``pred(item) == False``.
The second yields the items that have ``pred(item) == True``.
>>> is_odd = lambda x: x % 2 != 0
>>> iterable = range(10)
>>> even_items, odd_items = partition(is_odd, iterable)
>>> list(even_items), list(odd_items)
([0, 2, 4, 6, 8], [1, 3, 5, 7, 9])
"""
# partition(is_odd, range(10)) --> 0 2 4 6 8 and 1 3 5 7 9
t1, t2 = tee(iterable)
return filterfalse(pred, t1), filter(pred, t2)
示例15: wrapper
def wrapper(*args, **kwds):
# cache key records both positional and keyword args
key = args
if kwds:
key += (kwd_mark,) + tuple(sorted(kwds.items()))
# record recent use of this key
queue_append(key)
refcount[key] += 1
# get cache entry or compute if not found
try:
result, expire_time = cache[key]
if expire_time and time() > expire_time:
raise KeyError('Expired')
wrapper.hits += 1
except KeyError:
result = user_function(*args, **kwds)
if maxtime:
expire_time = time() + maxtime
else:
expire_time = None
cache[key] = result, expire_time
wrapper.misses += 1
# purge least recently used cache entry
if len(cache) > maxsize:
key = queue_popleft()
refcount[key] -= 1
while refcount[key]:
key = queue_popleft()
refcount[key] -= 1
del cache[key], refcount[key]
# periodically compact the queue by eliminating duplicate keys
# while preserving order of most recent access
if len(queue) > maxqueue:
refcount.clear()
queue_appendleft(sentinel)
for key in filterfalse(refcount.__contains__, iter(queue_pop, sentinel)):
queue_appendleft(key)
refcount[key] = 1
return result