def test_traverse(): """To test the traverse implementation we call gc.collect() while instances of all the C objects are still valid.""" acc = iteration_utilities.accumulate([]) app = iteration_utilities.applyfunc(lambda x: x, 1) cha = iteration_utilities.chained(int, float) cla = iteration_utilities.clamp([], 0, 1) com = iteration_utilities.complement(int) con = iteration_utilities.constant(1) dee = iteration_utilities.deepflatten([]) dup = iteration_utilities.duplicates([]) fli = iteration_utilities.flip(int) gro = iteration_utilities.grouper([], 2) ine = iteration_utilities.intersperse([], 1) iik = iteration_utilities.ItemIdxKey(10, 2) ite = iteration_utilities.iter_except(int, TypeError) mer = iteration_utilities.merge([]) nth = iteration_utilities.nth(1) pac = iteration_utilities.packed(int) par = iteration_utilities.partial(int, 10) rep = iteration_utilities.replicate([], 3) rou = iteration_utilities.roundrobin([]) see = iteration_utilities.Seen() sid = iteration_utilities.sideeffects([], lambda x: x) spl = iteration_utilities.split([], lambda x: True) sta = iteration_utilities.starfilter(lambda x: True, []) suc = iteration_utilities.successive([]) tab = iteration_utilities.tabulate(int) une = iteration_utilities.unique_everseen([]) unj = iteration_utilities.unique_justseen([]) gc.collect()
def valid(string): if 8 in string or 11 in string or 14 in string: return False if not any(starmap(straight, successive(string, 3))): return False pairs = unique_everseen(starfilter(eq, successive(string))) return 1 < count_items(pairs)
def _useful_items(self) -> typing.Iterator[Tuple[_KT, List[_VT]]]: """Filter out items with empty value lists. `self` is this dictionary. The method returns an iterator over dictionary items with non-empty lists. """ return iteration_utilities.starfilter(lambda _, val_lst: val_lst, self._dict.items())
def read_program(prog_file: Iterable[str]) -> List[ProgInstruction]: """Read the program stored in the given file. `prog_file` is the file containing the assembly program. The function returns the program instructions. """ program: typing.Generator[typing.Tuple[int, str], None, None] = iteration_utilities.starfilter( lambda _, line: line, enumerate(map(str.strip, prog_file), 1)) reg_registry = IndexedSet[_OperandInfo](fastcore.foundation.Self.name()) return [_create_instr(*line, reg_registry) for line in program]
def _coll_cap_edges(graph: DiGraph) -> typing.FrozenSet[_T]: """Collect capping edges from the given graph. `graph` is the graph to collect edges from. The function returns capping edges. A capping edge is the sole edge on either side of a node that determines the maximum flow through the node. """ out_degrees = graph.out_degree() in_degrees = iteration_utilities.starfilter( lambda node, in_deg: in_deg == 1 or out_degrees[node] == 1, graph.in_degree()) return frozenset( _get_cap_edge(graph.in_edges(node), graph.out_edges(node)) for node, _ in in_degrees)
def test_starfilter_failure6(): # function itself fails def failingfunc(a, b): raise ValueError('bad func') with pytest.raises(ValueError, match='bad func'): next(starfilter(failingfunc, [(T(1), T(1))]))
def test_starfilter_failure5(): # too many arguments for function call with pytest.raises(TypeError): next(starfilter(operator.eq, [(T(1), T(1), T(1))]))
def test_starfilter_failure3(): # not enough arguments for function call with pytest.raises(TypeError): next(starfilter(operator.eq, [(T(1), )]))
def test_starfilter_failure2(): # item not convertable to tuple with pytest.raises(TypeError): next(starfilter(operator.eq, [T(1)]))
def test_starfilter_failure9(): # Too few arguments with pytest.raises(TypeError): starfilter()
def test_starfilter_normal3(): inp = [(T(1), T(2)), (T(2), T(1))] assert list(starfilter(operator.eq, inp)) == []
def test_starfilter_empty1(): assert list(starfilter(operator.eq, [])) == []
def test_starfilter_pickle2(protocol): sf = starfilter(operator.eq, [(T(1), T(1)), (T(2), T(2))]) assert next(sf) == (T(1), T(1)) x = pickle.dumps(sf, protocol=protocol) assert list(pickle.loads(x)) == [(T(2), T(2))]
def test_starfilter_copy1(): _hf.iterator_copy(starfilter(operator.eq, [(T(1), T(1)), (T(2), T(2))]))
def test_starfilter_failure10(): # Changing next method with pytest.raises(_hf.CacheNext.EXC_TYP, match=_hf.CacheNext.EXC_MSG): # won't work with return_True because then "iternext" is refreshed # before the failure comes. list(starfilter(iteration_utilities.return_False, _hf.CacheNext([1])))
def test_starfilter_failure7(): # result of function has no boolean interpretation with pytest.raises(_hf.FailBool.EXC_TYP, match=_hf.FailBool.EXC_MSG): next(starfilter(lambda x, y: _hf.FailBool(), [(T(1), T(1))]))
def test_starfilter_failure8(): # Test that a failing iterator doesn't raise a SystemError with pytest.raises(_hf.FailNext.EXC_TYP, match=_hf.FailNext.EXC_MSG): next(starfilter(operator.ne, _hf.FailNext()))
def test_starfilter_normal4(): # same test as above but with lists inside. inp = [[T(1), T(2)], [T(2), T(1)]] assert list(starfilter(operator.eq, inp)) == []
logging.basicConfig( filename='.log', filemode='w', level=logging.DEBUG if args.debug else logging.CRITICAL) start, end = defaultdict(list), defaultdict(list) for rec in SeqIO.parse(args.file, 'fasta'): if kmers := find_kmers(str(rec.seq), args.k): start[kmers[0]].append(rec.id) end[kmers[-1]].append(rec.id) logging.debug(f'STARTS\n{pformat(start)}') logging.debug(f'ENDS\n{pformat(end)}') for kmer in set(start).intersection(set(end)): for pair in starfilter(op.ne, product(end[kmer], start[kmer])): print(*pair) # -------------------------------------------------- def find_kmers(seq: str, k: int) -> List[str]: """ Find k-mers in string """ n = len(seq) - k + 1 return [] if n < 1 else [seq[i:i + k] for i in range(n)] # -------------------------------------------------- def test_find_kmers() -> None: """ Test find_kmers """
def test_starfilter_attributes1(): it = starfilter(operator.eq, []) assert it.pred is operator.eq
filename='.log', filemode='w', level=logging.DEBUG if args.debug else logging.CRITICAL) start, end = defaultdict(list), defaultdict(list) for rec in SeqIO.parse(args.file, 'fasta'): if kmers := find_kmers(str(rec.seq), args.k): start[kmers[0]].append(rec.id) end[kmers[-1]].append(rec.id) logging.debug(f'STARTS\n{pformat(start)}') logging.debug(f'ENDS\n{pformat(end)}') dot = Digraph() for kmer in set(start).intersection(set(end)): for s1, s2 in starfilter(op.ne, product(end[kmer], start[kmer])): dot.node(s1) dot.node(s2) dot.edge(s1, s2) args.outfile.close() dot.render(args.outfile.name, view=args.view) print(f'Done, see outfile "{args.outfile.name}".') # -------------------------------------------------- def find_kmers(seq: str, k: int) -> List[str]: """ Find k-mers in string """ n = len(seq) - k + 1
def test_starfilter_failure1(): with pytest.raises(_hf.FailIter.EXC_TYP, match=_hf.FailIter.EXC_MSG): starfilter(operator.eq, _hf.FailIter())