Ejemplo n.º 1
0
    def take_until_match(self, start, end):
        events = []

        def is_memory(e):
            return e is not None and isinstance(e, Event) and (e.is_a(scribe.EventMemOwnedWriteExtra) or
                   e.is_a(scribe.EventMemOwnedReadExtra))

        if is_memory(start) and is_memory(end) and not start.has_syscall():
            events = list(itertools.takewhile(
                    lambda e: not is_memory(e) or (not self.mem_match(e, end) and not e.has_syscall()),
                    head(start.proc.events.after(start),
                        self.explorer.max_delete)))
            if len(events) > 0 and not self.mem_match(events[-1].next_event(), end):
                return None

        if end is not None and start.has_syscall():
            if end.nr in unistd.SYS_exit:
                return [start] + list(start.proc.events.after(start.syscall))[:-1]
            events = list(itertools.takewhile(
                    lambda e: not self.sys_match(e, end),
                    head(start.proc.syscalls.after(start.syscall),
                        self.explorer.max_delete)))
            if len(events) > 0 and not self.sys_match(events[-1].next_syscall(), end):
                return None

        events.insert(0, start)
        return events
Ejemplo n.º 2
0
def _parse(tok):
    while True:
        name = next(tok)
        if name.typ in ('newline', 'ws', 'comment'):
            continue
        if name.value == '{':
            break
        oper = next(tok)
        if name.typ == 'word':
            if oper.typ == 'op':
                if oper.value == '=':
                    firstarg = next(tok)
                    if firstarg.value == '(':
                        yield VarArray.parse(name,
                            *itertools.takewhile(
                                lambda x: x.value != ')', tok))
                        ntok = next(tok)
                        assert ntok.typ == 'newline', ntok
                    else:
                        yield VarValue.parse(name, firstarg,
                            *itertools.takewhile(
                                lambda x: x.typ != 'newline', tok))
                    continue
            elif oper.value == '(':
                assert next(tok).value == ')'
                ntok = next(tok)
                if ntok.typ == 'ws':
                    ntok = next(tok)
                assert ntok.value == '{'
                yield Function.parse(name, _parse(tok))
                continue

        yield CmdLine.parse(name, oper,
            *itertools.takewhile(lambda x: x.typ != 'newline', tok))
Ejemplo n.º 3
0
    def getNewWord(self):
        ''' Gets the new word '''
        assert self.validNewWord()
        letters = [self.getLetterPosition(l) for l in self.letters if l and not l.is_safe]
        letter = letters[0]
        direction = 'right'

        if len(letters) == 1:
            l = letters[0]
            if (l[1] - 1 >= 0 and self.getLetter(l[0], l[1] - 1)) or \
               (l[1] + 1 < self.height and self.getLetter(l[0], l[1] + 1)):
                direction = 'down'
        elif letters[0][0] == letters[-1][0]:
            direction = 'down'

        if direction == 'right':
            row = [self.getLetter(x, letter[1]) for x in range(self.width)]
            pivot = letter[0]
        else:
            row = [self.getLetter(letter[0], y) for y in range(self.height)]
            pivot = letter[1]

        word_right = list(takewhile(lambda x: x, row[pivot:]))
        word_left = row[:pivot]
        word_left.reverse()
        word_left = list(takewhile(lambda x: x, word_left))
        word_left.reverse()
        word = word_left + word_right
        x,y = self.getLetterPosition(word[0])

        return self.getLetterPosition(word[0]) + \
               (direction, ''.join(l.char for l in word).lower())
Ejemplo n.º 4
0
def geo_prog_range(lo, hi, ratio, thru=None):
	"""
	Returns a geometric progression between the given ranges.

	@param lo: low endpoint (inclusive)
	@param hi: high endpoint (inclusive)
	@param ratio: ratio of sequence
	@param through: sequence will pass through this number; defaults to low
	       endpoint.
	"""
	if thru is None:
		thru = lo

	if lo > hi:
		raise ValueError()

	if not (lo <= thru <= hi):
		raise ValueError()

	ratio = float(ratio)
	if -1 < ratio < 1:
		ratio = 1/ratio

	hiseq = list(takewhile(lambda x: lo <= x <= hi, geo_prog(thru, ratio)))
	loseq = list(takewhile(lambda x: lo <= x <= hi, geo_prog(thru, ratio, True)))

	loseq.reverse()
	return loseq + hiseq[1:]
Ejemplo n.º 5
0
    def handle_backend_response(self, metric_data, until_time):
        if not metric_data:
            raise BadBackendResponseError(
                "LValueWidget '%s' received empty response from backend")

        datapoints = metric_data[0]['datapoints']
        from_time = until_time - self.config['time_range']
        prev_time = from_time - self.config['time_range']

        prev = {
            'x': prev_time,
            'y': self.config['default_value']
        }
        for n in takewhile(lambda d: d['x'] <= prev_time, datapoints):
            prev = n

        last = {
            'x': from_time,
            'y': self.config['default_value']
        }
        for n in takewhile(lambda d: d['x'] <= from_time, datapoints):
            last = n

        return {
            'from': from_time,
            'to': until_time,
            'last': last['y'],
            'prev': prev['y'],
        }
Ejemplo n.º 6
0
def imperative_bitonic_sort(N):
  for k in takewhile(lambda x: x <= N, (2 ** x for x in count(1))):
    for j in takewhile(lambda x: x > 0, (k >> x for x in count(1))):
      for i in range(N):
        ij = i ^ j
        if ij > i:
          yield (ij, i) if (i & k) else (i, ij)
Ejemplo n.º 7
0
def solution():
    target = 1000000

    # Precompute squares for faster perfect square testing
    start = 1
    limit = 1001
    squares = set(takewhile(lambda n: n < 5*(limit**2),
                            (i*i for i in count(start))))

    cuboids = 0

    while True:
        for a in range(start, limit):
            for b in range(1, a+1):
                for c in range(1, b+1):
                    shortest_squared = a**2 + (b+c)**2

                    if shortest_squared in squares:
                        cuboids += 1

            if cuboids >= target:
                return a, cuboids

        # Double the number of available squares
        start, limit = limit, len(squares) + limit
        squares |= set(takewhile(lambda n: n < 5*(limit**2),
                                 (i*i for i in count(start))))
Ejemplo n.º 8
0
def imerge(i1,i2):

	while True:
		try:
			el1 = i1.next()
		except:
			for el in i2: yield el
			raise StopIteration
		try:
			el2 = i2.next()
		except:
			for el in i1: yield el
			raise StopIteration

		if el1 < el2:
			yield el1
			takefrom = itertools.takewhile(lambda x: x<el2,i1)
			for el in takefrom:
				yield el
			yield el2
		elif el1 > el2:
			yield el2
			takefrom = itertools.takewhile(lambda x: x<el1,i2)
			for el in takefrom:
				yield el
			yield el1
		else:
			yield el1
Ejemplo n.º 9
0
def solution(A):
    nonpositive = partial(ge, 0)
    left  = takewhile(nonpositive, A)
    right = takewhile(nonpositive, (-a for a in reversed(A)))
    merged = merge(left, right)
    groups = groupby(merged)
    return sum(1 for g in groups)
Ejemplo n.º 10
0
def start(watson, args):
    """
    Start monitoring the time for the given project. You can add tags
    indicating more specifically what you are working on with '+tag'.

    \b
    Example :
    $ watson start apollo11 +module +brakes
    Starting apollo11 [module, brakes] at 16:34
    """
    project = ' '.join(
        itertools.takewhile(lambda s: not s.startswith('+'), args)
    )

    # Find all the tags starting by a '+', even if there are spaces in them,
    # then strip each tag and filter out the empty ones
    tags = list(filter(None, map(operator.methodcaller('strip'), (
        # We concatenate the word with the '+' to the following words
        # not starting with a '+'
        w[1:] + ' ' + ' '.join(itertools.takewhile(
            lambda s: not s.startswith('+'), args[i + 1:]
        ))
        for i, w in enumerate(args) if w.startswith('+')
    ))))  # pile of pancakes !

    current = watson.start(project, tags)
    click.echo("Starting {} {} at {}".format(
        style('project', project),
        style('tags', tags),
        style('time', "{:HH:mm}".format(current['start']))
    ))
    watson.save()
