class Query: """ Parameters ---------- position_cutoff : float Used by SequenceDB to keep only optimizer that are positioned before the cut_off point. """ def __init__( self, include, require=None, exclude=None, subquery=None, position_cutoff=math.inf, extra_optimizations=None, ): self.include = OrderedSet(include) self.require = require or OrderedSet() self.exclude = exclude or OrderedSet() self.subquery = subquery or {} self.position_cutoff = position_cutoff if extra_optimizations is None: extra_optimizations = [] self.extra_optimizations = extra_optimizations if isinstance(self.require, (list, tuple)): self.require = OrderedSet(self.require) if isinstance(self.exclude, (list, tuple)): self.exclude = OrderedSet(self.exclude) def __str__(self): return ( "Query{inc=%s,ex=%s,require=%s,subquery=%s," "position_cutoff=%f,extra_opts=%s}" % ( self.include, self.exclude, self.require, self.subquery, self.position_cutoff, self.extra_optimizations, ) ) def __setstate__(self, state): self.__dict__.update(state) if not hasattr(self, "extra_optimizations"): self.extra_optimizations = [] # add all opt with this tag def including(self, *tags): return Query( self.include.union(tags), self.require, self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations, ) # remove all opt with this tag def excluding(self, *tags): return Query( self.include, self.require, self.exclude.union(tags), self.subquery, self.position_cutoff, self.extra_optimizations, ) # keep only opt with this tag. def requiring(self, *tags): return Query( self.include, self.require.union(tags), self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations, ) def register(self, *optimizations): return Query( self.include, self.require, self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations + list(optimizations), )
class OptimizationQuery: """An object that specifies a set of optimizations by tag/name.""" def __init__( self, include: Iterable[str], require: Optional[Union[OrderedSet, Sequence[str]]] = None, exclude: Optional[Union[OrderedSet, Sequence[str]]] = None, subquery: Optional[Dict[str, "OptimizationQuery"]] = None, position_cutoff: float = math.inf, extra_optimizations: Optional[Sequence[Tuple[Union["OptimizationQuery", OptimizersType], Union[int, float]]]] = None, ): """ Parameters ========== include: A set of tags such that every optimization obtained through this `OptimizationQuery` must have **one** of the tags listed. This field is required and basically acts as a starting point for the search. require: A set of tags such that every optimization obtained through this `OptimizationQuery` must have **all** of these tags. exclude: A set of tags such that every optimization obtained through this ``OptimizationQuery` must have **none** of these tags. subquery: A dictionary mapping the name of a sub-database to a special `OptimizationQuery`. If no subquery is given for a sub-database, the original `OptimizationQuery` will be used again. position_cutoff: Only optimizations with position less than the cutoff are returned. extra_optimizations: Extra optimizations to be added. """ self.include = OrderedSet(include) self.require = OrderedSet(require) if require else OrderedSet() self.exclude = OrderedSet(exclude) if exclude else OrderedSet() self.subquery = subquery or {} self.position_cutoff = position_cutoff self.name: Optional[str] = None if extra_optimizations is None: extra_optimizations = [] self.extra_optimizations = list(extra_optimizations) def __str__(self): return ("OptimizationQuery(" + f"inc={self.include},ex={self.exclude}," + f"require={self.require},subquery={self.subquery}," + f"position_cutoff={self.position_cutoff}," + f"extra_opts={self.extra_optimizations})") def __setstate__(self, state): self.__dict__.update(state) if not hasattr(self, "extra_optimizations"): self.extra_optimizations = [] def including(self, *tags: str) -> "OptimizationQuery": """Add rewrites with the given tags.""" return OptimizationQuery( self.include.union(tags), self.require, self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations, ) def excluding(self, *tags: str) -> "OptimizationQuery": """Remove rewrites with the given tags.""" return OptimizationQuery( self.include, self.require, self.exclude.union(tags), self.subquery, self.position_cutoff, self.extra_optimizations, ) def requiring(self, *tags: str) -> "OptimizationQuery": """Filter for rewrites with the given tags.""" return OptimizationQuery( self.include, self.require.union(tags), self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations, ) def register( self, *optimizations: Tuple["OptimizationQuery", Union[int, float]] ) -> "OptimizationQuery": """Include the given optimizations.""" return OptimizationQuery( self.include, self.require, self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations + list(optimizations), )
class OptimizationQuery: """An object that specifies a set of optimizations by tag/name.""" def __init__( self, include: Sequence[str], require: Optional[Sequence[str]] = None, exclude: Optional[Sequence[str]] = None, subquery: Optional[Dict[str, "OptimizationQuery"]] = None, position_cutoff: float = math.inf, extra_optimizations: Optional[Sequence[OptimizersType]] = None, ): """ Parameters ========== include: A set of tags such that every optimization obtained through this ``OptimizationQuery`` must have **one** of the tags listed. This field is required and basically acts as a starting point for the search. require: A set of tags such that every optimization obtained through this ``OptimizationQuery`` must have **all** of these tags. exclude: A set of tags such that every optimization obtained through this ``OptimizationQuery`` must have **none** of these tags. subquery: A dictionary mapping the name of a sub-database to a special ``OptimizationQuery``. If no subquery is given for a sub-database, the original ``OptimizationQuery`` will be used again. position_cutoff: Only optimizations with position less than the cutoff are returned. extra_optimizations: Extra optimizations to be added. """ self.include = OrderedSet(include) self.require = require or OrderedSet() self.exclude = exclude or OrderedSet() self.subquery = subquery or {} self.position_cutoff = position_cutoff self.name: Optional[str] = None if extra_optimizations is None: extra_optimizations = [] self.extra_optimizations = extra_optimizations if isinstance(self.require, (list, tuple)): self.require = OrderedSet(self.require) if isinstance(self.exclude, (list, tuple)): self.exclude = OrderedSet(self.exclude) def __str__(self): return ("OptimizationQuery(" + f"inc={self.include},ex={self.exclude}," + f"require={self.require},subquery={self.subquery}," + f"position_cutoff={self.position_cutoff}," + f"extra_opts={self.extra_optimizations})") def __setstate__(self, state): self.__dict__.update(state) if not hasattr(self, "extra_optimizations"): self.extra_optimizations = [] # add all opt with this tag def including(self, *tags): return OptimizationQuery( self.include.union(tags), self.require, self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations, ) # remove all opt with this tag def excluding(self, *tags): return OptimizationQuery( self.include, self.require, self.exclude.union(tags), self.subquery, self.position_cutoff, self.extra_optimizations, ) # keep only opt with this tag. def requiring(self, *tags): return OptimizationQuery( self.include, self.require.union(tags), self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations, ) def register(self, *optimizations): return OptimizationQuery( self.include, self.require, self.exclude, self.subquery, self.position_cutoff, self.extra_optimizations + list(optimizations), )