def prime_gen(): """ Gerador de números primos """ yield 2 primes = [] for value in count(start=3, step=2): stream_primes = takewhile(lambda x: x * x <= value, primes) if all(value % stream_primes != 0): primes.append(value) yield value
def splitter(lines, sep="-=", keep_idx=False): """ Splits underlined blocks without indentation (reStructuredText pattern). Parameters ---------- lines : A list of strings sep : Underline symbols. A line with only such symbols will be seen as a underlined one. keep_idx : If False (default), the function returns a collections.OrderedDict. Else, returns a list of index pairs Returns ------- A collections.OrderedDict instance where a block with underlined key like ``"Key\\n==="`` and a list of lines following will have the item (key, list of lines), in the order that they appeared in the lists input. Empty keys gets an order numbering, and might happen for example after a ``"----"`` separator. The values (lists of lines) don't include the key nor its underline, and is also stripped/trimmed as lines (i.e., there's no empty line as the first and last list items, but first and last line may start/end with whitespaces). """ separators = audiolazy.Stream( idx - 1 for idx, el in enumerate(lines) if all(char in sep for char in el) and len(el) > 0 ).append([len(lines)]) first_idx = separators.copy().take() blk_data = OrderedDict() empty_count = iter(audiolazy.count(1)) next_empty = lambda: "--Empty--{0}--".format(next(empty_count)) if first_idx != 0: blk_data[next_empty()] = lines[:first_idx] for idx1, idx2 in separators.blocks(size=2, hop=1): name = lines[idx1].strip() if lines[idx1].strip() != "" else next_empty() blk_data[name] = lines[idx1+2 : idx2] # Strips the empty lines for name in blk_data: while blk_data[name][-1].strip() == "": blk_data[name].pop() while blk_data[name][0].strip() == "": blk_data[name] = blk_data[name][1:] return blk_data
def mgl_seq(x): """ Sequence whose sum is the Madhava-Gregory-Leibniz series. [x, -x^3/3, x^5/5, -x^7/7, x^9/9, -x^11/11, ...] Returns ------- An endless sequence that has the property ``atan(x) = sum(mgl_seq(x))``. Usually you would use the ``atan()`` function, not this one. """ odd_numbers = thub(count(start=1, step=2), 2) return Stream(1, -1) * x ** odd_numbers / odd_numbers