Ejemplo n.º 11
0
def _pad_version(left, right):
    left_split, right_split = [], []

    # Get the release segment of our versions
    left_split.append(list(itertools.takewhile(lambda x: x.isdigit(), left)))
    right_split.append(list(itertools.takewhile(lambda x: x.isdigit(), right)))

    # Get the rest of our versions
    left_split.append(left[len(left_split):])
    right_split.append(left[len(right_split):])

    # Insert our padding
    left_split.insert(
        1,
        ["0"] * max(0, len(right_split[0]) - len(left_split[0])),
    )
    right_split.insert(
        1,
        ["0"] * max(0, len(left_split[0]) - len(right_split[0])),
    )

    return (
        list(itertools.chain(*left_split)),
        list(itertools.chain(*right_split)),
    )
def main():
    '''Extract all Python tutorials urls from this page:
    http://www.bogotobogo.com/python/pytut.php
    '''
    import itertools
    import os.path
    import re

    line_generator = fetchPage('http://www.bogotobogo.com/python/pytut.php')

    # We can call builtin function iter in two ways(Please refer to the manual):
    # 1. iter(iterable)
    # 2. iter(callable, sentinel)
    # It seems that we can't pass a generator function to 'callable' in the 2nd
    # form, so I use itertools.takewhile here.
    for line in itertools.takewhile(
        lambda x: x.strip() != '<h1>List of Python Tutorials</h1>',
        line_generator):
        pass

    # Ignore all lines until we find the HTML list's start tag: <ul>
    for line in itertools.takewhile(lambda x: x.strip() != '<ul>',
                                    line_generator):
        pass

    # Extract all the urls in the HTML list until we reach the end tag: </ul>
    with open(os.path.join(os.path.expanduser('~'), 'List_of_Python_Tutorials.txt'),
              'w') as fp:
        url_pattern = re.compile(r'<a href="(.*)">')
        
        for line in itertools.takewhile(lambda x: x.strip() != '</ul>',
                                        line_generator):
            search_res = url_pattern.search(line)
            if search_res:
                print(search_res.group(1), file = fp)
def PDB_RESN(PDB_FILE, RESN, CHAIN="A"): #STR - Returns 3 letter amino acid code corresponding to Residue number (RESN) in chain of interest

    #AMINO ACID DICTIONARY
    AADICT= {'CYS': 'C', 'ASP': 'D', 'SER': 'S', 'GLN': 'Q', 'LYS': 'K',
     'ILE': 'I', 'PRO': 'P', 'THR': 'T', 'PHE': 'F', 'ASN': 'N', 
     'GLY': 'G', 'HIS': 'H', 'LEU': 'L', 'ARG': 'R', 'TRP': 'W', 
     'ALA': 'A', 'VAL':'V', 'GLU': 'E', 'TYR': 'Y', 'MET': 'M', 
     #modified amino acids
     'MSE':'M', 'SCY':'C', 'CCS':'C', 'KCX':'K', 'MDO':'ASG', 
     'CME':'C', 'CSX':'C','CSO':'C','CSS':'C', 'SEP':'S','TPO':'T','LLP':'K',
     'SUI':'X', "NEP":"H", "FTR":"W", "TRQ":"W", "TQQ":"W", "TTQ":"W","1TQ":"W", 
     "0AF":"W", "TRW":"W" ,"TOQ":"W" }
        
    PDB_IN=open(PDB_FILE).readlines()
    
    #Get rid of comments
    PDB_IN= PDB_IN[len([a for a in itertools.takewhile(lambda x: x[0:4]!="ATOM",PDB_IN)]):]
    
    #Account for unmentioned chain "_"
    if CHAIN=="_":
        CHAIN=PDB_IN[0][21:22]
    
    #Get rid of ANISOU lines and get ATOMS for chain of interest including HETATM within chain
    PDB_IN=filter(lambda x:x[:6]!="ANISOU" and x[21:22]==CHAIN,PDB_IN)
    PDB_IN=[b for b in itertools.takewhile(lambda y: y[0:3]!="TER",PDB_IN)]
    
    #Get coordinates that correspond to residue number
    PDB_RES_NAME=[]
    for f in PDB_IN: #Use chain ATOMS including HETATM embedded if any
        if int(f[22:26])==int(RESN):
            PDB_RES_NAME.append(f[17:20]) #list of coordinates of Z atoms
    
    PDB_RES_NAME=unique(PDB_RES_NAME)
    return PDB_RES_NAME[0]
Ejemplo n.º 14
0
    def check_blank_before_after_class(slef, class_, docstring):
        """D20{3,4}: Class docstring should have 1 blank line around them.

        Insert a blank line before and after all docstrings (one-line or
        multi-line) that document a class -- generally speaking, the class's
        methods are separated from each other by a single blank line, and the
        docstring needs to be offset from the first method by a blank line;
        for symmetry, put a blank line between the class header and the
        docstring.

        """
        # NOTE: this gives false-positive in this case
        # class Foo:
        #
        #     """Docstring."""
        #
        #
        # # comment here
        # def foo(): pass
        if docstring:
            before, _, after = class_.source.partition(docstring)
            blanks_before = list(map(is_blank, before.split('\n')[:-1]))
            blanks_after = list(map(is_blank, after.split('\n')[1:]))
            blanks_before_count = sum(takewhile(bool, reversed(blanks_before)))
            blanks_after_count = sum(takewhile(bool, blanks_after))
            if blanks_before_count != 1:
                yield D203(blanks_before_count)
            if not all(blanks_after) and blanks_after_count != 1:
                yield D204(blanks_after_count)
Ejemplo n.º 15
0
def PDB_COOR(PDB_FILE, CHAIN="A"): #LIST - Return atomic coordinate lines from PDB file for chain of interest

    #AMINO ACID DICTIONARY
    AADICT= {'CYS': 'C', 'ASP': 'D', 'SER': 'S', 'GLN': 'Q', 'LYS': 'K',
     'ILE': 'I', 'PRO': 'P', 'THR': 'T', 'PHE': 'F', 'ASN': 'N', 
     'GLY': 'G', 'HIS': 'H', 'LEU': 'L', 'ARG': 'R', 'TRP': 'W', 
     'ALA': 'A', 'VAL':'V', 'GLU': 'E', 'TYR': 'Y', 'MET': 'M', 
     #modified amino acids
     'MSE':'M', 'SCY':'C', 'CCS':'C', 'KCX':'K', 'MDO':'ASG', 
     'CME':'C', 'CSX':'C','CSO':'C','CSS':'C', 'SEP':'S','TPO':'T','LLP':'K',
     'SUI':'X', "NEP":"H", "FTR":"W", "TRQ":"W", "TQQ":"W", "TTQ":"W","1TQ":"W", 
     "0AF":"W", "TRW":"W" ,"TOQ":"W" }
        
    PDB_IN=open(PDB_FILE).readlines()
    
    #Get rid of comments
    PDB_IN= PDB_IN[len([a for a in itertools.takewhile(lambda x: x[0:4]!="ATOM",PDB_IN)]):]
    
    #Account for unmentioned chain "_"
    if CHAIN=="_":
        CHAIN=PDB_IN[0][21:22]
    
    #Get rid of ANISOU lines and get ATOMS for chain of interest including HETATM within chain
    PDB_IN=filter(lambda x:x[:6]!="ANISOU" and x[21:22]==CHAIN,PDB_IN)
    PDB_IN=[b for b in itertools.takewhile(lambda y: y[0:3]!="TER",PDB_IN)]
    return PDB_IN
Ejemplo n.º 16
0
def process_buffer(buf):
    line_iter = iter(buf)
    header = next(line_iter).strip().split('\t')[1:]
    warc_header = ''.join(takewhile(lambda x: x.strip() != '', line_iter))
    http_header = ''.join(takewhile(lambda x: x.strip() != '', line_iter))
    html_content = ''.join(line_iter)
    return header, warc_header, http_header, html_content
Ejemplo n.º 17
0
def main():
    bound = 50000000
    primes = primelist()
    squares = takewhile(lambda x: x < bound, (prime**2 for prime in primes))
    cubes = takewhile(lambda x: x < bound, (prime**3 for prime in primes))
    biquadratic = takewhile(lambda x: x < bound, (prime**4 for prime in primes))
    print((len(set(s + c + t for (s, c, t) in product(squares, cubes, biquadratic) if s + c + t < bound))))
Ejemplo n.º 18
0
def main():
    limit = 50000000
    primes = euler(int(limit ** 0.5))
    squares = takewhile(lambda x: x < limit, (prime ** 2 for prime in primes))
    cubes = takewhile(lambda x: x < limit, (prime ** 3 for prime in primes))
    tesseracts = takewhile(lambda x: x < limit, (prime ** 4 for prime in primes))
    print((len(set(s + c + t for (s, c, t) in product(squares, cubes, tesseracts) if s + c + t < limit))))
