def test_pane_iterate_tr_rows(): pane = Pane.parse("_ Tag\t${lemma}\n") multiple_forms = ("form", "longer-form") filled_pane = pane.fill({"${lemma}": multiple_forms}) assert ilen(filled_pane.rows) == ilen(pane.rows) assert ilen(filled_pane.tr_rows) == len(multiple_forms)
def test_1(): (tree, hSubTrees) = parsePLL(''' top peach fuzzy navel pink apple red ''') n = ilen(tree.children()) assert n == 2 n = ilen(tree.siblings()) assert n == 0 # --- descendents() includes the node itself n = ilen(tree.descendents()) assert n == 6 assert ilen(tree.firstChild.children()) == 2 assert tree['label'] == 'top' assert tree.firstChild['label'] == 'peach' node = tree.firstChild.firstChild node['label'] == 'fuzzy navel'
def test_3(): s = ''' menubar file new *handler <<< my $evt = $_[0]; $evt.createNewFile(); return undef; open edit undo ''' (tree, hSubTrees) = parsePLL(s, debug=False) handler = hSubTrees['handler'] label = tree['label'] assert label == 'menubar' n = ilen(tree.children()) assert n == 2 n = ilen(tree.descendents()) assert n == 7 assert 'lHereDoc' in handler assert (handler['lHereDoc'][0] == 'my $evt = $_[0];\n$evt.createNewFile();\nreturn undef;\n')
def test_6(): s = ''' App * menubar file new open edit undo * layout row EditField SelectField ''' (tree, hSubTrees) = parsePLL(s, debug=False) subtree1 = hSubTrees['menubar'] subtree2 = hSubTrees['layout'] n = ilen(tree.descendents()) assert n == 11 assert (subtree1['label'] == 'menubar') n = ilen(subtree1.descendents()) assert n == 6 assert (subtree2['label'] == 'layout') n = ilen(subtree2.descendents()) assert n == 4 n = ilen(tree.followingNodes()) assert n == 11
def test_2(): # --- Root node can have siblings, i.e. input does not # need to be a true tree (tree, hSubTrees) = parsePLL(''' top peach fuzzy navel pink apple red next child of next ''') n = ilen(tree.children()) assert n == 2 n = ilen(tree.siblings()) assert n == 1 # --- descendents() includes the node itself n = ilen(tree.descendents()) assert n == 6 assert tree['label'] == 'top' assert tree.firstChild['label'] == 'peach' assert tree.nextSibling['label'] == 'next' assert tree.nextSibling.firstChild['label'] == 'child of next'
def test_1(): s = ''' top peach fuzzy navel pink apple red ''' (tree, hSubTrees) = parsePLL(s) n = ilen(tree.children()) assert n == 2 n = ilen(tree.descendents()) assert n == 6 assert ilen(tree.firstChild.children()) == 2 assert tree['label'] == 'top' assert tree.firstChild['label'] == 'peach' node = tree.firstChild.firstChild node['label'] == 'fuzzy navel'
def test_module(with_config) -> None: # TODO crap. if module is imported too early (on the top level, it makes it super hard to overrride config) # need to at least detect it... from my.pdfs import annotations, annotated_pdfs # todo check types etc as well assert ilen(annotations()) >= 3 assert ilen(annotated_pdfs()) >= 1
def stats(): from itertools import chain from more_itertools import ilen # todo make stats more defensive? return { 'articles' : ilen(articles()), 'highlights': ilen(chain.from_iterable(a.highlights for a in articles())), }
def org_to_exercise(o: OrgNode) -> Iterable[Res[Exercise]]: heading = o.heading [kind] = parser.kinds(heading) # todo kinda annoying to do it twice.. # FIXME: need shared attributes? cdt, _, = O._created(o) pdt = asdt(cdt) def aux(heading: str) -> Iterable[Res[Exercise]]: dt, heading = parser.extract_dt(heading) dt = asdt(dt) # meh ew, heading = parser.extract_extra(heading) dt = dt or pdt if dt is None: yield parse_error(RuntimeError('No datetime'), org=o) return try: sets, reps = parser.extract_sets_reps(heading, kind=kind) except Exception as e: yield parse_error(e, org=o, dt=dt) return # TODO hmm. if exercise doesn't have reps, then it won't be emitted at all (sets would be 0) for _ in range(sets): yield Exercise( dt=dt, kind=kind.kind, reps=reps, note=heading, # todo attach body? extra_weight=ew, src='orgmode', ) cs = o.body + '\n'.join(x.heading + '\n' + x.body for x in o[1:]) # all descendants lines = [l.strip() for l in cs.splitlines() if len(l.strip()) > 0] # try to process as list of sets (date + rep) body_res = list(chain.from_iterable(aux(l) for l in lines)) body_ok = ilen(x for x in body_res if isinstance(x, Exercise)) # or, treat it as the sets x rep log head_res = list(aux(heading)) head_ok = ilen(x for x in head_res if isinstance(x, Exercise)) if body_ok < 2: # kinda arbitrary, but I guess if there are no numbers in the body, it's unlinkely yield from head_res return if head_ok == 0: # ok, everything must have been logged in the body? yield from (r for r in body_res if not isinstance(r, Exception)) return # otherwise something's not quite right.. just emit the errors yield from (r for r in head_res + body_res if isinstance(r, Exception)) yield parse_error( RuntimeError(f'Confusing parsing:\n{head_res}\n{body_res}'), org=o)
def test_gen_tokens(): """Test gen_tokens.""" sent_numb = mit.ilen(gen_tokens(fangfang_en)) para_numb = mit.ilen(gen_tokens(fangfang_en, gen_para=1, gen_sent=0)) para_sent_numb = mit.ilen(gen_tokens(fangfang_en, gen_para=1)) assert sent_numb == 5814 assert para_numb == 885 assert para_sent_numb == 5814 + 885 assert len(list_tokens(fangfang_en)) == 5814 assert len(list_tokens(fangfang_en, gen_para=1, gen_sent=0)) == 885
def test_gen_tokens_zh(): """Test gen_tokens.""" sent_numb = mit.ilen(gen_tokens(fangfang_zh)) para_numb = mit.ilen(gen_tokens(fangfang_zh, gen_para=1, gen_sent=0)) para_sent_numb = mit.ilen(gen_tokens(fangfang_zh, gen_para=1)) assert sent_numb == 4818 assert para_numb == 745 assert para_sent_numb == 4818 + 745 assert len(list_tokens(fangfang_zh)) == 4818 assert len(list_tokens(fangfang_zh, gen_para=1, gen_sent=0)) == 745
def med_pool_vecs(embeddings, seg_pers: List[Tuple[str, str, str]], metric: str): output_size = ilen(groupby(seg_pers)) output_arr = np.empty((output_size, embeddings.shape[1]), dtype=embeddings.dtype) output_idx = 0 input_idx = 0 for grp, it in groupby(seg_pers): grp_size = ilen(it) new_input_idx = input_idx + grp_size output_arr[output_idx] = medoid_vec( embeddings[input_idx:new_input_idx], metric) input_idx = new_input_idx output_idx += 1 return output_arr
def test_steam() -> None: from my.steam.scraper import games, achievements, Achievement assert ilen(games()) > 10 ach: List[Achievement] = list(_raise_exceptions((achievements()))) assert any( [a.game_name == "Counter-Strike: Global Offensive" for a in ach])
def test_apple_types(): all_ev = list(events()) assert len(all_ev) > 10 all_types = set([Game, GameAchievement, GameLeaderboardData, Location]) assert all_types == set(map(type, all_ev)) # make sure we parsed everything without errors assert ilen(filter(lambda e: isinstance(e, Exception), all_ev)) == 0
def __len__(self) -> int: """Count the number of elements in this dictionary. `self` is this dictionary. """ return more_itertools.ilen(self.items())
def __init__(self, data: Sequence[Sequence[Union[str, int, float]]], seed: Optional[int] = None) -> None: self.data = list(data) self.len = ilen(self.data) self.rng = random.Random(x=seed) self.hist = self._get_data_hist(self.data)
def get_len_safely(value: Any, default: int = 1, return_copy: bool = False): # noinspection PyUnresolvedReferences """ Количество элементов в последовательности (Возможно передать единственное значение, тогда очевидно длина = 1) :param value: Единичное значение или последовательность :param default: Значение, возвращаемое если последовательность пустая, либо передано <None> :param return_copy: Вернуть копию, в случае если был передан генератор >>> gen_len, gen_copy = get_len_safely((x for x in range(3)), return_copy=True) >>> print("Generator len: {}".format(gen_len), '|', "Generator copy: {}".format(list(gen_copy))) Generator len: 3 | Generator copy: [0, 1, 2] >>> lst_len = get_len_safely(list(range(3))) >>> print("Iterable len: {}".format(lst_len)) Iterable len: 3 >>> one_value_len = get_len_safely(3) >>> print("One value len: {}".format(one_value_len)) One value len: 1 """ iterable, iter_copy = tee(always_iterable(value)) if ( is_lazy_sequence(value) and return_copy) else [always_iterable(value), None] iter_len = ilen(iterable) or default result = (iter_len, iter_copy) if iter_copy else iter_len return result
def _parse_range(*, unparsed_range: RangeTuple, end_parser: Converter, within_parser: Converter, parsed_range: Optional[RangeTuple] = None, error_message: Optional[str] = None) -> Optional[RangeTuple]: if parsed_range is not None: return parsed_range err_msg = error_message or RangeTuple.__doc__ after, before, within = None, None, None none_count = more_itertools.ilen( filter(lambda o: o is None, list(unparsed_range))) if none_count == 3: return None if none_count == 0: raise QueryException( "Cannot specify 'after', 'before' and 'within' at the same time!\n\n" + err_msg) [after_str, before_str, within_str] = tuple(unparsed_range) after = end_parser(after_str) if after_str is not None else None before = end_parser(before_str) if before_str is not None else None within = within_parser(within_str) if within_str is not None else None return RangeTuple(after=after, before=before, within=within)
def test_calls_messags(): assert ilen(calls()) > 10 msgs = list(messages()) assert len(msgs) > 50 from_me = list(filter(lambda x: x.from_me, msgs)) assert len(from_me) < len(msgs) assert len(from_me) > 10
def _post_load(self, data: Dict[str, Any]) -> Dict[str, Any]: """Pre-process remotes into Remote objects.""" remotes = { remote_name: Remote.from_raw(remote) for remote_name, remote in data["remotes"].items() } default_count = ilen(1 for remote in remotes.values() if remote.default) if default_count == 0: if len(remotes) == 1: default_remote = next(iter(remotes)) elif "origin" in remotes: default_remote = "origin" else: default_remote = None if default_remote: remotes[default_remote].default = True else: raise ValidationError("Repository has no default remote.", field_names="remotes") elif default_count > 1: raise ValidationError( "A repository cannot have multiple default remotes.", field_names="remotes" ) return {"remotes": remotes, "ref": data["ref"], "detach": data["detach"]}
def get_account_root_file_count(self) -> int: # Highly recommended to override this method in the particular implementation of the blockchain for # performance reasons warnings.warn( 'Using low performance implementation of get_account_root_file_count() method (override it)' ) return ilen(self.yield_blockchain_states())
def test_org_tag_handling(tmp_path: Path) -> None: src = get_test_src() # precondition assert 'heading with tags' in src html, errs = process( input=src, outdir=tmp_path, check_ids=False, active_tags=['tag2'], ) assert ilen(errs) == 0 # NOTE: there are some sneaky nbsps there.. # (defun org-html-format-headline-default-function # (and tags "   ") tags))) ## toc handling + adding tag links ## vvv NBSPS!! before = '<li><a href="#something"><span class="timestamp-wrapper"><span class="timestamp">[2019-09-02 19:45]</span></span> heading with tags <span class="tag"><span class="tag1">tag1</span> <span class="tag2">tag2</span></span></a>' after = '<li><a href="#something"><span class="timestamp-wrapper"><span class="timestamp">[2019-09-02 19:45]</span></span> heading with tags</a><span class="tag"><a class="tag1 tag-inactive" href="/tags.html">tag1</a><a class="tag2 tag-active" href="/tags.html#tag2">tag2</a></span>' # aft = '<li><a href="#something"><span class="timestamp-wrapper"><span class="timestamp">[2019-09-02 19:45]</span></span> heading with tags</a><span class="tag"><a class="tag1 tag-inactive" href="./tags.html">tag1</a><a class="tag2 tag-inactive" href="./tags.html">tag2</a></span>' assert after in html
def test_google_types(): all_ev = list(events()) assert len(all_ev) > 100 all_types = set([Location, AppInstall, LikedVideo, HtmlComment, HtmlEvent]) assert all_types == set(map(type, all_ev)) # make sure we parsed everything without errors assert ilen(filter(lambda e: isinstance(e, Exception), all_ev)) == 0
def test_includes(tmp_path: Path) -> None: i = tmp_path / 'input' i.mkdir() o = tmp_path / 'output' o.mkdir() a = i / 'a.org' a.write_text(''' * heading 1 :PROPERTIES: :CUSTOM_ID: heading1 :END: body1 * heading 2 :PROPERTIES: :CUSTOM_ID: heading2 :END: body2 ''') b = i / 'b.org' b.write_text(f''' #+INCLUDE: "a.org::#heading2" :only-contents t between #+INCLUDE: "{a}::#heading1" :only-contents t '''.strip()) html, errs = process( input=b.open(), outdir=o, check_ids=False, ) assert ilen(errs) == 0 assert html == '<p>\n body2\nbetween\n body1\n</p>\n'
def test_eval(tmp_path: Path) -> None: i = tmp_path / 'input' i.mkdir() o = tmp_path / 'output' o.mkdir() py = i / 'py' py.mkdir() te = py / 'test_eval.py' te.write_text(''' data = 'abacaba' ''') f = i / 'test.org' f.write_text(''' #+begin_src python :exports results :results output drawer :dir py :python python3 import test_eval print(test_eval.data) #+end_src '''.strip()) html, errs = process( input=f.open(), outdir=o, ) assert ilen(errs) == 0 assert html == '<p>\nabacaba\n</p>\n'
def test_compound_rows(): pane = Pane.parse("_ Tag\t${lemma}\n") row = one(pane.rows) multiple_forms = ("form", "longer-form") filled_row = row.fill({"${lemma}": multiple_forms}) assert isinstance(filled_row, CompoundRow) # Ensure all generated forms are in there: for form in multiple_forms: assert filled_row.contains_wordform(form) assert (filled_row.num_subrows == ilen(filled_row.subrows) == len(multiple_forms)), "expected as many subrows as there are forms" first_row_cells = tuple(first(filled_row.subrows).cells) assert first(row.cells).fst_tags == first_row_cells[0].fst_tags assert first_row_cells[0].row_span == len(multiple_forms) first_form = first_row_cells[-1] assert isinstance(first_form, WordformCell) assert first_form.inflection == multiple_forms[0] last_row_cells = tuple(last(filled_row.subrows).cells) assert isinstance(last_row_cells[0], SuppressOutputCell) last_form = last_row_cells[-1] assert isinstance(last_form, WordformCell) assert last_form.inflection == multiple_forms[-1]
def stat(func: Callable[[], Iterable[C]]) -> Dict[str, Any]: from more_itertools import ilen, take, first # todo not sure if there is something in more_itertools to compute this? errors = 0 def funcit(): nonlocal errors for x in func(): if isinstance(x, Exception): errors += 1 yield x it = iter(funcit()) res: Any if QUICK_STATS: initial = take(100, it) res = len(initial) if first(it, None) is not None: # todo can actually be none... # haven't exhausted res = f'{res}+' else: res = ilen(it) if errors > 0: # todo not sure, but for now ok res = f'{res} ({errors} errors)' return { func.__name__: res, }
def stats() -> Stats: from itertools import chain from more_itertools import ilen return { **stat(articles), 'highlights': ilen(chain.from_iterable(a.highlights for a in articles())), }
def users(): bingos = Bingo.query.all() n = len(data["probs"]) group = [ (user, 100 * ilen(gr) // n) for user, gr in groupby(bingos, key=lambda bng: bng.user) ] return render_template("users.html", version=version, group=group)
def proc_data(vecs, seg_pers: List[Tuple[str, str, str]], pool: str, metric: str): if pool == "min": dists = squareform(pdist(vecs, metric=metric)) sizes = [ilen(it) for _, it in groupby(seg_pers)] return min_pool_dists(dists, sizes, sizes) else: return vecs
def main(): def count_day(day): dow, (dom, year) = day return dow == 7 and dom == 1 and year != 1900 return ilen( filter(count_day, zip(days_of_week(), all_days_of_month(2000))))
def individual_metadata(dataset_text): with StringIO(dataset_text) as fp: r = csv.DictReader(fp, delimiter = ';') nrow = ilen(r) fp.seek(0) column_names = next(r) with StringIO(dataset_text) as fp: unique_keys = list(sorted(fromcsv(fp, delimiter = ';'))) return { 'nrow': nrow, 'unique_keys': unique_keys, 'column_names': column_names, }
def test_stratified(n:int, replace:bool): with open(fn, 'rb') as fp: # The function should finish quickly. def abort(*args): raise AssertionError('The function shouldn\'t take this long.') signal.signal(signal.SIGALRM, abort) signal.alarm(1) # We should get the right number of results. if n >= 0: observed = ilen(stratified_sample.sample(n, fp, replace = replace, give_up_at = 100)) assert observed == n else: with pytest.raises(ValueError): consume(stratified_sample.sample(n, fp)) signal.alarm(0)
def total(filesize, lines): ''' Levy & Lemeshow, page 30 Lohr, page 39, 219 ''' line_lengths = list(sorted(map(len, lines))) t = 0 for line_length, the_lines in groupby(line_lengths): w_i = filesize / line_length t_i = line_length * ilen(the_lines) t += w_i * t_i V_t = 0 for line_length, the_lines in groupby(line_lengths): t_psi = t_i / w_i ss = (t_psi - t) ** 2 V_t += ss return { 'expected_value': t, 'variance': V_t, }
def main(): return ilen(sum_to(200))
def num_primes(a, b): return ilen( takewhile( is_prime, iterate(a, b)))
def main(): return ilen(n for n in range(2, 1000001) if is_circular(n))
def main(): max_number = int(1e6) chains = ((n, ilen(collatz(n))) for n in range(1, max_number + 1)) return max(chains, key=lambda x: x[1])[0]