本文整理汇总了Python中twitter.common.collections.OrderedSet.discard方法的典型用法代码示例。如果您正苦于以下问题:Python OrderedSet.discard方法的具体用法?Python OrderedSet.discard怎么用?Python OrderedSet.discard使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twitter.common.collections.OrderedSet
的用法示例。
在下文中一共展示了OrderedSet.discard方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: InternalTarget
# 需要导入模块: from twitter.common.collections import OrderedSet [as 别名]
# 或者: from twitter.common.collections.OrderedSet import discard [as 别名]
#.........这里部分代码省略.........
if not scanned_back: # done with coalescing the current type, move on to next
current_type = discriminator(current_target)
return sorted_targets
def sort(self):
"""Returns a list of targets this target depends on sorted from most dependent to least."""
return InternalTarget.sort_targets([self])
def coalesce(self, discriminator):
"""Returns a list of targets this target depends on sorted from most dependent to least and
grouped where possible by target type as categorized by the given discriminator.
"""
return InternalTarget.coalesce_targets([self], discriminator)
def __init__(self, name, dependencies, exclusives=None):
Target.__init__(self, name, exclusives=exclusives)
self._injected_deps = []
self.processed_dependencies = resolve(dependencies)
self.add_labels('internal')
self.dependency_addresses = OrderedSet()
self.dependencies = OrderedSet()
self.internal_dependencies = OrderedSet()
self.jar_dependencies = OrderedSet()
# TODO(John Sirois): just use the more general check: if parsing: delay(doit) else: doit()
# Fix how target _ids are built / addresses to not require a BUILD file - ie: support anonymous,
# non-addressable targets - which is what meta-targets really are once created.
# Defer dependency resolution after parsing the current BUILD file to allow for forward
# references
self._post_construct(self.update_dependencies, self.processed_dependencies)
self._post_construct(self.inject_dependencies)
def _propagate_exclusives(self):
# Note: this overrides Target._propagate_exclusives without
# calling the supermethod. Targets in pants do not necessarily
# have a dependencies field, or ever have their dependencies
# available at all pre-resolve. Subtypes of InternalTarget, however,
# do have well-defined dependency lists in their dependencies field,
# so we can do a better job propagating their exclusives quickly.
if self.exclusives is not None:
return
self.exclusives = copy.deepcopy(self.declared_exclusives)
for t in self.dependencies:
if isinstance(t, Target):
t._propagate_exclusives()
self.add_to_exclusives(t.exclusives)
elif hasattr(t, "declared_exclusives"):
self.add_to_exclusives(t.declared_exclusives)
def add_injected_dependency(self, spec):
self._injected_deps.append(spec)
def inject_dependencies(self):
self.update_dependencies(resolve(self._injected_deps))
def update_dependencies(self, dependencies):
if dependencies:
for dependency in dependencies:
if hasattr(dependency, 'address'):
self.dependency_addresses.add(dependency.address)
for resolved_dependency in dependency.resolve():
if is_concrete(resolved_dependency) and not self.valid_dependency(resolved_dependency):
raise TargetDefinitionException(self, 'Cannot add %s as a dependency of %s'
% (resolved_dependency, self))
self.dependencies.add(resolved_dependency)
if isinstance(resolved_dependency, InternalTarget):
self.internal_dependencies.add(resolved_dependency)
if hasattr(resolved_dependency, '_as_jar_dependencies'):
self.jar_dependencies.update(resolved_dependency._as_jar_dependencies())
def valid_dependency(self, dep):
"""Subclasses can over-ride to reject invalid dependencies."""
return True
def replace_dependency(self, dependency, replacement):
self.dependencies.discard(dependency)
self.internal_dependencies.discard(dependency)
self.jar_dependencies.discard(dependency)
self.update_dependencies([replacement])
def _walk(self, walked, work, predicate = None):
Target._walk(self, walked, work, predicate)
for dep in self.dependencies:
if isinstance(dep, Target) and not dep in walked:
walked.add(dep)
if not predicate or predicate(dep):
additional_targets = work(dep)
dep._walk(walked, work, predicate)
if additional_targets:
for additional_target in additional_targets:
additional_target._walk(walked, work, predicate)
示例2: Context
# 需要导入模块: from twitter.common.collections import OrderedSet [as 别名]
# 或者: from twitter.common.collections.OrderedSet import discard [as 别名]
#.........这里部分代码省略.........
"""
return self._target_roots
def __str__(self):
return 'Context(id:%s, state:%s, targets:%s)' % (self.id, self.state, self.targets())
def acquire_lock(self):
""" Acquire the global lock for the root directory associated with this context. When
a goal requires serialization, it will call this to acquire the lock.
"""
def onwait(pid):
print('Waiting on pants process %s to complete' % _process_info(pid), file=sys.stderr)
return True
if self._lock.is_unlocked():
runfile = os.path.join(self._buildroot, '.pants.run')
self._lock = Lock.acquire(runfile, onwait=onwait)
def release_lock(self):
"""Release the global lock if it's held.
Returns True if the lock was held before this call.
"""
if self._lock is Lock.unlocked():
return False
else:
self._lock.release()
self._lock = Lock.unlocked()
return True
def replace_targets(self, target_roots):
"""Replaces all targets in the context with the given roots and their transitive
dependencies.
"""
self._target_roots = target_roots
self._targets = OrderedSet()
for target in target_roots:
self.add_target(target)
self.id = Target.identify(self._targets)
def add_target(self, target):
"""Adds a target and its transitive dependencies to the run context.
The target is not added to the target roots.
"""
def add_targets(tgt):
self._targets.update(tgt.resolve())
target.walk(add_targets)
def add_new_target(self, target_base, target_type, *args, **kwargs):
"""Creates a new target, adds it to the context and returns it.
This method ensures the target resolves files against the given target_base, creating the
directory if needed and registering a source root.
"""
target = self._create_new_target(target_base, target_type, *args, **kwargs)
self.add_target(target)
return target
def _create_new_target(self, target_base, target_type, *args, **kwargs):
if not os.path.exists(target_base):
os.makedirs(target_base)
SourceRoot.register(target_base, target_type)
with ParseContext.temp(target_base):
return target_type(*args, **kwargs)
def remove_target(self, target):
"""Removes the given Target object from the context completely if present."""
if target in self.target_roots:
self.target_roots.remove(target)
self._targets.discard(target)
def targets(self, predicate=None):
"""Selects targets in-play in this run from the target roots and their transitive dependencies.
If specified, the predicate will be used to narrow the scope of targets returned.
"""
return filter(predicate, self._targets)
def dependants(self, on_predicate=None, from_predicate=None):
"""Returns a map from targets that satisfy the from_predicate to targets they depend on that
satisfy the on_predicate.
"""
core = set(self.targets(on_predicate))
dependees = defaultdict(set)
for target in self.targets(from_predicate):
if hasattr(target, 'dependencies'):
for dependency in target.dependencies:
if dependency in core:
dependees[target].add(dependency)
return dependees
def resolve(self, spec):
"""Returns an iterator over the target(s) the given address points to."""
with ParseContext.temp():
return Pants(spec).resolve()
@contextmanager
def state(self, key, default=None):
value = self._state.get(key, default)
yield value
self._state[key] = value
示例3: Context
# 需要导入模块: from twitter.common.collections import OrderedSet [as 别名]
# 或者: from twitter.common.collections.OrderedSet import discard [as 别名]
#.........这里部分代码省略.........
"""
if self._lock.is_unlocked():
return False
else:
self._lock.release()
self._lock = Lock.unlocked()
return True
def is_unlocked(self):
"""Whether the global lock object is actively holding the lock."""
return self._lock.is_unlocked()
def replace_targets(self, target_roots):
"""Replaces all targets in the context with the given roots and their transitive
dependencies.
"""
self._target_roots = list(target_roots)
self._targets = OrderedSet()
for target in self._target_roots:
self.add_target(target)
self.id = Target.identify(self._targets)
def add_target(self, target):
"""Adds a target and its transitive dependencies to the run context.
The target is not added to the target roots.
"""
def add_targets(tgt):
self._targets.update(tgt for tgt in tgt.resolve() if isinstance(tgt, self._target_base))
target.walk(add_targets)
def add_new_target(self, target_base, target_type, *args, **kwargs):
"""Creates a new target, adds it to the context and returns it.
This method ensures the target resolves files against the given target_base, creating the
directory if needed and registering a source root.
"""
if 'derived_from' in kwargs:
derived_from = kwargs.get('derived_from')
del kwargs['derived_from']
else:
derived_from = None
target = self._create_new_target(target_base, target_type, *args, **kwargs)
self.add_target(target)
if derived_from:
target.derived_from = derived_from
return target
def _create_new_target(self, target_base, target_type, *args, **kwargs):
if not os.path.exists(target_base):
os.makedirs(target_base)
SourceRoot.register(target_base, target_type)
with ParseContext.temp(target_base):
return target_type(*args, **kwargs)
def remove_target(self, target):
"""Removes the given Target object from the context completely if present."""
if target in self.target_roots:
self.target_roots.remove(target)
self._targets.discard(target)
def targets(self, predicate=None):
"""Selects targets in-play in this run from the target roots and their transitive dependencies.
If specified, the predicate will be used to narrow the scope of targets returned.
"""
return filter(predicate, self._targets)
def dependents(self, on_predicate=None, from_predicate=None):
"""Returns a map from targets that satisfy the from_predicate to targets they depend on that
satisfy the on_predicate.
"""
core = set(self.targets(on_predicate))
dependees = defaultdict(set)
for target in self.targets(from_predicate):
if hasattr(target, 'dependencies'):
for dependency in target.dependencies:
if dependency in core:
dependees[target].add(dependency)
return dependees
def resolve(self, spec):
"""Returns an iterator over the target(s) the given address points to."""
with ParseContext.temp():
return Pants(spec).resolve()
@contextmanager
def state(self, key, default=None):
value = self._state.get(key, default)
yield value
self._state[key] = value
@contextmanager
def timing(self, label):
if self.timer:
with self.timer.timing(label):
yield
else:
yield
示例4: InternalTarget
# 需要导入模块: from twitter.common.collections import OrderedSet [as 别名]
# 或者: from twitter.common.collections.OrderedSet import discard [as 别名]
#.........这里部分代码省略.........
# [a,b]
if len(sorted_targets) <= 2:
return sorted_targets
# For these, we'd like to coalesce if possible, like:
# [a,b,a,c,a,c] -> [a,a,a,b,c,c]
# adopt a quadratic worst case solution, when we find a type change edge, scan forward for
# the opposite edge and then try to swap dependency pairs to move the type back left to its
# grouping. If the leftwards migration fails due to a dependency constraint, we just stop
# and move on leaving "type islands".
current_type = None
# main scan left to right no backtracking
for i in range(len(sorted_targets) - 1):
current_target = sorted_targets[i]
if current_type != discriminator(current_target):
scanned_back = False
# scan ahead for next type match
for j in range(i + 1, len(sorted_targets)):
look_ahead_target = sorted_targets[j]
if current_type == discriminator(look_ahead_target):
scanned_back = True
# swap this guy as far back as we can
for k in range(j, i, -1):
previous_target = sorted_targets[k - 1]
mismatching_types = current_type != discriminator(previous_target)
not_a_dependency = look_ahead_target not in previous_target.internal_dependencies
if mismatching_types and not_a_dependency:
sorted_targets[k] = sorted_targets[k - 1]
sorted_targets[k - 1] = look_ahead_target
else:
break # out of k
break # out of j
if not scanned_back: # done with coalescing the current type, move on to next
current_type = discriminator(current_target)
return sorted_targets
def sort(self):
"""Returns a list of targets this target depends on sorted from most dependent to least."""
return InternalTarget.sort_targets([ self ])
def coalesce(self, discriminator):
"""Returns a list of targets this target depends on sorted from most dependent to least and
grouped where possible by target type as categorized by the given discriminator."""
return InternalTarget.coalesce_targets([ self ], discriminator)
def __init__(self, name, dependencies, is_meta):
Target.__init__(self, name, is_meta)
self.add_label('internal')
self.dependencies = OrderedSet()
self.internal_dependencies = OrderedSet()
self.jar_dependencies = OrderedSet()
# TODO(John Sirois): if meta targets were truly built outside parse contexts - we could instead
# just use the more general check: if parsing: delay(doit) else: doit()
# Fix how target _ids are built / addresses to not require a BUILD file - ie: support anonymous,
# non-addressable targets - which is what meta-targets really are once created.
if is_meta:
# Meta targets are built outside any parse context - so update dependencies immediately
self.update_dependencies(dependencies)
else:
# Defer dependency resolution after parsing the current BUILD file to allow for forward
# references
self._post_construct(self.update_dependencies, dependencies)
def update_dependencies(self, dependencies):
if dependencies:
for dependency in dependencies:
for resolved_dependency in dependency.resolve():
self.dependencies.add(resolved_dependency)
if isinstance(resolved_dependency, InternalTarget):
self.internal_dependencies.add(resolved_dependency)
if hasattr(resolved_dependency, '_as_jar_dependencies'):
self.jar_dependencies.update(resolved_dependency._as_jar_dependencies())
def replace_dependency(self, dependency, replacement):
self.dependencies.discard(dependency)
self.internal_dependencies.discard(dependency)
self.jar_dependencies.discard(dependency)
self.update_dependencies([replacement])
def _walk(self, walked, work, predicate = None):
Target._walk(self, walked, work, predicate)
for dep in self.dependencies:
if isinstance(dep, Target) and not dep in walked:
walked.add(dep)
if not predicate or predicate(dep):
additional_targets = work(dep)
dep._walk(walked, work, predicate)
if additional_targets:
for additional_target in additional_targets:
additional_target._walk(walked, work, predicate)
示例5: Context
# 需要导入模块: from twitter.common.collections import OrderedSet [as 别名]
# 或者: from twitter.common.collections.OrderedSet import discard [as 别名]
#.........这里部分代码省略.........
"""
if self._lock.is_unlocked():
return False
else:
self._lock.release()
self._lock = Lock.unlocked()
return True
def is_unlocked(self):
"""Whether the global lock object is actively holding the lock."""
return self._lock.is_unlocked()
def replace_targets(self, target_roots):
"""Replaces all targets in the context with the given roots and their transitive
dependencies.
"""
self._target_roots = list(target_roots)
self._targets = OrderedSet()
for target in self._target_roots:
self.add_target(target)
self.id = Target.identify(self._targets)
def add_target(self, target):
"""Adds a target and its transitive dependencies to the run context.
The target is not added to the target roots.
"""
def add_targets(tgt):
self._targets.update(tgt for tgt in tgt.resolve() if isinstance(tgt, self._target_base))
target.walk(add_targets)
def add_new_target(self, target_base, target_type, *args, **kwargs):
"""Creates a new target, adds it to the context and returns it.
This method ensures the target resolves files against the given target_base, creating the
directory if needed and registering a source root.
"""
if 'derived_from' in kwargs:
derived_from = kwargs.get('derived_from')
del kwargs['derived_from']
else:
derived_from = None
target = self._create_new_target(target_base, target_type, *args, **kwargs)
self.add_target(target)
if derived_from:
target.derived_from = derived_from
return target
def _create_new_target(self, target_base, target_type, *args, **kwargs):
if not os.path.exists(target_base):
os.makedirs(target_base)
SourceRoot.register(target_base, target_type)
with ParseContext.temp(target_base):
return target_type(*args, **kwargs)
def remove_target(self, target):
"""Removes the given Target object from the context completely if present."""
if target in self.target_roots:
self.target_roots.remove(target)
self._targets.discard(target)
def targets(self, predicate=None):
"""Selects targets in-play in this run from the target roots and their transitive dependencies.
If specified, the predicate will be used to narrow the scope of targets returned.
"""
return filter(predicate, self._targets)
def dependents(self, on_predicate=None, from_predicate=None):
"""Returns a map from targets that satisfy the from_predicate to targets they depend on that
satisfy the on_predicate.
"""
core = set(self.targets(on_predicate))
dependees = defaultdict(set)
for target in self.targets(from_predicate):
if hasattr(target, 'dependencies'):
for dependency in target.dependencies:
if dependency in core:
dependees[target].add(dependency)
return dependees
def resolve(self, spec):
"""Returns an iterator over the target(s) the given address points to."""
with ParseContext.temp():
return Pants(spec).resolve()
@contextmanager
def state(self, key, default=None):
value = self._state.get(key, default)
yield value
self._state[key] = value
@contextmanager
def timing(self, label):
if self.timer:
with self.timer.timing(label):
yield
else:
yield