Ejemplo n.º 19
0
Archivo: funcs.py Proyecto: kyleam/etc
def see(dat, n=5):
    """Preview long iterables (in interactive session)

    If `dat` is a pandas data frame, return `n` columns and rows. This
    is useful for cases where there are too many columns to effectively
    use `head`.

    If `dat` is a dict, return first `n` iterations of the keys and
    values.

    For any other types, return the first `n` iterations.
    """
    if isinstance(dat, pd.DataFrame):
        total_rows, total_cols = dat.shape
        nrows = n if n < total_rows else total_rows
        ncols = n if n < total_cols else total_cols
        return dat.iloc[:nrows, :ncols]

    def under_enum(x):
        return x[0] < n

    if isinstance(dat, dict):
        first_n = takewhile(under_enum, enumerate(dat.items()))
        return {key: vals for i, (key, vals) in first_n}

    ## all others, just return list of n iterations
    return [item for _, item in takewhile(under_enum, enumerate(dat))]
Ejemplo n.º 20
0
def _parse_stream(cue_stream):

    track_keyword = "TRACK"
    disk_keywords = ["PERFORMER", "TITLE", "CATALOG",
                     "CDTEXTFILE", "FILE", "SONGWRITER"]
    track_keywords = ["PERFORMER", "TITLE", "TRACK", "INDEX",
                      "FLAGS", "ISRC", "PREGAP", "POSTGAP"]

    cue = {}
    lines = [line.strip() for line in cue_stream]
    
    # Splits cue file on lines about tracks and lines about disk.
    # It is supposed that cue sheet contains only one FILE field
    pred = lambda line: line.find(track_keyword) == -1
    disk_info = list(itertools.takewhile(pred, lines))

    cue["disk_info"] = _parse_block(disk_info, disk_keywords, True)
    
    cue["tracks"] = []
    while True:
        lines = list(itertools.dropwhile(pred, lines))
        if not lines: break
        track_block = [lines[0]]
        lines = lines[1:]
        track_block += list(itertools.takewhile(pred, lines))
        cue["tracks"].append(_parse_block(track_block, track_keywords, False))

    # Some handmade fixes
    for track in cue["tracks"]:
        number, track_type = track["TRACK"].split()
        track["number"] = int(number)
        track["type"] = track_type
        del track["TRACK"]

    return cue
Ejemplo n.º 21
0
def problem31():
    x = 200
    ret = []

    # init
    pence = [1, 2, 5, 10, 20, 50, 100, 200]
    stack = list([i] for i in takewhile(lambda n: n<=x, pence))

    # tree traverse
    while len(stack) > 0:
        it = stack.pop()
        n = it[-1]

        # Count the remaining money r
        r = x - sum(it)
        if r < 0:
            continue
        elif r == 0:
            #print it
            ret.append(it)
            continue

        for i in takewhile(lambda a: a<=n, pence):
            stack.append( it+[i] )

    ret = len(ret)
    assert(ret == 73682)
    print 'problem31 = %d' % ret
Ejemplo n.º 22
0
def seq_range(seq:Iterable[int],ini:int,fin:int,key:Callable[...,int]=None) -> Iterator[int]:
    """Regresa un generador con los elementos en seq talque
       ini <= x <= fin para los x en seq
       (se asume que seq es una secuencia creciente de números)"""
    if key:
        return itertools.dropwhile(lambda x: ini>key(x), itertools.takewhile(lambda x: key(x)<=fin,seq))
    return itertools.dropwhile(lambda x: ini>x, itertools.takewhile(lambda x: x<=fin,seq))
Ejemplo n.º 23
0
 def get_q_bin(q):
     ilen = lambda i: sum(1 for _ in i)
     lmins = [ilen(takewhile(lambda x: x<1e-5,qe)) for qe in q]
     lmaxs = [len(qe) - ilen(takewhile(lambda x: x<1e-5,qe[::-1])) for qe in q]
     
     def q_bin(x,axis=None):
         nq = q.shape[1]
         if type(x)==slice:
             return slice_bins(lmins=lmins,
                               lmaxs=lmaxs,
                               lslice=x)
         else:
             if axis==0 or axis is None and x.ndim==1: 
                 nl = min(nq,x.shape[0])
                 nqp = slice_bins(lmins, lmaxs=lmaxs, lslice=slice(0,nl))
                 return dot(q[nqp,:nl],x[:nl])
             elif axis==1: 
                 nl = min(nq,x.shape[1])
                 nqp = slice_bins(lmins, lmaxs=lmaxs, lslice=slice(0,nl))
                 return dot(x[...,:nl],q[nqp,:nl].T)
             elif axis is None:
                 nl = min(nq,x.shape[0])
                 nqp = slice_bins(lmins, lmaxs=lmaxs, lslice=slice(0,nl))
                 return dot(dot(q[nqp,:nl],x[:nl,:nl]),q[nqp,:nl].T)
             
     q_bin.q = q
     q_bin.lmaxs = lmaxs
     q_bin.lmins = lmins
     return q_bin
Ejemplo n.º 24
0
def longest_subpalindrome_slice(text):
    "Return (i, j) such that text[i:j] is the longest palindrome in text."
    ''' Since a palindrome has a center and characters around the center are
    symmetric. We search all the centers in ''text'' and keep track of the
    longest palindrome so far. '''
    text = text.lower()
    txtlen = len(text)
    max_paln = null_paln = (0, -1)
    for cpos in xrange(0, txtlen):
        # Odd number has the center text[cpos]
        res = list(itertools.takewhile(
                lambda x: text[x[0]] == text[x[1]],
                ((i, j) for i, j in itertools.izip(
                        range(cpos, -1, -1), range(cpos, txtlen)))))
        odd_paln = res[-1]
        res = list(itertools.takewhile(
                lambda x: text[x[0]] == text[x[1]],
                ((i, j) for i, j in itertools.izip(
                        range(cpos-1, -1, -1), range(cpos, txtlen)))))
        even_paln = res[-1] if res else null_paln
        max_paln = max((max_paln, odd_paln, even_paln), key=lambda x: (x[1] - x[0]))
    # Returns a Python Style range
    max_paln =  (max_paln[0], max_paln[1] + 1)
    print max_paln
    return max_paln
Ejemplo n.º 25
0
def numeric_range(*args):
    """An extension of the built-in ``range()`` function whose arguments can
    be any orderable numeric type.

    With only *stop* specified, *start* defaults to ``0`` and *step*
    defaults to ``1``. The output items will match the type of *stop*:

        >>> list(numeric_range(3.5))
        [0.0, 1.0, 2.0, 3.0]

    With only *start* and *stop* specified, *step* defaults to ``1``. The
    output items will match the type of *start*:

        >>> from decimal import Decimal
        >>> start = Decimal('2.1')
        >>> stop = Decimal('5.1')
        >>> list(numeric_range(start, stop))
        [Decimal('2.1'), Decimal('3.1'), Decimal('4.1')]

    With *start*, *stop*, and *step*  specified the output items will match
    the type of ``start + step``:

        >>> from fractions import Fraction
        >>> start = Fraction(1, 2)  # Start at 1/2
        >>> stop = Fraction(5, 2)  # End at 5/2
        >>> step = Fraction(1, 2)  # Count by 1/2
        >>> list(numeric_range(start, stop, step))
        [Fraction(1, 2), Fraction(1, 1), Fraction(3, 2), Fraction(2, 1)]

    If *step* is zero, ``ValueError`` is raised. Negative steps are supported:

        >>> list(numeric_range(3, -1, -1.0))
        [3.0, 2.0, 1.0, 0.0]

    Be aware of the limitations of floating point numbers; the representation
    of the yielded numbers may be surprising.

    """
    argc = len(args)
    if argc == 1:
        stop, = args
        start = type(stop)(0)
        step = 1
    elif argc == 2:
        start, stop = args
        step = 1
    elif argc == 3:
        start, stop, step = args
    else:
        err_msg = 'numeric_range takes at most 3 arguments, got {}'
        raise TypeError(err_msg.format(argc))

    values = (start + (step * n) for n in count())
    if step > 0:
        return takewhile(partial(gt, stop), values)
    elif step < 0:
        return takewhile(partial(lt, stop), values)
    else:
        raise ValueError('numeric_range arg 3 must not be zero')
Ejemplo n.º 26
0
def it_filter():
    # drop_while返回一个迭代器,返回输入可迭代对象中条件第一次为false之后的元素
    print list(it.dropwhile(lambda x: x > 1, iter([0, 1, 2, -1])))
    print list(it.dropwhile(lambda x: x > 1, [0, 1, 2, -1]))

    # take_while返回一个迭代器,返回输入可迭代对象中为True的对象,直到碰到一个False
    print list(it.takewhile(lambda x: x > 1, iter([0, 1, 2, -1])))
    print list(it.takewhile(lambda x: x > 1, iter([2, 1, 2, -1])))
Ejemplo n.º 27
0
    def parse(cls, line):
        """Parse a raw string into a Line.

        Also should raise on any invalid line.  It will be quite liberal with
        hostmasks (accepting such joys as '' and 'user1@user2@user3'), but
        trying to enforce strict validity in hostmasks will be slow.

        """
        if not line:
            _logger.warning("Blank line passed in!")
            return

        raw_line = line
        tags = None
        hostmask = None
        params = list()

        # Do we have tags?
        if line[0] == '@':
            space = line.index(' ')  # Grab the separator
            tags = Tags.parse(line[1:space])
            line = line[space:].lstrip()

        # Do we have a hostmask?
        if line[0] == ':':
            space = line.index(' ')  # Grab the separator
            hostmask = Hostmask.parse(line[1:space])
            line = line[space:].lstrip()

        # Grab command
        command = reduce(operator.concat,
                         takewhile(lambda char: char not in (' ', ':'), line))
        assert len(command) > 0

        line = line[len(command):].lstrip().rstrip('\r\n')

        # Retrieve parameters
        while len(line) > 0:
            next_param = ''
            line = line.lstrip()

            if not line:
                # XXX is this really the correct behaviour?
                next_param = ''
                break

            if line[0] == ':':
                next_param = line[1:]
                line = ''
            else:
                next_param = reduce(operator.concat,
                                    takewhile(lambda char: char != ' ', line))
                line = line[len(next_param):]

            params.append(next_param)

        return cls(tags=tags, hostmask=hostmask, command=command,
                   params=params, line=raw_line)
Ejemplo n.º 28
0
  def __init__(self, nesting, spec):
    assert spec, 'Reactive called with empty argument'

    indent = spec[0].line.GetIndent()
    for elem in spec[1:]:
      if elem.line.GetIndent() != indent:
        elem.ReportError('indentation change in a group')
        break

    # Compute self._nesting, which should be the longest group
    # subsequence that has strictly increasing indentation and ends at
    # the current group.  Invariant: the nesting argument holds a
    # mutable duplicate copy of self._nesting of the last created
    # Reactive object.
    while nesting and nesting[-1][0] >= indent:
      nesting.pop()
    nesting.append((indent, spec[0].line.lineno))
    self._nesting = list(nesting)

    def IsTemplate(obj):
      return isinstance(obj, directive.Template)

    def IsMarker(obj):
      return isinstance(obj, directive.Marker)

    def IsSend(obj):
      return isinstance(obj, directive.Send)

    self._patterns = []
    index = 0

    while index < len(spec) and IsTemplate(spec[index]):
      markers = list(itertools.takewhile(IsMarker, spec[index+1:]))
      self._patterns.append(Pattern(spec[index], markers))
      index += len(markers)+1
    self._actions = list(itertools.takewhile(IsSend, spec[index:]))

    # Ashier interprets the final pattern in a group as a partial-line
    # pattern and all others as full-line patterns.  In accordance
    # with that interpretation, we add an EOL marker to each pattern
    # except for the last.
    for pattern in self._patterns[:-1]:
      pattern.AttachEOLMarker()

    if not self._patterns:
      spec[0].ReportError('group has no templates')

    if index+len(self._actions) < len(spec):
      non_action = spec[index+len(self._actions)]
      non_action.ReportError('template/marker after action')

    bound_names = set()
    for pat in self._patterns:
      bound_names.update(pat.bound_names)
    for send in self._actions:
      free_names = send.References().difference(bound_names)
      for name in free_names:
        send.ReportError('unbound name: %s' % name)
Ejemplo n.º 29
0
def solution1():
    triangles   = (n * (n + 1) // 2 for n in count(143))
    pentagonals = (n * (3 * n - 1) // 2 for n in count(165))
    hexagonals  = (n * (2 * n - 1) for n in count(285))

    for h in hexagonals:
        for p in takewhile(lambda x: x <= h, pentagonals): 
            for t in takewhile(lambda x: x <= p, triangles): 
                if p == t == h: return t
Ejemplo n.º 30
0
def sum_distinct_m(N):
    '''Return the sum of distinct M(p,q,N) for all p<q prime pairs.''' 
    P, p_max = map(long, primes('lt', int(ceil(N * 0.5)) + 1)), N ** 0.5
    return sum(max(p ** k * q ** l
                   for k in xrange(1, int(log(float(N) / q) / log(p)) + 1) 
                   for l in xrange(1, int(log(float(N) / p ** k) / log(q)) + 1))
               for (p, q) in
               ((p, q) for (i, p) in takewhile(lambda p: p[1] <= p_max, enumerate(P))
                for q in (P[j] for j in takewhile(lambda j: P[j] <= N / P[i], xrange(i + 1, len(P))))))
Ejemplo n.º 31
0
def get_primes_sieve(n):
    return list(itertools.takewhile(lambda p: p < n, sieve()))
Ejemplo n.º 32
0
 def count_spaces(string):
     spaces = 0
     for x in itertools.takewhile(str.isspace, str(string)):
         spaces += 1
     return spaces
Ejemplo n.º 33
0
def decode(model,
           input_fn,
           vocab,
           outstream=sys.stdout,
           hooks=[],
           save_all=False,
           print_query=False):
    class NoStream():
        def write(self, _):
            pass

    if outstream is None:
        outstream = NoStream()
    predictions = model.predict(input_fn, hooks=hooks)
    inputwise_hypotheses = {}
    inputwise_hypotheses = defaultdict(lambda: [], inputwise_hypotheses)

    def hypothesis_make_string(hypothesis, spaces=True):
        eng = ""
        for k in range(len(hypothesis)):
            if hypothesis[k] < 0 or hypothesis[k] == vocab.end_token_id:
                break
            eng += vocab.idx2word[hypothesis[k]]
            if spaces:
                eng += ' '
        return eng

    current_query = ""
    current_response = ""
    current_speaker = np.ones(1) * -1
    i = 0
    for pred in predictions:
        i += 1
        processed_inp = ''
        if current_query != hypothesis_make_string(pred["inputs"]) or (
                "speaker_ids" in pred
                and not np.array_equal(current_speaker, pred["speaker_ids"])):
            inputwise_hypotheses = {}
            inputwise_hypotheses = defaultdict(lambda: [],
                                               inputwise_hypotheses)
            current_query = hypothesis_make_string(pred["inputs"])
            if "speaker_ids" in pred:
                current_speaker = pred["speaker_ids"]
            if print_query:
                if current_query != "":
                    outstream.write("\n")
                outstream.write("Inputs: " + current_query + "\n")
            inp = itertools.takewhile(lambda x: x != vocab.end_token_id,
                                      pred["inputs"])  # strip the EOS
            processed_inp = ' '.join(vocab.idx2word[idx] for idx in list(inp))
            if "targets" in pred:
                outstream.write("Targets: " +
                                hypothesis_make_string(pred["targets"]) + "\n")
            if not save_all and len(hooks) > 0 and isinstance(
                    hooks[0], InteractiveInputHook):
                hooks[0].context_ls.append(
                    current_query.split(hooks[0].eos_token + ' ')[-1])
                hooks[0].context_ls.append(
                    hypothesis_make_string(pred["outputs"]))
                hooks[0].context_ls = hooks[0].context_ls[2:]
        elif current_response == hypothesis_make_string(pred["outputs"]):
            continue
        current_response = hypothesis_make_string(pred["outputs"])
        inputwise_hypotheses[processed_inp].append(pred)
        #(pred["inputs"], pred["scores"], pred["outputs"], pred["speaker_ids"]))
        if save_all and len(hooks) > 0 and isinstance(hooks[0],
                                                      InteractiveInputHook):
            hooks[0].pre_mmi = inputwise_hypotheses[processed_inp]
        outstream.write(
            "%f : %s\n" %
            (pred["scores"], hypothesis_make_string(pred["outputs"])))
    for _, v in inputwise_hypotheses.items():
        v.sort(key=lambda x: x["scores"], reverse=True)
    return inputwise_hypotheses
Ejemplo n.º 34
0
iterCycle = itertools.cycle(['a', 'b', 'c', 'd', 'e'])
nCount = 5
for item in iterCycle:
    print(item)
    time.sleep(.1)
    nCount -= 1
    if (nCount <= 0):
        break

#repeat,repeat()负责把一个元素无限重复下去,不过如果提供第二个参数就可以限定重复次数:

iterRepeat = itertools.repeat('abc', 5)
for i in iterRepeat:
    print(i)
    time.sleep(.1)

#takewhile
iterTakeWhile = itertools.takewhile(lambda x: (x < 10), itertools.count(1, 1))
for x in iterTakeWhile:
    print(x)
    time.sleep(.5)

#chain
for x in itertools.chain('abc', 'def'):
    print(x)

#group

for x in itertools.groupby('abcdefeffffabc'):
    print(x)
Ejemplo n.º 35
0
#!/usr/bin/env python3

import itertools

"""
算法有两个重要的优化点:
    1. 组合中的最大BIGGEST有限制,也就是考虑前9个数和最小的情况
    2. 组合按生序排列的首元素有最大限制,首元素是我们递归试算的起始点
"""

BIGGEST = 100 - sum(range(1, 10))
MAX_HEAD = list(itertools.takewhile(lambda x:sum(x) <= 100, (range(1, 101)[i:i+10] for i in range(0, 100))))[-1][0]

def fetch(current, selected=[]):
    if current <= BIGGEST and len(selected) < 10:
        selected_ = selected + [current]
        sum_val = sum(selected_)
        n = len(selected_)
        if sum_val == 100 and n == 10:
            yield selected_
        elif sum_val < 100 and n < 10:
            for i in range(current+1, 100 - sum_val + 1):
                yield from fetch(i, selected_)


if __name__ == '__main__':
    choices = [c for h in range(1, MAX_HEAD + 1) for c in fetch(h)]
    for c in choices:
        print(','.join(str(i) for i in c))
Ejemplo n.º 36
0
from itertools import count, takewhile

with open('p042_words.txt') as f:
    words = [
        sum(ord(c) - ord('A') + 1 for c in w)
        for w in (n.strip('"') for n in f.read().split(','))
    ]

triangles = set(
    takewhile(lambda i: i <= max(words), ((n + 1) * n / 2 for n in count(1))))
print sum(1 for c in words if c in triangles)
Ejemplo n.º 37
0
def dump(dump_path, parse_path, memory, debug_info, target, branch):
    address = locate_core_dump(memory, debug_info)
    dump_type = debug_info.variables['rpm_core_dump'].vartype
    rpm_core_dump = decode_object('rpm_core_dump', address, dump_type, memory, debug_info)

    rpm_ulogContext_type = debug_info.variables['rpm_ulogContext'].vartype
    ulog_state = rpm_core_dump.ulog_state.address
    rpm_ulogContext = decode_object('rpm_ulogContext', ulog_state, rpm_ulogContext_type, memory, debug_info)

    log = rpm_ulogContext.logHead
    if log.address == 0:
        logger.error('Failed to find any RPM ulogs (rpm_ulogContext.logHead == NULL)!')
        return

    if log.version != 0x1000:
        logger.error('This script only knows how to dump RPM ULog version 0x1000 (Originating July 2012)')
        if log.version in [2, 3, 4]:
            logger.error('It appears your logs are version %d' % log.version)
        else:
            logger.error('Your log version (%d) is unknown to this script.  Is your log corrupted?' % log.version)
        return

    while log.address != 0:
        log_name = ''.join(itertools.takewhile(lambda c: c != '\0', (chr(c) for c in log.name)))

        log_enabled = (log.logStatus & 0x2) != 0
        if not log_enabled:
            logger.debug('Not dumping ulog (disabled): %d' % log_name)
            continue

        use_64_bit_timestamps = (log.feature_flags1 & 0x1) != 0

        logger.debug('Dumping ulog: %s' % log_name)
        log_file_name = os.path.join(dump_path, '%s.ulog' % log_name)
        with open(log_file_name, 'w') as log_file:
            log_buffer   = log.buffer
            log_size     = log.bufSize
            log_mask     = log.bufSizeMask
            read         = log.readWriter
            readers_read = log.read
            write        = log.write
            bytes_in_log = write - read

            if bytes_in_log > log_size:
                logger.warning('While parsing ulog "%s" -> reported log size %d bigger than the expected log size %d' % (log_name, bytes_in_log, log_size)) 
                logger.warning('The most common cause of this is memory corruption, under-voltage, or writes to unpowered RAM.')
                logger.warning('Will try to continue with this ULog decode, but results may be unpredictable.')

            while read < write:
                # Advance past the message format value
                read = read + 2
                
                # Read the number of bytes in this message
                msg_length = log_buffer[read & log_mask] + (log_buffer[(read + 1) & log_mask] << 8)
                
                # Back up to the format for later
                read = read - 2
                      
                # Handle the current ULog message
                dump_ulog_msg(log_file, log_buffer, log_mask, read, msg_length)
                
                # Move our read pointer past the message we just finished (and 4 byte align it)
                read = read + ((msg_length + 3) & 0xFFFFFFFC)

        log = log.next

    # Now run the second-stage parsing script, if applicable.
    rpm_external_log = os.path.join(dump_path, 'RPM External Log.ulog')
    if os.path.exists(rpm_external_log):
        print_p('\t\tPost-processing the RPM "external" log...')
        parser = parse_path+'rpm_log_bfam.py'
        parser_params = ['python', parser, '-f', rpm_external_log, '-t', target, '-b', branch]

        try:
            pretty_output = subprocess.check_output(parser_params)
            pretty_output = pretty_output.replace('\r\n', '\n')
        except subprocess.CalledProcessError:
            pretty_output = '<failed to run log parser>\n'
        with open(os.path.join(dump_path, 'rpm-log.txt'), 'w') as pretty_log:
            pretty_log.write(pretty_output)

        try:
            parser_params = parser_params + ['-r']
            raw_output = subprocess.check_output(parser_params)
            raw_output = raw_output.replace('\r\n', '\n')
        except subprocess.CalledProcessError:
            raw_output = '<failed to run log parser>\n'
        with open(os.path.join(dump_path, 'rpm-rawts.txt'), 'w') as raw_log:
            raw_log.write(raw_output)
Ejemplo n.º 38
0
 def count_lines_in_file(file_path: str):
     with open(file_path, 'rb') as f:
         bufgen = takewhile(lambda x: x, (f.raw.read(1024 * 1024) for _ in repeat(None)))
         return sum(buf.count(b'\n') for buf in bufgen)
Ejemplo n.º 39
0
def f():
    for i in range(5):
        #yeld = return
        yield i


gen = f()
print(list(gen))

################  receive numbers: n^2 < 100

import itertools
inf_list = itertools.count(0)

l1 = list(itertools.takewhile(lambda x: x * x < 100, inf_list))
print(l1)

####  groupby  - to save stack of pixels

data = [0] * 10 + [1] * 1 + [0] * 4
list01 = list(itertools.groupby(data))
print(list01)
#[(value, len(list01))] itertools.groupby(data))

##  combinatorika point 2d, receive all combinatoions of naibours

list = list(itertools.product([-1, 0, 1], repeat=2))
print(list)
list(itertools.combinations([-1, 0, 1], 2))
print("comb = ", list)
Ejemplo n.º 40
0
def _test_takewhile():
    for item in itertools.takewhile(pred, seq):
        print item
Ejemplo n.º 41
0
    def __init__(self, lines):

        self.lines = []

        # Custom work for a code block
        is_inside_code_block = False
        code_buffer = []
        code_block_indent = None
        for line in lines:

            is_code_block = _code_block_marker == line.lstrip()[:3]

            if is_code_block:
                is_inside_code_block = not is_inside_code_block

            if is_code_block or is_inside_code_block:
                code_buffer.append(line.rstrip())

            if is_code_block and not is_inside_code_block:
                space_ITR = itertools.takewhile(lambda x: x == ' ', line)
                code_block_indent = len(list(space_ITR))

                # Remove the code buffer lines
                code_buffer = code_buffer[1:-1]

                # Empty out the contents of the buffer
                code_block = '__CODE_BLOCK_SPACE'.join(code_buffer)
                header = code_block_indent * ' ' + '@codeblock '
                block = header + code_block
                self.lines.append(block)

                code_buffer = []
            elif not is_inside_code_block:
                self.lines.append(line)

        # Parse and filter for blank lines
        self.lines = [x for x in map(tagline, self.lines) if not x.empty]

        # Section shouldn't be empty
        assert (self.lines)

        # Section should start with a header
        assert (self.lines[0].is_header())

        soup = bs4.BeautifulSoup("", 'html.parser')
        lines = iter(self)

        # Parse the header
        z = lines.next().build(indent=-5)
        soup.append(z)

        for x in lines:
            tag = x.build(indent=x.indent)
            name = x.primary_name
            if name in ["background", "background_video", "unsplash"]:
                assert (z.name == "section")
                z.append(tag)
                tag = soup.new_tag("div", indent=-2)
                tag["class"] = [
                    "wrap",
                ]
                z.append(tag)

            elif name == "footer":
                z.findParent('section').append(tag)

            elif x.indent > z["indent"]:
                # Append to the deepest child
                children = z.find_all()
                if not children:
                    z.append(tag)
                else:
                    children[-1].append(tag)

            elif x.indent == z["indent"]:
                z.parent.append(tag)

            elif x.indent < z["indent"]:
                while "indent" not in z.attrs or x.indent < z["indent"]:
                    z = z.parent

                # Take one more step so we are on the parent
                z.parent.append(tag)

            z = tag

        # We need to resoup the pot
        soup = bs4.BeautifulSoup(unicode(soup), 'html.parser')

        # Remove all the indent tags
        for tag in soup.find_all(True, indent=True):
            del tag.attrs["indent"]

        # If there are any li elements without a proper parent ul,ol
        # wrap them in one
        for tag in soup.find_all('li'):
            parent = tag.parent

            if parent.name not in ['ol', 'ul']:
                ul = soup.new_tag('ul')
                ul['class'] = [
                    'markdownlist',
                ]

                for x in tag.find_next_siblings('li'):
                    ul.append(x.extract())
                ul.insert(0, copy.copy(tag))

                tag.replaceWith(ul)

        # Remove all the text tags and replace with a string
        # for tag in soup.find_all("text"):
        #    tag.unwrap()

        self.soup = soup
Ejemplo n.º 42
0
    def reframe(self, target=None, action=None):
        self.log.debug('reframe(target=%s, action=%s) window=%s', repr(target),
                       repr(action), repr(self.window))

        cursor, chunk = next(self.window.view(self.window.cursor, 'backward'))

        if action == 'pagedown':
            self.head = self.sill
            if self.head.offset > 0:
                self.head.offset -= 1
            self.log.debug('reframe pagedown to %s', self.head)
            self.reframe_state = 'soft'
            self.old_cursor = self.window.cursor
            return
        elif action == 'pageup':
            screenlines = self.height - 1 - self.head.offset
            self.log.debug('reframe pageup, screenline=%d', screenlines)
        elif action == 'clever-down':
            screenlines = max(self.height - self.chunksize(chunk), 0)
        elif target is None:
            screenlines = self.height // 2
        elif target >= 0:
            screenlines = min(self.height - 1, target)
        else:  # target < 0
            screenlines = max(self.height + target, 0)

        self.log.debug('reframe, previous frame=%s', repr(self.head))
        self.log.debug('reframe, height=%d, target=%d', self.height,
                       screenlines)

        self.head = Location(self, cursor)
        self.log.debug('reframe, initial, mark=%x: %s', id(cursor),
                       repr(self.head))

        view = self.window.view(self.window.cursor, 'backward')

        mark, chunk = next(view)
        self.log.debug('reframe looking for cursor, mark=%s, chunk=%s',
                       repr(mark), repr(chunk))
        chunk = itertools.takewhile(lambda x: 'visible' not in x[0], chunk)
        chunk = list(chunk)
        chunklines = self.chunksize(chunk)
        self.log.debug('reframe cursor chunk, screenlines=%d, chunklines=%s',
                       screenlines, chunklines)
        if not chunklines:
            self.log.debug('reframe, not chunklines, chunk=%s', chunk)
        screenlines -= chunklines
        self.log.debug(
            'reframe cursor chunk, loop bottom, mark=%x, /offset=%d', id(mark),
            max(0, -screenlines))

        if screenlines <= 0:
            self.head = Location(self, mark, max(0, -screenlines - 1))
        else:
            for mark, chunk in view:
                chunklines = self.chunksize(chunk)
                self.log.debug('reframe, screenlines=%d, len(chunklines)=%s',
                               screenlines, chunklines)
                screenlines -= chunklines
                if screenlines <= 0:
                    break
                self.log.debug('reframe, loop bottom, mark=%x, /offset=%d',
                               id(mark), max(0, -screenlines))
            self.head = Location(self, mark, max(0, -screenlines))

        self.log.debug('reframe, post-loop,   mark=%x, /offset=%d', id(mark),
                       max(0, -screenlines))
        self.log.debug('reframe, post-loop, screenlines=%d, head=%s',
                       screenlines, repr(self.head))
Ejemplo n.º 43
0
def rawincount(filename):
    f = gzip.open(filename, 'rt')
    bufgen = takewhile(lambda x: x, (f.read(8192*1024) for _ in repeat(None)))
    return sum( buf.count('\n') for buf in bufgen )
Ejemplo n.º 44
0
def semistructured_statements(
    doc: Doc,
    entity: str,
    *,
    cue: str = "be",
    ignore_entity_case: bool = True,
    min_n_words: int = 1,
    max_n_words: int = 20,
) -> Tuple[Union[Span, Token], Union[Span, Token], Span]:
    """
    Extract "semi-structured statements" from a spacy-parsed doc, each as a
    (entity, cue, fragment) triple. This is similar to subject-verb-object triples.

    Args:
        doc
        entity: a noun or noun phrase of some sort (e.g. "President Obama",
            "global warming", "Python")
        cue: verb lemma with which ``entity`` is associated
            (e.g. "talk about", "have", "write")
        ignore_entity_case: If True, entity matching is case-independent
        min_n_words: Min number of tokens allowed in a matching fragment
        max_n_words: Max number of tokens allowed in a matching fragment

    Yields:
        Next matching triple, consisting of (entity, cue, fragment).

    Notes:
        Inspired by N. Diakopoulos, A. Zhang, A. Salway. Visual Analytics of
        Media Frames in Online News and Blogs. IEEE InfoVis Workshop on Text
        Visualization. October, 2013.

        Which itself was inspired by by Salway, A.; Kelly, L.; Skadiņa, I.; and
        Jones, G. 2010. Portable Extraction of Partially Structured Facts from
        the Web. In Proc. ICETAL 2010, LNAI 6233, 345-356. Heidelberg, Springer.
    """
    if ignore_entity_case is True:
        entity_toks = entity.lower().split(" ")
        get_tok_text = lambda x: x.lower_  # noqa: E731
    else:
        entity_toks = entity.split(" ")
        get_tok_text = lambda x: x.text  # noqa: E731
    first_entity_tok = entity_toks[0]
    n_entity_toks = len(entity_toks)
    cue = cue.lower()
    cue_toks = cue.split(" ")
    n_cue_toks = len(cue_toks)

    def is_good_last_tok(tok):
        if tok.is_punct:
            return False
        if tok.pos in {CONJ, DET}:
            return False
        return True

    for sent in doc.sents:
        for tok in sent:

            # filter by entity
            if get_tok_text(tok) != first_entity_tok:
                continue
            if n_entity_toks == 1:
                the_entity = tok
                the_entity_root = the_entity
            if tok.i + n_cue_toks >= len(doc):
                continue
            elif all(
                    get_tok_text(tok.nbor(i=i + 1)) == et
                    for i, et in enumerate(entity_toks[1:])):
                the_entity = doc[tok.i:tok.i + n_entity_toks]
                the_entity_root = the_entity.root
            else:
                continue

            # filter by cue
            terh = the_entity_root.head
            if terh.lemma_ != cue_toks[0]:
                continue
            if n_cue_toks == 1:
                min_cue_i = terh.i
                max_cue_i = terh.i + n_cue_toks
                the_cue = terh
            elif all(
                    terh.nbor(i=i + 1).lemma_ == ct
                    for i, ct in enumerate(cue_toks[1:])):
                min_cue_i = terh.i
                max_cue_i = terh.i + n_cue_toks
                the_cue = doc[terh.i:max_cue_i]
            else:
                continue
            if the_entity_root in the_cue.rights:
                continue

            # now add adjacent auxiliary and negating tokens to the cue, for context
            try:
                min_cue_i = min(left.i for left in itertools.takewhile(
                    lambda x: x.dep_ in {"aux", "neg"},
                    reversed(list(the_cue.lefts)),
                ))
            except ValueError:
                pass
            try:
                max_cue_i = max(right.i for right in itertools.takewhile(
                    lambda x: x.dep_ in {"aux", "neg"}, the_cue.rights))
            except ValueError:
                pass
            if max_cue_i - min_cue_i > 1:
                the_cue = doc[min_cue_i:max_cue_i]
            else:
                the_cue = doc[min_cue_i]

            # filter by fragment
            try:
                min_frag_i = min(right.left_edge.i for right in the_cue.rights)
                max_frag_i = max(right.right_edge.i
                                 for right in the_cue.rights)
            except ValueError:
                continue
            while is_good_last_tok(doc[max_frag_i]) is False:
                max_frag_i -= 1
            n_fragment_toks = max_frag_i - min_frag_i
            if (n_fragment_toks <= 0 or n_fragment_toks < min_n_words
                    or n_fragment_toks > max_n_words):
                continue
            # HACK...
            if min_frag_i == max_cue_i - 1:
                min_frag_i += 1
            the_fragment = doc[min_frag_i:max_frag_i + 1]

            yield (the_entity, the_cue, the_fragment)
Ejemplo n.º 45
0
    print("continue")
    yield "BBB"
    print("end")


temp = iter(generator_ex1())
for v in temp:
    print(v)
temp2 = [x * 2 for x in generator_ex1()]
temp3 = (x * 2 for x in generator_ex1())
print(temp2)
print(temp3)

import itertools
gen1 = itertools.count(1, 2.5)
gen2 = itertools.takewhile(lambda n: n < 1000, itertools.count(1, 2.5))
gen3 = itertools.filterfalse(lambda n: n < 3, [1, 2, 3, 4, 5])
gen4 = itertools.accumulate([x for x in range(1, 101)])
gen5 = itertools.chain("ABCDE", range(1, 11, 2))
gen6 = itertools.chain(enumerate("ABCDE"))
gen7 = itertools.product("ABCDE", repeat=2)
gen8 = itertools.groupby("AAABBCCCDDDEEE")

print(next(gen1))
print(next(gen1))
print(next(gen2))
print(next(gen2))
print(next(gen3))
print(next(gen3))
print(next(gen4))
print(next(gen4))
Ejemplo n.º 46
0
def common_prefix(paths, sep='/'):
    bydirectorylevels = zip(*[p.split(sep) for p in paths])
    return sep.join(x[0]
                    for x in takewhile(all_names_equal, bydirectorylevels))
Ejemplo n.º 47
0
def readcstr(f):
    toeof = iter(functools.partial(f.read, 1), b'')
    return (b''.join(itertools.takewhile(b'\0'.__ne__, toeof))).decode("ASCII")
Ejemplo n.º 48
0
def get_primes_erat(n):
    return list(itertools.takewhile(lambda p: p < n, erat2()))
Ejemplo n.º 49
0
def build(build_python, build_java):
    if tuple(sys.version_info[:2]) not in SUPPORTED_PYTHONS:
        msg = ("Detected Python version {}, which is not supported. "
               "Only Python {} are supported.").format(
                   ".".join(map(str, sys.version_info[:2])),
                   ", ".join(".".join(map(str, v)) for v in SUPPORTED_PYTHONS))
        raise RuntimeError(msg)

    if is_invalid_windows_platform():
        msg = ("Please use official native CPython on Windows,"
               " not Cygwin/MSYS/MSYS2/MinGW/etc.\n" +
               "Detected: {}\n  at: {!r}".format(sys.version, sys.executable))
        raise OSError(msg)

    bazel_env = dict(os.environ, PYTHON3_BIN_PATH=sys.executable)

    if is_native_windows_or_msys():
        SHELL = bazel_env.get("SHELL")
        if SHELL:
            bazel_env.setdefault("BAZEL_SH", os.path.normpath(SHELL))
        BAZEL_SH = bazel_env["BAZEL_SH"]
        SYSTEMROOT = os.getenv("SystemRoot")
        wsl_bash = os.path.join(SYSTEMROOT, "System32", "bash.exe")
        if (not BAZEL_SH) and SYSTEMROOT and os.path.isfile(wsl_bash):
            msg = ("You appear to have Bash from WSL,"
                   " which Bazel may invoke unexpectedly. "
                   "To avoid potential problems,"
                   " please explicitly set the {name!r}"
                   " environment variable for Bazel.").format(name="BAZEL_SH")
            raise RuntimeError(msg)

    # Check if the current Python already has pickle5 (either comes with newer
    # Python versions, or has been installed by us before).
    pickle5 = None
    if sys.version_info >= (3, 8, 2):
        import pickle as pickle5
    else:
        try:
            import pickle5
        except ImportError:
            pass
    if not pickle5:
        download_pickle5(os.path.join(ROOT_DIR, PICKLE5_SUBDIR))

    # Note: We are passing in sys.executable so that we use the same
    # version of Python to build packages inside the build.sh script. Note
    # that certain flags will not be passed along such as --user or sudo.
    # TODO(rkn): Fix this.
    if not os.getenv("SKIP_THIRDPARTY_INSTALL"):
        pip_packages = ["psutil", "setproctitle"]
        subprocess.check_call([
            sys.executable, "-m", "pip", "install", "-q",
            "--target=" + os.path.join(ROOT_DIR, THIRDPARTY_SUBDIR)
        ] + pip_packages,
                              env=dict(os.environ, CC="gcc"))

    version_info = bazel_invoke(subprocess.check_output, ["--version"])
    bazel_version_str = version_info.rstrip().decode("utf-8").split(" ", 1)[1]
    bazel_version_split = bazel_version_str.split(".")
    bazel_version_digits = [
        "".join(takewhile(str.isdigit, s)) for s in bazel_version_split
    ]
    bazel_version = tuple(map(int, bazel_version_digits))
    if bazel_version < SUPPORTED_BAZEL:
        logger.warning("Expected Bazel version {} but found {}".format(
            ".".join(map(str, SUPPORTED_BAZEL)), bazel_version_str))

    bazel_targets = []
    bazel_targets += ["//:ray_pkg"] if build_python else []
    bazel_targets += ["//java:ray_java_pkg"] if build_java else []
    return bazel_invoke(subprocess.check_call,
                        ["build", "--verbose_failures", "--"] + bazel_targets,
                        env=bazel_env)
Ejemplo n.º 50
0
#34
#5

#repeat()
#    repeat(object[, times]) 重复times次;
itertools.repeat(10, 3)  #--> 10 10 10

#dropwhile()
#    dropwhile(func, seq );当函数f执行返回假时, 开始迭代序列

itertools.dropwhile(lambda x: x < 5, [1, 4, 6, 4, 1])  #--> 6 4 1

#takewhile()
#    takewhile(predicate, iterable);返回序列,当predicate为true是截止。

itertools.takewhile(lambda x: x < 5, [1, 4, 6, 4, 1])  #--> 1 4

#islice()
#    islice(seq[, start], stop[, step]);返回序列seq的从start开始到stop结束的步长为step的元素的迭代器
for i in itertools.islice("abcdef", 0, 4, 2):  #a, c
    print(i)

#product()
#    product(iter1,iter2, ... iterN, [repeat=1]);创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数
itertools.product('ABCD', 'xy')  #--> Ax Ay Bx By Cx Cy Dx Dy
itertools.product(range(2), repeat=3)  #--> 000 001 010 011 100 101 110 111
for i in itertools.product([1, 2, 3], [4, 5], [6, 7]):
    print(i)
#(1, 4, 6)
#(1, 4, 7)
#(1, 5, 6)
Ejemplo n.º 51
0
import itertools


def fib():
    p1, p2 = 1, 0
    while True:
        yield p1
        p1, p2 = p1 + p2, p1


print('take first 10 elements:')
print(list(itertools.islice(fib(), 10)))

print('take first 10 even elements:')
print(list(itertools.islice(filter(lambda x: x % 2 == 0, fib()), 10)))

print('take elements less than 40:')
print(list(itertools.takewhile(lambda x: x < 40, fib())))

print('take elements > 20 and < 70:')
print(
    list(
        itertools.takewhile(lambda x: x < 70,
                            itertools.dropwhile(lambda x: x <= 20, fib()))))
Ejemplo n.º 52
0
 def is_ready(self):
     finished = list(
         itertools.takewhile(lambda i: i.is_ready(),
                             self.unfinished_children))
     self.unfinished_children.difference_update(finished)
     return not self.unfinished_children
Ejemplo n.º 53
0
def strtoll(val, base=10):
    """Mimics libc's strtoll function"""
    return int("".join(takewhile(str.isdigit, val)), base)
# accumulate()
# ==============================================================

# Example 1 : Make a list using accumulate and range
nums = list(accumulate(range(8)))
# Produces list 0,1,3,6,10,16,21,28
# (0),(0 + 1), (0 + 1 + 2) etc

# ==============================================================
# takewhile()
# ==============================================================

# Example 1 : Using Lambda + takewhile to return items in list until
# the rule is broken.
nums = [0, 2, 4, 6, 8, 10, 2, 3, 4, 5]
print(list(takewhile(lambda x: x <= 6, nums)))
# prints out 0,2,4,6 (ignores the items at the end because the rule
# failed on '8')

# ==============================================================
# chain()
# ==============================================================

# Example 1 : Using chain to join two lists
list(chain([1, 3, 2], [3, 5, 9]))
# produces a single list of 1,3,2,3,5,9

# ==============================================================
# product()
# ==============================================================
Ejemplo n.º 55
0
def _take_to_end(deq, start):
    a = reversed(deq)
    return reversed(tuple(itertools.takewhile(lambda y: y > start, a)))
def generate_whole_story(session,
                         conf,
                         data_generator,
                         placeholders,
                         ops,
                         sample=False,
                         first_sentences=None,
                         sample_from=10):
    eos_tok_int = data_generator.vocab.string_to_int(EOS_TOK)
    eod_tok_int = data_generator.vocab.string_to_int(EOD_TOK)
    unknown_int = data_generator.vocab.string_to_int(UNKNOWN_TOK)
    input_seq, choices, bow_vectors = placeholders
    stories = []
    n_stories = data_generator.n_stories
    for i in range(conf.batch_size):
        if first_sentences is None:
            story_filename = data_generator.story_files[np.random.randint(
                0, n_stories)]
            first_sentence = load_story(story_filename)[0]
        else:
            first_sentence = first_sentences[i]
        encoded_first_sentence = encode_sentence(first_sentence,
                                                 data_generator.vocab)
        stories.append([encoded_first_sentence])
    bow = [np.zeros(len(data_generator.vocab)) for _ in range(conf.batch_size)]
    normalized_bow = [
        np.zeros(len(data_generator.vocab)) for _ in range(conf.batch_size)
    ]
    sl = 0
    eod = [False for _ in range(conf.batch_size)]
    while sl < 100 and (sl < 3 or (not np.all(eod))):
        sl += 1
        for i in range(conf.batch_size):
            bow[i] += bag_of_words(bow[i], stories[i][-1],
                                   data_generator.use_in_bow)
            normalized_bow[i] = normalize_bow(bow[i])
        x = [
            create_context(story, data_generator.vocab, conf.input_length)
            for story in stories
        ]
        session.run(ops[0:2], {input_seq.name: x, bow_vectors.name: bow})
        preds = []
        for i in range(conf.output_length):
            if sample is None:
                sample = bool(np.random.randint(2))
            w = generate_next_word(session,
                                   ops,
                                   choices,
                                   sample,
                                   unknown_int=unknown_int,
                                   exclude_padding=(i < 3),
                                   n_best=sample_from)
            preds.append(w)
        y_pred = np.vstack(preds).transpose()
        for i in range(conf.batch_size):
            if eod[i] and sl > 3:
                next_sentence = []
            else:
                next_sentence = list(
                    takewhile(lambda c: c != eos_tok_int, y_pred[i]))
                if eod_tok_int in y_pred[i]:
                    eod[i] = True
            if len(next_sentence) < len(y_pred[i]):
                next_sentence.append(eos_tok_int)
            stories[i].append(next_sentence)

    decoded_stories = []
    for i, story in enumerate(stories):
        story_text = ''
        for sentence in story:
            decoded_sentence = data_generator.decode_data(sentence,
                                                          skip_specials=True)
            if decoded_sentence.strip() is not '':
                story_text += decoded_sentence
                story_text += '\n'
        decoded_stories.append(story_text)
    return decoded_stories
Ejemplo n.º 57
0
def floatRange(start, stop, step):
    gen = takewhile(lambda x: x < stop, count(start, step))
    return list(gen)
Ejemplo n.º 58
0
    def _get_topN_users(self, n):
        """Get the top N users for a defined challenge

        Ranking criterias:
            1. succeed every goal of the challenge
            2. total completeness of each goal (can be over 100)

        Only users having reached every goal of the challenge will be returned
        unless the challenge ``reward_failure`` is set, in which case any user
        may be considered.

        :returns: an iterable of exactly N records, either User objects or
                  False if there was no user for the rank. There can be no
                  False between two users (if users[k] = False then
                  users[k+1] = False
        """
        Goals = self.env['gamification.goal']
        (start_date,
         end_date) = start_end_date_for_period(self.period, self.start_date,
                                               self.end_date)
        challengers = []
        for user in self.user_ids:
            all_reached = True
            total_completeness = 0
            # every goal of the user for the running period
            goal_ids = Goals.search([('challenge_id', '=', self.id),
                                     ('user_id', '=', user.id),
                                     ('start_date', '=', start_date),
                                     ('end_date', '=', end_date)])
            for goal in goal_ids:
                if goal.state != 'reached':
                    all_reached = False
                if goal.definition_condition == 'higher':
                    # can be over 100
                    total_completeness += 100.0 * goal.current / goal.target_goal
                elif goal.state == 'reached':
                    # for lower goals, can not get percentage so 0 or 100
                    total_completeness += 100

            challengers.append({
                'user': user,
                'all_reached': all_reached,
                'total_completeness': total_completeness
            })

        challengers.sort(key=lambda k:
                         (k['all_reached'], k['total_completeness']),
                         reverse=True)
        if not self.reward_failure:
            # only keep the fully successful challengers at the front, could
            # probably use filter since the successful ones are at the front
            challengers = itertools.takewhile(lambda c: c['all_reached'],
                                              challengers)

        # append a tail of False, then keep the first N
        challengers = itertools.islice(
            itertools.chain(
                (c['user'] for c in challengers),
                itertools.repeat(False),
            ), 0, n)

        return tuple(challengers)
Ejemplo n.º 59
0
    def _respond(self, request):
        head_block = self._get_head_block(request)
        self._validate_ids(request.block_ids)
        blocks = None
        paging_response = None

        if request.block_ids:
            blocks = self._block_store.get_blocks(request.block_ids)
            blocks = itertools.filterfalse(
                lambda block: block.block_num > head_block.block_num, blocks)

            # realize the iterator
            blocks = list(map(lambda blkw: blkw.block, blocks))

            paging_response = client_list_control_pb2.ClientPagingResponse()
        else:
            paging = request.paging
            sort_reverse = BlockListRequest.is_reverse(
                request.sorting, self._status.INVALID_SORT)
            limit = min(paging.limit, MAX_PAGE_SIZE) or DEFAULT_PAGE_SIZE
            iterargs = {'reverse': not sort_reverse}

            if paging.start:
                iterargs['start_block_num'] = paging.start
            elif not sort_reverse:
                iterargs['start_block'] = head_block

            block_iter = None
            try:
                block_iter = self._block_store.get_block_iter(**iterargs)
                blocks = block_iter
                if sort_reverse:
                    blocks = itertools.takewhile(
                        lambda block: block.block_num <= head_block.block_num,
                        blocks)

                blocks = itertools.islice(blocks, limit)
                # realize the result list, which will evaluate the underlying
                # iterator
                blocks = list(map(lambda blkw: blkw.block, blocks))

                next_block = next(block_iter, None)
                if next_block:
                    next_block_num = BlockStore.block_num_to_hex(
                        next_block.block_num)
                else:
                    next_block_num = None

                start = self._block_store.get(
                    blocks[0].header_signature).block_num
            except ValueError:
                if paging.start:
                    return self._status.INVALID_PAGING

                return self._status.NO_ROOT

            paging_response = client_list_control_pb2.ClientPagingResponse(
                next=next_block_num,
                limit=limit,
                start=BlockStore.block_num_to_hex(start))

        if not blocks:
            return self._wrap_response(self._status.NO_RESOURCE,
                                       head_id=head_block.identifier,
                                       paging=paging_response)

        return self._wrap_response(head_id=head_block.identifier,
                                   paging=paging_response,
                                   blocks=blocks)
Ejemplo n.º 60
0
from itertools import takewhile

print(list(takewhile(lambda x: x < 5, [1, 4, 6, 4, 1])))