Exemple #1
0
 def test_zip_longest(self):
     res = zip('ABCDEF', 'xyz')
     self.assertEqual([('A', 'x'), ('B', 'y'), ('C', 'z')], list(res))
     res = zip_longest('ABCDEF', 'xyz')
     self.assertEqual([('A', 'x'), ('B', 'y'), ('C', 'z'), ('D', None), ('E', None), ('F', None)], list(res))
     res = zip_longest('ABCDEF', 'xyz', fillvalue='?')
     self.assertEqual([('A', 'x'), ('B', 'y'), ('C', 'z'), ('D', '?'), ('E', '?'), ('F', '?')], list(res))
Exemple #2
0
    def createBytes(buffer, rsBlocks):
        offset = 0
        maxDcCount = 0
        maxEcCount = 0
        totalCodeCount = 0
        dcdata = []
        ecdata = []
        for block in rsBlocks:
            totalCodeCount += block.totalCount
            dcCount = block.dataCount
            ecCount = block.totalCount - dcCount
            maxDcCount = max(maxDcCount, dcCount)
            maxEcCount = max(maxEcCount, ecCount)
            dcdata.append(buffer.buffer[offset:offset+dcCount])
            offset += dcCount
            rsPoly = QRUtil.getErrorCorrectPolynomial(ecCount)
            rawPoly = QRPolynomial(dcdata[-1], rsPoly.getLength() - 1)
            modPoly = rawPoly.mod(rsPoly)
            rLen = rsPoly.getLength() - 1
            mLen = modPoly.getLength()
            ecdata.append([ (modPoly.get(i) if i >= 0 else 0)
                          for i in range(mLen - rLen, mLen) ])

        data = [ d for dd in itertools.chain(
                zip_longest(*dcdata), zip_longest(*ecdata))
                 for d in dd if d is not None]
        return data
    def add_multiples(l: list) -> list:
        from itertools import zip_longest

        if len(l) > 1:
            l = [x for x in list(
                NumberGenerator.chain(
                    *zip_longest(
                        l,
                        reversed(
                            NumberGenerator.get_multiples(
                                1000,
                                len(l)
                            )
                        )
                    )
                )
            ) if x is not None]
        for (k, j) in enumerate(l):
            if isinstance(j, list):
                l[k] = [x for x in list(
                    NumberGenerator.chain(
                        *zip_longest(
                            l[k],
                            reversed(
                                NumberGenerator.get_multiples(
                                    10,
                                    len(l[k])
                                )
                            )
                        )
                    )
                ) if x is not None]
        return l
Exemple #4
0
def compareFiles(f1, f2, compares = [lambda a,b: b-a]):
	"""Compares the numbers in two files using one of several comparison methods."""
	#regex that matches a number
	np = re.compile(r'((\d+(\.\d*)?)|(\.\d+)([eE][+-]?\d+)?)')
	
	total = [0 for i in compares]
	count = 0
	
	for stra, strb in zip_longest(f1, f2):
		if stra == None:
			print(strb)
		elif strb == None:
			print(stra)
		else:
			itera, iterb = np.finditer(stra), np.finditer(strb)	
			for toka, tokb in zip_longest(itera, iterb):
				if toka == None:
					print(tokb.group(), end=' ')
				elif tokb == None:
					print(toka.group(), end=' ')
				else:
					print('<', end='')
					a, b = toka.group(), tokb.group()
					vals = [comp(float(a), float(b)) for comp in compares]
					for i in range(0,len(vals)):
						total[i] += vals[i]
					print(*["{0:.4}".format(val) for val in vals],sep='|', end='')
					count += 1
					print('> ', end='')
			print()
	averages = [t/count for t in total]
	print('---------\naverages:')
	print(*averages, sep=' | ')
Exemple #5
0
    def _extract_function_signature(self, node):
        for node in ast.walk(node):
            if isinstance(node, ast.arguments):
                # Extract positional arguments and possible default values.
                args = [arg.arg for arg in node.args]
                args_defaults = self._get_function_defaults(node)
                # Extract kwonlyargs and defaults.
                kwonlyargs = [arg.arg for arg in node.kwonlyargs]
                kwonlyargs_defaults = self._get_function_defaults(node, kwonlyargs=True)

                # Combine arguments and their corresponding default values,
                # if no default value exists fill it with a NoDefault object.
                args_plus_defaults = list(zip_longest(reversed(args),
                                                      reversed(args_defaults),
                                                      fillvalue=NoDefault()))
                kwonlyargs_plus_defaults = list(zip_longest(reversed(kwonlyargs),
                                                            reversed(kwonlyargs_defaults),
                                                            fillvalue=NoDefault()))

                vararg = node.vararg.arg if node.vararg is not None else None
                kwarg = node.kwarg.arg if node.kwarg is not None else None

                return FunctionArguments(
                    args=args_plus_defaults,
                    vararg=vararg,
                    kwarg=kwarg,
                    kwonlyargs=kwonlyargs_plus_defaults
                )
Exemple #6
0
    def test_tpid_eval_full_scan(self):
        """
        Test the full scan

        @return:
        @rtype:
        """
        data = self.data_csv
        steps = self.steps_csv

        res_wb = tpidmany.tpid_eval_full_scan(data, steps, 7)
        self.xl_need_closing.append(res_wb.Parent)

        xl, wb, ws, cells = xlObjs(self.exp_full_result, visible=False)
        self.xl_need_closing.append(xl)

        result = res_wb.Worksheets(1).UsedRange.Value2
        expected = wb.Worksheets(1).UsedRange.Value2

        # Include excel cell address of any mismatch
        # start = 1 for 1 based index
        addr = cellStr
        for r, (exp_row, result_row) in enumerate(zip_longest(result, expected), 1):
            for c, (exp_cell, result_cell) in enumerate(zip_longest(exp_row, result_row), 1):
                self.assertEqual(exp_cell, result_cell, msg=addr(r, c))
def prepare_data(text_array, num_chars):
    """
    Process inputs, mask, and outputs for each text array
    :param text_array: Array of arrays in which each subarray is an array of character indices
    :param num_chars: Number of characters to represent
    :return: (inputs (n_chars-1, n_text_arrays, n_unique_chars), mask (n_chars-1, n_text_arrays), outputs (n_chars-1, n_text_arrays))
    """

    x = []
    y = []

    for sequence in text_array:
        x.append(sequence[:-1])
        y.append(sequence[1:])

    # x_idxs has dimensionality (n_chars-1, n_text_arrays)
    x_idxs = np.array(list(itertools.zip_longest(*x, fillvalue=-1)))
    mask = x_idxs >= 0
    y = np.array(list(itertools.zip_longest(*y, fillvalue=-1)))

    x = np.zeros(x_idxs.shape + (num_chars,), dtype=theano.config.floatX)
    # TODO: use indexing='ij' for this
    idx1, idx0 = np.meshgrid(np.arange(x_idxs.shape[1]), np.arange(x_idxs.shape[0]))
    x[idx0, idx1, x_idxs] = 1

    return x, mask, y
Exemple #8
0
  def eval_ReplacementList(self, cPPAST):
    # This means: return the replacement with macros replaced.
    # e.g. #define bar 2
    #      #define var 1 bar 3
    # eval( ReplacementList([1, bar, 3]) ) = ReplacementList([1, 2, 3])
    # input and output tokens are ctokens.
  
    def make_cToken(token):
      if token.type == 'c':
        return copy(token)
      (string, terminalId, function) = self.cLS.match(token.source_string, True)
      #print(token.source_string, terminalId, token.resource, token.lineno, token.colno)
      #newId = self.cPPTtocT[token.id]
      return cToken( terminalId, token.resource, c_Parser.terminals[terminalId], string, token.lineno, token.colno, None )

    tokens = list(map(make_cToken, cPPAST.getAttr('tokens')))
    rtokens = []
    newTokens = []

    tokensWithLookahead = zip_longest(tokens, tokens[1:])
    for token, lookahead in tokensWithLookahead:
      if token.id == c_Parser.TERMINAL_IDENTIFIER and token.getString() in self.symbols:
        replacement = self.symbols[token.getString()]
        if isinstance(replacement, self.cPFF.cPreprocessorFunction):
          if lookahead and lookahead.id == c_Parser.TERMINAL_LPAREN:
            params = self._getMacroFunctionParams(tokensWithLookahead)
            result = replacement.run(params, token.lineno, token.colno)
            newTokens.extend(result)
            continue
          elif lookahead:
            newTokens.append(token)
            continue
          else:
            # TODO: can we get rid of this?  seems like a relic...
            newTokens.append(replacement)
            continue
        elif replacement is None:
          continue
        elif isinstance(replacement, list):
          tmp = []
          for (rtoken, next_rtoken) in zip_longest(replacement, replacement[1:]):
            if not next_rtoken:
              if isinstance(rtoken, self.cPFF.cPreprocessorFunction) and lookahead.id == c_Parser.TERMINAL_LPAREN:
                 params = self._getMacroFunctionParams(tokensWithLookahead)
                 result = rtoken.run(params, token.lineno, token.colno)
                 tmp.extend(result)
                 break
            new_token = copy(rtoken)
            new_token.colno = token.colno
            new_token.lineno = token.lineno
            if new_token.id == pp_Parser.TERMINAL_PP_NUMBER:
              new_token.id = c_Parser.TERMINAL_INTEGER_CONSTANT
            tmp.append(new_token)
          newTokens.extend(tmp)
          continue
        else:
          raise Exception('unknown macro replacement type', replacement)
      else:
        newTokens.append(token)
    return newTokens
def UTF8_encode1(c):
    """implements  chr.encode() e.g.: "A".encode(encoding="UTF-8")"""
    character_number = ord(c)
    if character_number in range(0xd800,0xe000):
        raise UnicodeEncodeError("Cannot encode a surrogate (a character number in range(0xd800,0xe000))")


# Note: adapted from From rfc3629, page 4

#   cnbl < 8     0000 0000-0000 007F | 0xxxxxxx
#   cnbl > 7     0000 0080-0000 07FF | 110xxxxx 10xxxxxx
#   cnbl > 11    0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
#   cnbl > 16    0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx

    if character_number < 128:  # cnbl < 8
        return bytes([character_number])

    cnbl = character_number.bit_length()
    byte_length = 1 + (cnbl > 7) + (cnbl > 11) + (cnbl > 16)
    utf8_hdr = ((0xc0,0xe0,0xf0)[byte_length-2],)

    base64cn = int2baseb(character_number,64) # 10xx xxxx

    if len(base64cn) < byte_length:
        base64cn = (0,)+base64cn
    assert len(base64cn) == byte_length

    print([bin(byt) for byt in base64cn])
    print([bin(byt) for byt in bytes([b|a for a,b in zip_longest(base64cn,utf8_hdr,fillvalue=0x80)])])
    return bytes([b|a for a,b in zip_longest(base64cn,utf8_hdr,fillvalue=0x80)])
Exemple #10
0
 def store_full_hashes(self, hash_prefix, hashes):
     "Store hashes found for the given hash prefix"
     log.debug("storing full hashes for %s", binascii.hexlify(hash_prefix).decode('ascii'))
     log.log(TRACE, 'storing prefix %s with hashes %s', hash_prefix, hashes)
     self.cleanup_expired_hashes()
     cache_lifetime = hashes['cache_lifetime']
     for hash_info, metadata_info in itertools.zip_longest(list(hashes['hashes'].items()), list(hashes['metadata'].items())):
         list_name_bytes = hash_info[0]
         #  log.debug("list name bytes is: {list_name_bytes}".format(list_name_bytes=list_name_bytes))
         list_name = list_name_bytes.decode("ascii")  # convert the bytestring listname to a 'normal' python string
         log.log(TRACE, 'storing full hashes for list name %s', list_name)
         hash_values = hash_info[1]
         metadata_values = metadata_info[1]
         for hash_value, metadata_value in itertools.zip_longest(hash_values, metadata_values):
             if metadata_value is None:
                 metadata_value = 0
             log.log(TRACE, "metadata value is: %s", metadata_value)
             log.debug("storing hash '%s'", binascii.hexlify(hash_value).decode("ascii"))
             q = "INSERT INTO full_hash (value, list_name, metadata, downloaded_at, expires_at)\
                 VALUES (?, ?, ?, current_timestamp, datetime(current_timestamp, '+%d SECONDS'))"
             q = q % cache_lifetime
             self.dbc.execute(q, [sqlite3.Binary(hash_value), list_name, metadata_value])
     q = "UPDATE hash_prefix SET full_hash_expires_at=datetime(current_timestamp, '+%d SECONDS') \
         WHERE chunk_type='add' AND value=?"
     self.dbc.execute(q % cache_lifetime, [sqlite3.Binary(hash_prefix)])
     self.db.commit()
Exemple #11
0
def mycheck_fr(project_id):
    """Check for french documents only"""

    # Validate input
    project_dir = check_project_id(project_id)
    if project_dir is None:
        abort(404)

    files_dir = os.path.join(project_dir, "files")
    filename = secure_filename(request.args.get('file', ''))
    f1 = os.path.join(files_dir, filename)
    if not os.path.isfile(f1):
        abort(404)

    ancienne_orthographe, nouvelle_orthographe, mots_ens, mots_ents, mots_ens_ents, mots_ans, mots_ants, mots_ans_ants = check_fr(f1)

    furtif = Furtif()
    furtif = Furtif()
    furtif.load_config()
    furtif.check_furtif(f1, '<span class="furtif-check">', '</span>', to_html=True)

    return render_template('check_fr.tmpl',
                           project_id=project_id,
                           filename=filename,
                           orthographe=zip_longest(ancienne_orthographe, nouvelle_orthographe, fillvalue=""),
                           mots_ens=zip_longest(mots_ens, mots_ents, mots_ens_ents, fillvalue=""),
                           mots_ans=zip_longest(mots_ans, mots_ants, mots_ans_ants, fillvalue=""),
                           furtif=furtif)
Exemple #12
0
def type(session, type_id):
    resp = session.query(Type).filter(Type.id == type_id).first()
    ret = {'ty': resp}
    if resp is not None:
        ret['relations_to'] = list(itertools.zip_longest(resp.double_damage_to, resp.half_damage_to, resp.no_damage_to))
        ret['relations_from'] = list(
            itertools.zip_longest(resp.double_damage_from, resp.half_damage_from, resp.no_damage_from))
    return ret if resp else None
Exemple #13
0
 def _wrap_zip(self, value):
     """
     Wraps the value in a polynomial with same degree.
     """
     if isinstance(value, Polynomial):
         return zip_longest(self.coefs, value.coefs, fillvalue=0)
     else:
         return zip_longest(self.coefs, [value], fillvalue=0)
Exemple #14
0
def diff_designs(design1, design2):
    """
    Compare two cadnano (v1) objects (after loading from json files).
    """
    if design1['name'] != design2['name']:
        print("New name:", design1['name'], "->", design2['name'])
    if design1['vstrands'] == design2['vstrands']:
        print("The two designs share the exact same vstrands.")
        return
    old_vstrands = design1['vstrands']
    old_vstrands_tups = list_to_tups(old_vstrands)
    old_vstrands_set = set(old_vstrands_tups)
    new_vstrands = design2['vstrands']
    new_vstrands_tups = list_to_tups(new_vstrands)
    new_vstrands_set = set(new_vstrands_tups)
    # set arithmatics: & = intersection = common elements; | = union;
    # s - t = set with elements in s but not in t; s ^ t = new set with elements in either s or t but not both
    vstrands_common = new_vstrands_set & old_vstrands_set
    vstrands_added = new_vstrands_set - old_vstrands_set
    vstrands_removed = old_vstrands_set - new_vstrands_set
    changed_mask = [new != old for new, old in zip_longest(new_vstrands_tups, old_vstrands_tups)]
    # changed_mask[i] is True if old_vstrands[i] is different from new_vstrands[i]
    n_changed = sum(changed_mask)
    if n_changed == 0:
        print("No pair-wise changes in vstrands.")
        return
    # There should be some threshold depending on n_changed.
    # If e.g. a vstrand has been inserted early, do not do pair-wise comparison.
    #for vstrand in old_vstrands_tups:
    print(n_changed, "pair-wise changes in vstrands.")
    print(len(vstrands_added), "vstrands added")
    print(len(vstrands_removed), "vstrands removed")
    print(len(vstrands_common), "vstrands in common (although could be shuffled around)")
    print("Pairwise changes:")
    for i, (new_vs, old_vs) in enumerate(zip_longest(new_vstrands_tups, old_vstrands_tups)):
        if new_vs == old_vs:
            continue
        if old_vs is None or new_vs is None:
            print("extended vstrand", i, ("new" if old_vs is None else "old"), "vstrand added.")
            continue
        if old_vs in vstrands_common:
            old_vstrands_tups.index(new_vstrands_tups[0])
            print("old vstrand", i, "moved to", new_vstrands_tups.index(old_vs), "(but is otherwise identical)")
            continue
        old_num = old_vstrands[i].get('num')
        print("Changes for vstrand %s (index %s):" % (old_num, i))
        for (oldk, oldv), (newk, newv) in zip(old_vs, new_vs):
            if not oldk == newk:
                print("** vstrand %s change in keys '%s' != '%s' - this should not happen!!" % (i, oldk, newk))
                continue
            if newv == oldv:
                continue
            else:
                pw_changes = [new_elem != old_elem for new_elem, old_elem in zip(oldv, newv)]
                changed_idx = [i for i, val in enumerate(pw_changes) if val is True]
                print("- '%s': %s pairwise changes - idx %s" % \
                      (oldk, sum(pw_changes),
                       changed_idx if len(changed_idx) < 5 else "%s,...,%s" % (changed_idx[0], changed_idx[-1])))
Exemple #15
0
def tabulate_data( allele_df, dbh ):

    buf = []
    buf2 = []
    buf3 = []

    table = pivot_table( allele_df,
                            index='sample_id', columns='marker_id', values='value',
                            aggfunc = lambda x: tuple(x) )

    heights = pivot_table( allele_df,
                            index='sample_id', columns='marker_id', values='height',
                            aggfunc = lambda x: tuple(x) )

    assay_ids = pivot_table( allele_df,
                            index='sample_id', columns='marker_id', values='assay_id',
                            aggfunc = lambda x: tuple(x) )

    buf.append( tuple( ['Sample', 'ID'] +
                    [ dbh.get_marker_by_id(x).code for x in table.columns ] ) )
    buf2.append( tuple( ['Sample', 'ID'] +
                    [ dbh.get_marker_by_id(x).code for x in heights.columns ] ) )
    buf3.append( tuple( ['Sample', 'ID'] +
                    [ dbh.get_marker_by_id(x).code for x in assay_ids.columns ] ) )


    empty = tuple()

    rows = [ ((dbh.get_sample_by_id(r[0]).code,), (r[0],)) + r[1:]
                for r in table.itertuples() ]
    rows.sort()

    height_rows = [ ((dbh.get_sample_by_id(r[0]).code,), (r[0],)) + r[1:]
                                for r in heights.itertuples() ]
    height_rows.sort()

    assayid_rows = [ ((dbh.get_sample_by_id(r[0]).code,), (r[0],)) + r[1:]
                                for r in assay_ids.itertuples() ]
    assayid_rows.sort()

    for row in rows:
        data = [ x if type(x) == tuple else empty for x in row ]
        for cols in zip_longest( *data, fillvalue='' ):
            buf.append( cols )

    for height_row in height_rows:
        data = [ x if type(x) == tuple else empty for x in height_row ]
        for cols in zip_longest( *data, fillvalue='' ):
            buf2.append( cols )

    for assayid_row in assayid_rows:
        data = [ x if type(x) == tuple else empty for x in assayid_row ]
        for cols in zip_longest( *data, fillvalue='' ):
            buf3.append( cols )


    return (buf, buf2, buf3)
Exemple #16
0
def prepare_data( allele_df ):

    #buf = io.StringIO()

    #temp_csv = csv.writer( buf, delimiter='\t' )
    buf = []
    buf2 = []
    buf3 = []

    table = pivot_table( allele_df, rows='sample_id', cols='marker_id', values='value',
                            aggfunc = lambda x: tuple(x) )

    heights = pivot_table( allele_df, rows='sample_id', cols='marker_id', values='height',
                            aggfunc = lambda x: tuple(x) )

    assay_ids = pivot_table( allele_df, rows='sample_id', cols='marker_id', values='assay_id',
                            aggfunc = lambda x: tuple(x) )

    buf.append( tuple( ['Sample'] + 
                    [ Marker.get(x).code for x in table.columns ] ) )
    buf2.append( tuple( ['Sample'] + 
                    [ Marker.get(x).code for x in heights.columns ] ) )
    buf3.append( tuple( ['Sample'] +
                    [ Marker.get(x).code for x in assay_ids.columns ] ) )


    empty = tuple()

    rows = [ (((Sample.get(r[0]).code, r[0]),),) + r[1:] for r in table.itertuples() ]
    rows.sort()

    height_rows = [ (((Sample.get(r[0]).code, r[0]),),) + r[1:]
                                for r in heights.itertuples() ]
    height_rows.sort()

    assayid_rows = [ (((Sample.get(r[0]).code, r[0]),),) + r[1:]
                                for r in assay_ids.itertuples() ]
    assayid_rows.sort()

    for row in rows:
        data = [ x if type(x) == tuple else empty for x in row ]
        for cols in zip_longest( *data, fillvalue='' ):
            buf.append( cols )

    for height_row in height_rows:
        data = [ x if type(x) == tuple else empty for x in height_row ]
        for cols in zip_longest( *data, fillvalue='' ):
            buf2.append( cols )

    for assayid_row in assayid_rows:
        data = [ x if type(x) == tuple else empty for x in assayid_row ]
        for cols in zip_longest( *data, fillvalue='' ):
            buf3.append( cols )


    return (buf, buf2, buf3)
Exemple #17
0
    def testinit(self):
        l = SortedList([5,9,2,3,6])

        for j, k in itertools.zip_longest(l, [2,3,5,6,9]):
            self.assertEqual(j, k)

        l = SortedList([5,9,2,3,6], keyfunc=lambda j: -j)

        for j, k in itertools.zip_longest(l, [9,6,5,3,2]):
            self.assertEqual(j, k)
Exemple #18
0
	def show(self, screen=None): 
		if screen is None: 
			screen = self.screen
		startLine = self.startLine
		for ubLine, ubAreaLine in zip_longest(self.window.ubLine_list[startLine: startLine+4], self, fillvalue=[None]*8): 
			for ub, ubArea in zip_longest(ubLine, ubAreaLine): 
				ubArea.ub = ub
				ubArea.show(screen)

		self.scrollBar.show(screen)
Exemple #19
0
	def show(self, screen=None): 
		if screen is None: 
			screen = self.screen
		startLine = self.startLine

		for itemLine, itemAreaLine in zip_longest(self.itemLine_list[startLine: startLine+6], self, fillvalue=[None]*6): 
			for item, itemArea in zip_longest(itemLine, itemAreaLine): 
				itemArea.item = item
				itemArea.show(screen)

		self.scrollBar.show(screen)
Exemple #20
0
 def __repr__(self):
     id_counts = pretty_print_counts(self.coverage, 'Id_count')
     prim_counts = pretty_print_counts(self.coverage, 'Primer_count')
     count_acc = []
     for line in zip_longest(prim_counts, id_counts, fillvalue=""):
         count_acc.append("".join(line))
     clusters = pretty_print_clusters(self.coverage)
     join_acc = []
     for line in zip_longest(clusters, count_acc, fillvalue="{}".format(54 * " ")):
         join_acc.append("".join(line))
     joined = "\n".join(join_acc)
     return joined
Exemple #21
0
    def show(self, screen=None):
        if screen is None:
            screen = self.screen
        startLine = self.startLine
        for unitLine, unitAreaLine in zip_longest(
            self.itemLine_list[startLine : startLine + 6], self, fillvalue=[None] * 8
        ):
            for unit, unitArea in zip_longest(unitLine, unitAreaLine):
                unitArea.unit = unit
                unitArea.show(screen)

        self.scrollBar.show(screen)
Exemple #22
0
def recurse_fxy(l1, l2, f, level):
    res = []
    res_append = res.append
    # will only be used if lists are of unequal length
    fl = l2[-1] if len(l1) > len(l2) else l1[-1]
    if level == 1:
        for u, v in zip_longest(l1, l2, fillvalue=fl):
            res_append(f(u, v))
    else:
        for u, v in zip_longest(l1, l2, fillvalue=fl):
            res_append(recurse_fxy(u, v, f, level-1))
    return res
def grouper(iterable, n, fillvalue=None, shorten=False, num_groups=None):
    args = [iter(iterable)] * n
    out = zip_longest(*args, fillvalue=fillvalue)
    out = list(out)
    if num_groups is not None:
        default = (fillvalue, ) * n
        assert isinstance(num_groups, int)
        out = list(each for each, _ in zip_longest(out, range(num_groups), fillvalue=default))
    if shorten:
        assert fillvalue is None
        out = (tuple(e for e in each if e is not None) for each in out)
    return out
def main():
    inp = open("input.txt").read().splitlines()

    # readable
    for line in list(zip_longest(*inp[1:])):
        print("".join([char if char is not None else " " for char in line]))

    # slightly less readable
    list(map(print_line, list(zip_longest(*inp[1:]))))

    #even less readable
    list(map(lambda line : print("".join([char if char is not None else " " for char in line])), list(zip_longest(*open("input.txt").read().splitlines()[1:]))))
Exemple #25
0
def get_readunits_from_args(fqs1, fqs2):
    """Turn fastq arguments into fake ReadUnits
    """

    if fqs1:
        assert isinstance(fqs1, list)

    if fqs2:
        assert isinstance(fqs2, list)
        paired = True
    else:
        fqs2 = len(fqs1)*[None]
        paired = False

    for f in fqs1 + fqs2:
        if f and not os.path.exists(f):
            logger.fatal("Non-existing input file %s", f)
            raise ValueError(f)

    if paired:
        print_fq_sort_warning = False
        # sorting here should ensure R1 and R2 match
        fq_pairs = list(zip_longest(sorted(fqs1), sorted(fqs2)))
        fq_pairs_orig = set(zip_longest(fqs1, fqs2))
        for (fq1, fq2) in fq_pairs:
            if (fq1, fq2) not in fq_pairs_orig:
                print_fq_sort_warning = True
                break
        if print_fq_sort_warning:
            logger.warning("Are you sure paired-end reads are in correct order?")

    if len(fqs1) != len(set(fqs1)):
        logger.warning("Looks like the same files was given twice?")
        #logger.debug("len(fqs1)={} len(set(fqs1))={}".format(len(fqs1), len(set(fqs1))))
    if paired:
        if len(fqs2) != len(set(fqs2)):
            logger.warning("Looks like the same files was given twice?")
            #logger.debug("len(fqs2)={} len(set(fqs2))={}".format(len(fqs2), len(set(fqs2))))

    readunits = dict()
    fq_pairs = list(zip_longest(fqs1, fqs2))
    for (fq1, fq2) in fq_pairs:
        run_id = flowcell_id = library_id = lane_id = rg_id = None
        fq1 = os.path.abspath(fq1)
        if fq2 is not None:
            fq2 = os.path.abspath(fq2)
        ru = ReadUnit(run_id, flowcell_id, library_id, lane_id, rg_id,
                      fq1, fq2)
        ru = ru._replace(rg_id=create_rg_id_from_ru(ru))
        readunits[key_for_readunit(ru)] = dict(ru._asdict())

    return readunits
Exemple #26
0
def test_range_get():
    r = range(10)
    t = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    for v1, v2 in zip_longest(r, t):
        assert v1 == v2
    r = range(3, 10, 3)
    t = [3, 6, 9]
    for v1, v2 in zip_longest(r, t):
        assert v1 == v2
    with pytest.raises(IndexError):
        range(10)[10]
    with pytest.raises(IndexError):
        range(10)[-11]
Exemple #27
0
    def __init__(self, vexs=[], edges=[], weights=[]):
        assert(len(set(vexs)) == len(vexs))
        from itertools import zip_longest
        self._vertexs = [Vertex(value, []) for value in vexs]
        self._num_vertex = vexs.__len__
        self._num_edge = edges.__len__
        self._edges = [Edge(edge, weight, False) for edge, weight in zip_longest(edges,weights)]

        # weights should not longer than edges
        if len(weights) > len(edges):
            weights = weights[:len(edges)]
        for edge, weight in zip_longest(edges, weights, fillvalue=1):
            self.addEdge(edge, weight)
Exemple #28
0
    def __init__(self, data, headline=None, align='l', padding=2, floatprec=2,
                 truncate=True, header_padding=0, datetimefs='%Y-%m-%d %H:%M',
                 theme='simple'):
        # TODO: Are these attributes needed?
        self.align = align
        self.padding = padding
        self.floatprec = floatprec
        self.datetimefs = datetimefs
        self.theme = theme

        # Use a deque to be able to prepend the table header easily.
        data = deque(self._normalize(data))
        # Transpose data to get max column widths.
        # Take care of zip and zip_longest, see #8.
        if truncate:
            columns = list(zip(*data))
        else:
            columns = list(zip_longest(*data, fillvalue=''))
        widths = self._get_widths(columns)

        if headline:
            if header_padding:
                padding_str = ' ' * header_padding
                headline = ['{0}{1}{0}'.format(padding_str, col)
                            for col in headline]

            # Prepend the header to the table and update columns.
            header = self._get_header(headline, widths)
            # We have to use reversed; from docs.python.org:
            # The series of left appends results in reversing the
            # order of elements in the iterable argument.
            data.extendleft(reversed(header))
            # See #8.
            if truncate:
                columns = list(zip(*data))
            else:
                columns = list(zip_longest(*data, fillvalue=''))
            widths = self._get_widths(columns)

        if self.THEMES[self.theme]['footer_line']:
            footer = self._get_footer(widths)
            data.append(footer)
            # See #8.
            if truncate:
                columns = list(zip(*data))
            else:
                columns = list(zip_longest(*data, fillvalue=''))
            widths = self._get_widths(columns)

        # Align columns and then transpose it again to get the table back.
        self.data = list(zip(*self._align(columns, widths)))
Exemple #29
0
    def testfind(self):
        l = SortedList([9,7,1,3,6,2])

        for j, k in itertools.zip_longest(l.find(), [1,2,3,6,7,9]):
            self.assertEqual(j, k)

        for j, k in itertools.zip_longest(l.find(min_key=3), [3,6,7,9]):
            self.assertEqual(j, k)

        for j, k in itertools.zip_longest(l.find(max_key=6), [1,2,3,6]):
            self.assertEqual(j, k)

        for j, k in itertools.zip_longest(l.find(min_key=3, max_key=6), [3,6]):
            self.assertEqual(j, k)
 def yield_open_entry(open_entry):
     """ Yield all open changes. """
     ls, rs = open_entry
     # Get unchanged parts onto the right line
     if ls[0] == rs[0]:
         yield (False, ls[0], rs[0])
         for l, r in zip_longest(ls[1:], rs[1:]):
             yield (True, l, r)
     elif ls[-1] == rs[-1]:
         for l, r in zip_longest(ls[:-1], rs[:-1]):
             yield (l != r, l, r)
         yield (False, ls[-1], rs[-1])
     else:
         for l, r in zip_longest(ls, rs):
             yield (True, l, r)
Exemple #31
0
 def assertSimilarSequence(self, seq1, seq2, msg=None):
     for a, b in zip_longest(seq1, seq2):
         self.assertSimilar(a, b)
Exemple #32
0
    def evaluate(self,
                 results,
                 metric='bbox',
                 logger=None,
                 jsonfile_prefix=None,
                 classwise=False,
                 proposal_nums=(100, 300, 1000),
                 iou_thrs=None,
                 metric_items=None):
        """Evaluation in COCO protocol.

        Args:
            results (list[list | tuple]): Testing results of the dataset.
            metric (str | list[str]): Metrics to be evaluated. Options are
                'bbox', 'segm', 'proposal', 'proposal_fast'.
            logger (logging.Logger | str | None): Logger used for printing
                related information during evaluation. Default: None.
            jsonfile_prefix (str | None): The prefix of json files. It includes
                the file path and the prefix of filename, e.g., "a/b/prefix".
                If not specified, a temp file will be created. Default: None.
            classwise (bool): Whether to evaluating the AP for each class.
            proposal_nums (Sequence[int]): Proposal number used for evaluating
                recalls, such as recall@100, recall@1000.
                Default: (100, 300, 1000).
            iou_thrs (Sequence[float], optional): IoU threshold used for
                evaluating recalls/mAPs. If set to a list, the average of all
                IoUs will also be computed. If not specified, [0.50, 0.55,
                0.60, 0.65, 0.70, 0.75, 0.80, 0.85, 0.90, 0.95] will be used.
                Default: None.
            metric_items (list[str] | str, optional): Metric items that will
                be returned. If not specified, ``['AR@100', 'AR@300',
                'AR@1000', 'AR_s@1000', 'AR_m@1000', 'AR_l@1000' ]`` will be
                used when ``metric=='proposal'``, ``['mAP', 'mAP_50', 'mAP_75',
                'mAP_s', 'mAP_m', 'mAP_l']`` will be used when
                ``metric=='bbox' or metric=='segm'``.

        Returns:
            dict[str, float]: COCO style evaluation metric.
        """

        metrics = metric if isinstance(metric, list) else [metric]
        allowed_metrics = ['bbox', 'segm', 'proposal', 'proposal_fast']
        for metric in metrics:
            if metric not in allowed_metrics:
                raise KeyError(f'metric {metric} is not supported')
        if iou_thrs is None:
            iou_thrs = np.linspace(.5,
                                   0.95,
                                   int(np.round((0.95 - .5) / .05)) + 1,
                                   endpoint=True)
        if metric_items is not None:
            if not isinstance(metric_items, list):
                metric_items = [metric_items]

        result_files, tmp_dir = self.format_results(results, jsonfile_prefix)

        eval_results = OrderedDict()
        cocoGt = self.coco
        for metric in metrics:
            msg = f'Evaluating {metric}...'
            if logger is None:
                msg = '\n' + msg
            print_log(msg, logger=logger)

            if metric == 'proposal_fast':
                ar = self.fast_eval_recall(results,
                                           proposal_nums,
                                           iou_thrs,
                                           logger='silent')
                log_msg = []
                for i, num in enumerate(proposal_nums):
                    eval_results[f'AR@{num}'] = ar[i]
                    log_msg.append(f'\nAR@{num}\t{ar[i]:.4f}')
                log_msg = ''.join(log_msg)
                print_log(log_msg, logger=logger)
                continue

            iou_type = 'bbox' if metric == 'proposal' else metric
            if metric not in result_files:
                raise KeyError(f'{metric} is not in results')
            try:
                predictions = mmcv.load(result_files[metric])
                if iou_type == 'segm':
                    # Refer to https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py#L331  # noqa
                    # When evaluating mask AP, if the results contain bbox,
                    # cocoapi will use the box area instead of the mask area
                    # for calculating the instance area. Though the overall AP
                    # is not affected, this leads to different
                    # small/medium/large mask AP results.
                    for x in predictions:
                        x.pop('bbox')
                    warnings.simplefilter('once')
                    warnings.warn(
                        'The key "bbox" is deleted for more accurate mask AP '
                        'of small/medium/large instances since v2.12.0. This '
                        'does not change the overall mAP calculation.',
                        UserWarning)
                cocoDt = cocoGt.loadRes(predictions)
            except IndexError:
                print_log('The testing results of the whole dataset is empty.',
                          logger=logger,
                          level=logging.ERROR)
                break

            cocoEval = COCOeval(cocoGt, cocoDt, iou_type)
            cocoEval.params.catIds = self.cat_ids
            cocoEval.params.imgIds = self.img_ids
            cocoEval.params.maxDets = list(proposal_nums)
            cocoEval.params.iouThrs = iou_thrs
            # mapping of cocoEval.stats
            coco_metric_names = {
                'mAP': 0,
                'mAP_50': 1,
                'mAP_75': 2,
                'mAP_s': 3,
                'mAP_m': 4,
                'mAP_l': 5,
                'AR@100': 6,
                'AR@300': 7,
                'AR@1000': 8,
                'AR_s@1000': 9,
                'AR_m@1000': 10,
                'AR_l@1000': 11
            }
            if metric_items is not None:
                for metric_item in metric_items:
                    if metric_item not in coco_metric_names:
                        raise KeyError(
                            f'metric item {metric_item} is not supported')

            if metric == 'proposal':
                cocoEval.params.useCats = 0
                cocoEval.evaluate()
                cocoEval.accumulate()

                # Save coco summarize print information to logger
                redirect_string = io.StringIO()
                with contextlib.redirect_stdout(redirect_string):
                    cocoEval.summarize()
                print_log('\n' + redirect_string.getvalue(), logger=logger)

                if metric_items is None:
                    metric_items = [
                        'AR@100', 'AR@300', 'AR@1000', 'AR_s@1000',
                        'AR_m@1000', 'AR_l@1000'
                    ]

                for item in metric_items:
                    val = float(
                        f'{cocoEval.stats[coco_metric_names[item]]:.3f}')
                    eval_results[item] = val
            else:
                cocoEval.evaluate()
                cocoEval.accumulate()

                # Save coco summarize print information to logger
                redirect_string = io.StringIO()
                with contextlib.redirect_stdout(redirect_string):
                    cocoEval.summarize()
                print_log('\n' + redirect_string.getvalue(), logger=logger)

                if classwise:  # Compute per-category AP
                    # Compute per-category AP
                    # from https://github.com/facebookresearch/detectron2/
                    precisions = cocoEval.eval['precision']
                    # precision: (iou, recall, cls, area range, max dets)
                    assert len(self.cat_ids) == precisions.shape[2]

                    results_per_category = []
                    for idx, catId in enumerate(self.cat_ids):
                        # area range index 0: all area ranges
                        # max dets index -1: typically 100 per image
                        nm = self.coco.loadCats(catId)[0]
                        precision = precisions[:, :, idx, 0, -1]
                        precision = precision[precision > -1]
                        if precision.size:
                            ap = np.mean(precision)
                        else:
                            ap = float('nan')
                        results_per_category.append(
                            (f'{nm["name"]}', f'{float(ap):0.3f}'))

                    num_columns = min(6, len(results_per_category) * 2)
                    results_flatten = list(
                        itertools.chain(*results_per_category))
                    headers = ['category', 'AP'] * (num_columns // 2)
                    results_2d = itertools.zip_longest(*[
                        results_flatten[i::num_columns]
                        for i in range(num_columns)
                    ])
                    table_data = [headers]
                    table_data += [result for result in results_2d]
                    table = AsciiTable(table_data)
                    print_log('\n' + table.table, logger=logger)

                if metric_items is None:
                    metric_items = [
                        'mAP', 'mAP_50', 'mAP_75', 'mAP_s', 'mAP_m', 'mAP_l'
                    ]

                for metric_item in metric_items:
                    key = f'{metric}_{metric_item}'
                    val = float(
                        f'{cocoEval.stats[coco_metric_names[metric_item]]:.3f}'
                    )
                    eval_results[key] = val
                ap = cocoEval.stats[:6]
                eval_results[f'{metric}_mAP_copypaste'] = (
                    f'{ap[0]:.3f} {ap[1]:.3f} {ap[2]:.3f} {ap[3]:.3f} '
                    f'{ap[4]:.3f} {ap[5]:.3f}')
        if tmp_dir is not None:
            tmp_dir.cleanup()
        return eval_results
Exemple #33
0
from itertools import zip_longest

with open('input', 'r') as program:
    program = program.read()
    program = program[7:]
    program = program.split('\nmask = ')

memory = {}

for data in program:
    data = data.split("\n")
    mask_value = data[0]

    for i in data[1:]:
        i = i.split(" = ")
        i[1] = f"{int(i[1]):036b}"
        for idx, (value, mask) in enumerate(zip_longest(i[1], mask_value)):
            if mask != "X":
                i[1] = i[1][:idx] + mask + i[1][idx + 1:]
        i[1] = int(i[1], 2)
        memory[i[0]] = i[1]

print("Part 1 | The sum of all values left in memory:", sum(memory.values()))
    def _derive_coco_results(self, coco_eval, iou_type, class_names=None):
        """
        Derive the desired score numbers from summarized COCOeval.

        Args:
            coco_eval (None or COCOEval): None represents no predictions from model.
            iou_type (str):
            class_names (None or list[str]): if provided, will use it to predict
                per-category AP.

        Returns:
            a dict of {metric name: score}
        """

        metrics = {
            "bbox": ["AP", "AP50", "AP75", "APs", "APm", "APl"],
            "segm": ["AP", "AP50", "AP75", "APs", "APm", "APl"],
            "keypoints": ["AP", "AP50", "AP75", "APm", "APl"],
        }[iou_type]

        if coco_eval is None:
            self._logger.warn("No predictions from the model!")
            return {metric: float("nan") for metric in metrics}

        # the standard metrics
        results = {
            metric: float(coco_eval.stats[idx] * 100 if coco_eval.stats[idx] >= 0 else "nan")
            for idx, metric in enumerate(metrics)
        }
        self._logger.info(
            "Evaluation results for {}: \n".format(iou_type) + create_small_table(results)
        )
        if not np.isfinite(sum(results.values())):
            self._logger.info("Some metrics cannot be computed and is shown as NaN.")

        if class_names is None or len(class_names) <= 1:
            return results
        # Compute per-category AP
        # from https://github.com/facebookresearch/Detectron/blob/a6a835f5b8208c45d0dce217ce9bbda915f44df7/detectron/datasets/json_dataset_evaluator.py#L222-L252 # noqa
        precisions = coco_eval.eval["precision"]
        # precision has dims (iou, recall, cls, area range, max dets)
        assert len(class_names) == precisions.shape[2]

        results_per_category = []
        for idx, name in enumerate(class_names):
            # area range index 0: all area ranges
            # max dets index -1: typically 100 per image
            precision = precisions[:, :, idx, 0, -1]
            precision = precision[precision > -1]
            ap = np.mean(precision) if precision.size else float("nan")
            results_per_category.append(("{}".format(name), float(ap * 100)))

        # tabulate it
        N_COLS = min(6, len(results_per_category) * 2)
        results_flatten = list(itertools.chain(*results_per_category))
        results_2d = itertools.zip_longest(*[results_flatten[i::N_COLS] for i in range(N_COLS)])
        table = tabulate(
            results_2d,
            tablefmt="pipe",
            floatfmt=".3f",
            headers=["category", "AP"] * (N_COLS // 2),
            numalign="left",
        )
        self._logger.info("Per-category {} AP: \n".format(iou_type) + table)

        results.update({"AP-" + name: ap for name, ap in results_per_category})
        return results
Exemple #35
0
def keeper_costs():
    filename = f'keeper-costs-{get_todays_date()}.csv'
    filepath = f'reports/{filename}'
    makedirs(path.dirname(filepath), exist_ok=True)
    if path.exists(filepath):
        print("EXISTS")
        return send_file(filepath,
                         attachment_filename=filename,
                         as_attachment=True,
                         cache_timeout=-1)

    players = db.session.query(Team.name,
                               Player.first_name,
                               Player.last_name,
                               Player.keeper_cost) \
        .join(Player, Player.team_id == Team.id) \
        .order_by(Player.keeper_cost.asc()) \
        .all()

    keeper_map = {}
    for player in players:
        team_name = player[0]
        player_name = f'{player[1]} {player[2]}'
        keeper_cost = player[3]

        if team_name not in keeper_map:
            keeper_map[team_name] = []
        keeper_map[team_name].append((player_name, keeper_cost))

    rosters = list(
        itertools.zip_longest(*keeper_map.values(), fillvalue=[None, None]))
    for i, row in enumerate(rosters):
        rosters[i] = [item for sublist in row for item in sublist]

    teams = db.session.query(Team.id, Team.name) \
        .all()
    team_map = {}
    for team in teams:
        team_map[team[0]] = team[1]

    picks = db.session.query(Pick.draft_round,
                             Pick.original_team_id,
                             Pick.owning_team_id) \
        .order_by(Pick.draft_round) \
        .all()
    pick_map = {}
    for pick in picks:
        owner_team = team_map[pick[2]]
        original_team = team_map[pick[1]]
        draft_value = pick[0]
        draft_round = f'{draft_value}{__get_ordinal(draft_value)} Round'
        if original_team != owner_team:
            draft_round = f'{draft_round} ({original_team})'
        if owner_team not in pick_map:
            pick_map[owner_team] = []
        pick_map[owner_team].append((draft_round, draft_value))
    draft_picks = list(
        itertools.zip_longest(*pick_map.values(), fillvalue=[None, None]))
    for i, row in enumerate(draft_picks):
        draft_picks[i] = [item for sublist in row for item in sublist]

    teams = []
    for team in keeper_map.keys():
        teams.append(team)
        teams.append(None)

    with open(filepath, 'w') as f:
        writer = csv.writer(f)
        writer.writerow(teams)
        writer.writerows(rosters)
        writer.writerows(draft_picks)

    return send_file(filepath,
                     attachment_filename=filename,
                     as_attachment=True,
                     cache_timeout=-1)
Exemple #36
0
def mlp_extractor(flat_observations, net_arch, act_fun):
    """
    Constructs an MLP that receives observations as an input and outputs a latent representation for the policy and
    a value network. The ``net_arch`` parameter allows to specify the amount and size of the hidden layers and how many
    of them are shared between the policy network and the value network. It is assumed to be a list with the following
    structure:

    1. An arbitrary length (zero allowed) number of integers each specifying the number of units in a shared layer.
       If the number of ints is zero, there will be no shared layers.
    2. An optional dict, to specify the following non-shared layers for the value network and the policy network.
       It is formatted like ``dict(vf=[<value layer sizes>], pi=[<policy layer sizes>])``.
       If it is missing any of the keys (pi or vf), no non-shared layers (empty list) is assumed.

    For example to construct a network with one shared layer of size 55 followed by two non-shared layers for the value
    network of size 255 and a single non-shared layer of size 128 for the policy network, the following layers_spec
    would be used: ``[55, dict(vf=[255, 255], pi=[128])]``. A simple shared network topology with two layers of size 128
    would be specified as [128, 128].

    :param flat_observations: (tf.Tensor) The observations to base policy and value function on.
    :param net_arch: ([int or dict]) The specification of the policy and value networks.
        See above for details on its formatting.
    :param act_fun: (tf function) The activation function to use for the networks.
    :return: (tf.Tensor, tf.Tensor) latent_policy, latent_value of the specified network.
        If all layers are shared, then ``latent_policy == latent_value``
    """
    latent = flat_observations
    policy_only_layers = [
    ]  # Layer sizes of the network that only belongs to the policy network
    value_only_layers = [
    ]  # Layer sizes of the network that only belongs to the value network

    # Iterate through the shared layers and build the shared parts of the network
    for idx, layer in enumerate(net_arch):
        if isinstance(layer, int):  # Check that this is a shared layer
            layer_size = layer
            latent = act_fun(
                linear(latent,
                       "shared_fc{}".format(idx),
                       layer_size,
                       init_scale=np.sqrt(2)))
        else:
            assert isinstance(
                layer, dict
            ), "Error: the net_arch list can only contain ints and dicts"
            if 'pi' in layer:
                assert isinstance(
                    layer['pi'], list
                ), "Error: net_arch[-1]['pi'] must contain a list of integers."
                policy_only_layers = layer['pi']

            if 'vf' in layer:
                assert isinstance(
                    layer['vf'], list
                ), "Error: net_arch[-1]['vf'] must contain a list of integers."
                value_only_layers = layer['vf']
            break  # From here on the network splits up in policy and value network

    # Build the non-shared part of the network
    latent_policy = latent
    latent_value = latent
    for idx, (pi_layer_size, vf_layer_size) in enumerate(
            zip_longest(policy_only_layers, value_only_layers)):
        if pi_layer_size is not None:
            assert isinstance(
                pi_layer_size,
                int), "Error: net_arch[-1]['pi'] must only contain integers."
            latent_policy = act_fun(
                linear(latent_policy,
                       "pi_fc{}".format(idx),
                       pi_layer_size,
                       init_scale=np.sqrt(2)))

        if vf_layer_size is not None:
            assert isinstance(
                vf_layer_size,
                int), "Error: net_arch[-1]['vf'] must only contain integers."
            latent_value = act_fun(
                linear(latent_value,
                       "vf_fc{}".format(idx),
                       vf_layer_size,
                       init_scale=np.sqrt(2)))

    return latent_policy, latent_value
Exemple #37
0
    def move_data(
        self,
        source_remote_paths: Union[str, Iterable[str]],
        target_remote_paths: Union[None, str, Iterable[str]] = None,
        *,
        source_client: Optional["SegmentClient"] = None,
        strategy: str = "abort",
    ) -> None:
        """Move data to this segment, also used to rename data.

        Arguments:
            source_remote_paths: The source remote paths of the moved data.
            target_remote_paths: The target remote paths of the moved data.
                This argument is used to specify new remote paths of the moved data.
                If None, the remote path of the moved data will not be changed after copy.
            source_client: The source segment client of the moved data.
                This argument is used to specifies where the moved data comes from when the moved
                data is from another segment.
                If None, the moved data comes from this segment.
            strategy: The strategy of handling the name conflict. There are three options:

                1. "abort": stop copying and raise exception;
                2. "override": the source data will override the origin data;
                3. "skip": keep the origin data.

        Raises:
            InvalidParamsError: When strategy is invalid.
            OperationError: When the type or the length of target_remote_paths is not equal
                with source_remote_paths.
                Or when the dataset_id and drafter_number of source_client
                is not equal with the current segment client.

        """
        self._status.check_authority_for_draft()

        if strategy not in _STRATEGIES:
            raise InvalidParamsError(param_name="strategy",
                                     param_value=strategy)

        if not target_remote_paths:
            all_target_remote_paths = []
            all_source_remote_paths = ([source_remote_paths] if isinstance(
                source_remote_paths, str) else list(source_remote_paths))

        elif isinstance(source_remote_paths, str) and isinstance(
                target_remote_paths, str):
            all_target_remote_paths = [target_remote_paths]
            all_source_remote_paths = [source_remote_paths]

        elif not isinstance(source_remote_paths, str) and not isinstance(
                target_remote_paths, str):
            all_target_remote_paths = list(target_remote_paths)
            all_source_remote_paths = list(source_remote_paths)
            if len(all_target_remote_paths) != len(all_source_remote_paths):
                raise OperationError(
                    "To move the data, the length of target_remote_paths "
                    "must be equal with source_remote_paths")
        else:
            raise OperationError(
                "To move the data, the type of target_remote_paths "
                "must be equal with source_remote_paths")

        source = {}
        if source_client:
            if (source_client.status.draft_number == self.status.draft_number
                    and source_client._dataset_id  # pylint: disable=protected-access
                    == self._dataset_id  # pylint: disable=protected-access
                ):
                source["segmentName"] = source_client.name
            else:
                raise OperationError(
                    "To move the data, the dataset_id and drafter_number of source_client "
                    "must be equal with the current segment client")
        else:
            source["segmentName"] = self.name

        post_data: Dict[str, Any] = {
            "strategy": strategy,
            "source": source,
            "segmentName": self.name,
        }
        post_data.update(self._status.get_status_info())

        for targets, sources in zip_longest(
                chunked(all_target_remote_paths, 128),
                chunked(all_source_remote_paths, 128)):
            if targets:
                post_data["remotePaths"] = targets
            post_data["source"]["remotePaths"] = sources

            self._client.open_api_do("POST",
                                     "data?multipleMove",
                                     self._dataset_id,
                                     json=post_data)
Exemple #38
0
def hamming_distance(a, b) -> int:
    return sum(i is BLANK or j is BLANK or i != j
               for i, j in itertools.zip_longest(a, b, fillvalue=BLANK))
print(
    'поэтому, если cам <lst> в цикле изменяется, то при объявлении цикла пользуйте <lst[:]>'
)

#8. Составть словарь из двух списков - в одном ключи, в другом значения
keys = ['a', 'b', 'c']
vals = [1, 2, 3]
dictionary = dict(zip(keys, vals))
print('\n#8. СЛОВАРЬ ИЗ ДВУХ СПИСКОВ:\ndict(zip({},{})) = {}'.format(
    keys, vals, dictionary))

#9. Словарь из списков разной длины. Есть key, но нет val - то val=None; если нету key - игнорировать
from itertools import zip_longest
keys = ['a', 'b', 'c', 'd']
vals = [1, 2, 3, 4, 5, 6]
dictionary = dict(zip_longest(keys, vals))
print(
    '\n#9. СЛОВАРЬ ИЗ ДВУХ СПИСКОВ:\ndictionary = dict(izip_longest({}, {})) = {}'
    .format(keys, vals, dictionary))
print('!!!Выполнить\nif None in dictionary\n\tdictionary.pop(None)')
if None in dictionary:
    dictionary.pop(None)
print('dictionary = {}'.format(dictionary))

#10. Инвертировать словарь (ключи поменять со значениями)
dictionary = {'a': 1, 'b': 2, 'c': 3}
new_dictionary = {
    key: val
    for key, val in zip(dictionary.values(), dictionary.keys())
}
print('\n#10. ИНВЕРТИРОВАННЫЙ СЛОВАРЬ:\n {} <=> {}'.format(
Exemple #40
0
def main():
    """ Read input file and process informations

    It initialize lawn map and iterate on mower's to create them,
    place them on the map and process moves.

    First line from input file is lawn map size
    Others lines are grouped 2 by 2 and mean:
    - mower's initial position & orientation
    - mower's moves list

    Before exit, mowers' status is displayed
    """

    # Process command line arguments and setup logging verbosity
    args = read_args()
    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    with open(args.file) as input_file:
        # First line is lawn size
        (lawn_size_horizontal, lawn_size_vertical) = tuple(
            int(x) for x in input_file.readline().rstrip().split(" "))
        lawn_map = init_lawn(lawn_size_horizontal + 1, lawn_size_vertical + 1)

        # Next lines are grouped 2 by 2 which define:
        # - mower initial position & orientation
        # - mower moves list
        mowers = 0
        for initial_position, moves_list in zip_longest(*[input_file] * 2):
            mowers += 1
            if moves_list is None:
                # If we don't have enough lines for current mower
                # That means we reached the end of the line
                # Therefore, we can safely exit loop
                logging.debug("Not enough line. Skipping mower")
                break
            logging.debug("Initializing mower")
            # Extract moxer's position, orientation and moves' list from input file
            mower_initial_position = tuple(
                x for x in initial_position.rstrip().split(" "))
            mower_moves_list = moves_list.replace(" ", "").rstrip()
            try:
                # Initialize mower
                lawn_map, mower = init_mower(lawn_map, mower_initial_position,
                                             mower_moves_list)
            except ValueError as err:
                print("Mower %d: %s" % (mowers, err))
                continue
            except IndexError as err:
                print("Mower %d: %s" % (mowers, err))
                continue
            logging.debug("Mower %d created", mowers)
            try:
                lawn_map, mower = move_mower(lawn_map, mower)
            except ValueError as err:
                print("Mower %d: %s" % (mowers, err))
                continue
            # Mower's moves' list processed.
            # Print final mower's position & orientation
            print(" ".join(str(i) for i in mower["position"]))
Exemple #41
0
 def _combine_pipelines(self):
     gen1, gen2 = tee(self.data_source_generator, 2)
     combined = zip_longest(self.pipe_X(gen1), self.pipe_y(gen2))
     return combined
Exemple #42
0
def zip_longest_out(a, b):
    return zip_longest(a, b)
Exemple #43
0
def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)
Exemple #44
0
def ptable(headers,
           *rows,
           max_width=200,
           str=str,
           str_by_type={},
           justification=()):
    """
    Make an easily readable table.

    :param headers: iterable of header values
    :param rows: iterables of column contents (must all be the same length as headers)
    :param max_width: maximum number of columns for the table (including margins and separators)
    :param str: function to convert items to strings
    :param str_by_type: dictionary mapping types to custom str functions to be used for those types
                        used in preference over default str except for headers which always use default str
    :return: a string containing the table
    """
    headers = [str(h).split("\n") for h in headers]
    rows = [[(str_by_type.get(type(c)) or str)(c).split("\n") for c in row]
            for row in rows]
    assert len(set(len(row) for row in rows) | {len(headers)}
               ) == 1, "headers and rows must have same number of columns"
    # 2 chars padding on the left, 3 between each column, 2 on the right
    available_width = max_width - 2 - (len(headers) - 1) * 3 - 2
    assert available_width >= len(
        headers
    ), "must provide enough width for at least one character per column"
    # use the max and median widths of each column to see if any need to be squeezed to fit the overall max_width
    col_width_maxes = []
    col_width_medians = []
    # zip(*rows) transposes to columns
    # zip_longest in case there are no rows
    for header, col in zip_longest(headers, zip(*rows), fillvalue=()):
        widths = [max(len(line) for line in r) for r in col]
        widths.append(max(len(line) for line in header))
        col_width_maxes.append(max(widths))
        col_width_medians.append(median_low(widths))
    col_widths = col_width_maxes

    if sum(col_width_maxes) > available_width:
        # reduce the column with the greatest difference between max and median width by one repeatedly until it fits
        diffs = {
            i: d
            for i, d in enumerate(
                mx - md for mx, md in zip(col_width_maxes, col_width_medians))
        }
        to_chop = defaultdict(int)
        while sum(col_width_maxes) - sum(to_chop.values()) > available_width:
            i, _ = max(diffs.items(), key=itemgetter(1))
            diffs[i] -= 1
            to_chop[i] += 1
        for i, tc in to_chop.items():
            col_widths[i] -= tc
        headers = [_squeeze(h, col_widths[i]) for i, h in enumerate(headers)]
        rows = [[_squeeze(c, col_widths[i]) for i, c in enumerate(row)]
                for row in rows]
        # recalculate the max width after the squeeze and use the lesser of that and the current width to avoid
        # whitespace at the end of wrapped lines
        for i, (header, col) in enumerate(
                zip_longest(headers, zip(*rows), fillvalue=())):
            widths = [max(len(line) for line in r) for r in col]
            widths.append(max(len(line) for line in header))
            col_widths[i] = min(col_widths[i], max(widths))

    out = ""
    header_height = max(len(h) for h in headers)
    for header in headers:
        header.extend([""] * (header_height - len(header)))
    for line in zip(*headers):
        out += "| {} |\n".format(" | ".join(
            just(col_line, col_widths[i]) for i, (just, col_line) in enumerate(
                zip_longest(justification, line, fillvalue=ljust))))
    out += "|"
    for just, cw in zip_longest(justification, col_widths, fillvalue=ljust):
        # markdown justification markers
        l = ":" if just in (ljust, cjust) else " "
        r = ":" if just in (cjust, rjust) else " "
        out += "{}{}{}|".format(l, "-" * cw, r)
    out += "\n"
    for row in rows:
        row_height = max(len(c) for c in row)
        for col in row:
            col.extend([""] * (row_height - len(col)))
        for line in zip(*row):
            out += "| {} |\n".format(" | ".join(
                just(col_line, col_widths[i])
                for i, (just, col_line) in enumerate(
                    zip_longest(justification, line, fillvalue=ljust))))
    return out
Exemple #45
0
def annotate(X: Union[np.ndarray, Series, List, Tuple],
             Y: Union[np.ndarray, Series, List, Tuple],
             T: Union[np.ndarray, Series, List, Tuple],
             subset: Optional[Union[np.ndarray, Series, List, Tuple]] = None,
             ax: mpl.axes.Axes = None,
             word_shorten: Optional[int] = None,
             **annotate_kws):
    """Annotates a matplotlib plot with text.

    Offsets are pre-determined according to the scale of the plot.

    Parameters
    ----------
    X : list/tuple/np.ndarray/pd.Series (1d)
        The x-positions of the text.
    Y : list/tuple/np.ndarray/pd.Series (1d)
        The y-positions of the text.
    T : list/tuple/np.ndarray/pd.Series (1d)
        The text array.
    subset : list/tuple/np.ndarray/pd.Series (1d)
        An array of indices to select a subset from.
    ax : matplotlib.ax.Axes, optional, default=None
        If None, creates one.
    word_shorten : int, optional
        If not None, shortens annotated strings to be more concise and displayable

    Other Parameters
    ----------------
    annotate_kws : dict
        Other keywords to pass to `ax.annotate`

    Returns
    -------
    ax : matplotlib.ax.Axes
        The same matplotlib plot, or the one generated
    """
    instance_check((X, Y), (list, tuple, np.ndarray, Series))
    instance_check(T, (list, tuple, np.ndarray, Series, Index))
    instance_check(subset,
                   (type(None), list, tuple, np.ndarray, Series, Index))
    instance_check(ax, (type(None), mpl.axes.Axes))
    arrays_equal_size(X, Y, T)
    # convert to numpy.
    _X = as_flattened_numpy(X).copy()
    _Y = as_flattened_numpy(Y).copy()
    _T = as_flattened_numpy(T)

    if word_shorten:
        _T = shorten(_T, newl=word_shorten)

    if _X.dtype.kind == "f":
        _X += (_X.max() - _X.min()) / 30.0
        _Y += -((_Y.max() - _Y.min()) / 30.0)

    if ax is None:
        fig, ax = plt.subplots(figsize=(8, 5))
    if subset is None:
        for x, y, t in it.zip_longest(_X, _Y, _T):
            ax.annotate(t, xy=(x, y), **annotate_kws)
    else:
        for i in subset:
            ax.annotate(_T[i], xy=(_X[i], _Y[i]), **annotate_kws)

    return ax
Exemple #46
0
 def grouper(n, iterable, fillvalue=None):
     kargs = [iter(iterable)] * n
     return zip_longest(*kargs, fillvalue=fillvalue)
Exemple #47
0
def to_NNF(expr, composite_map=None):
    """
    Generates the Negation Normal Form of any boolean expression in terms
    of AND, OR, and Literal objects.

    Examples
    ========

    >>> from sympy import Q, Eq
    >>> from sympy.assumptions.cnf import to_NNF
    >>> from sympy.abc import x, y
    >>> expr = Q.even(x) & ~Q.positive(x)
    >>> to_NNF(expr)
    (Literal(Q.even(x), False) & Literal(Q.positive(x), True))

    Supported boolean objects are converted to corresponding predicates.

    >>> to_NNF(Eq(x, y))
    Literal(Q.eq(x, y), False)

    If ``composite_map`` argument is given, ``to_NNF`` decomposes the
    specified predicate into a combination of primitive predicates.

    >>> cmap = {Q.nonpositive: Q.negative | Q.zero}
    >>> to_NNF(Q.nonpositive, cmap)
    (Literal(Q.negative, False) | Literal(Q.zero, False))
    >>> to_NNF(Q.nonpositive(x), cmap)
    (Literal(Q.negative(x), False) | Literal(Q.zero(x), False))
    """
    from sympy.assumptions.ask import Q
    from sympy.assumptions.assume import AppliedPredicate, Predicate

    if composite_map is None:
        composite_map = dict()


    binrelpreds = {Eq: Q.eq, Ne: Q.ne, Gt: Q.gt, Lt: Q.lt, Ge: Q.ge, Le: Q.le}
    if type(expr) in binrelpreds:
        pred = binrelpreds[type(expr)]
        expr = pred(*expr.args)

    if isinstance(expr, Not):
        arg = expr.args[0]
        tmp = to_NNF(arg, composite_map)  # Strategy: negate the NNF of expr
        return ~tmp

    if isinstance(expr, Or):
        return OR(*[to_NNF(x, composite_map) for x in Or.make_args(expr)])

    if isinstance(expr, And):
        return AND(*[to_NNF(x, composite_map) for x in And.make_args(expr)])

    if isinstance(expr, Nand):
        tmp = AND(*[to_NNF(x, composite_map) for x in expr.args])
        return ~tmp

    if isinstance(expr, Nor):
        tmp = OR(*[to_NNF(x, composite_map) for x in expr.args])
        return ~tmp

    if isinstance(expr, Xor):
        cnfs = []
        for i in range(0, len(expr.args) + 1, 2):
            for neg in combinations(expr.args, i):
                clause = [~to_NNF(s, composite_map) if s in neg else to_NNF(s, composite_map)
                          for s in expr.args]
                cnfs.append(OR(*clause))
        return AND(*cnfs)

    if isinstance(expr, Xnor):
        cnfs = []
        for i in range(0, len(expr.args) + 1, 2):
            for neg in combinations(expr.args, i):
                clause = [~to_NNF(s, composite_map) if s in neg else to_NNF(s, composite_map)
                          for s in expr.args]
                cnfs.append(OR(*clause))
        return ~AND(*cnfs)

    if isinstance(expr, Implies):
        L, R = to_NNF(expr.args[0], composite_map), to_NNF(expr.args[1], composite_map)
        return OR(~L, R)

    if isinstance(expr, Equivalent):
        cnfs = []
        for a, b in zip_longest(expr.args, expr.args[1:], fillvalue=expr.args[0]):
            a = to_NNF(a, composite_map)
            b = to_NNF(b, composite_map)
            cnfs.append(OR(~a, b))
        return AND(*cnfs)

    if isinstance(expr, ITE):
        L = to_NNF(expr.args[0], composite_map)
        M = to_NNF(expr.args[1], composite_map)
        R = to_NNF(expr.args[2], composite_map)
        return AND(OR(~L, M), OR(L, R))

    if isinstance(expr, AppliedPredicate):
        pred, args = expr.function, expr.arguments
        newpred = composite_map.get(pred, None)
        if newpred is not None:
            return to_NNF(newpred.rcall(*args), composite_map)

    if isinstance(expr, Predicate):
        newpred = composite_map.get(expr, None)
        if newpred is not None:
            return to_NNF(newpred, composite_map)

    return Literal(expr)
            locations.append(location)

        except:

            location = 'None'

        try:

            job_description = driver.find_element_by_xpath(
                "//*[@class='jobsearch-jobDescriptionText']").text
            summary.append(job_description)

        except:

            job_description = 'None'

driver.close()

# Converting all the details into dataframe and csv file
final = []
for item in zip_longest(title, company, locations, summary):
    final.append(item)

df4 = pd.DataFrame(
    final, columns=['Job_title', 'Company_name', 'Locations', 'Summary'])
# df.to_csv('booked.csv')

# Converting into csv file

df4.to_csv("indeed.csv")
Exemple #49
0
#List of contractor emails
contractorEmailList = []

#CSV File //Make sure the file is presented in your project folder
with open('drbill_contracting_new_jersey_email.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)

    next(csv_reader)

    for line in csv_reader:
        contractorEmailList.append(line[0])

#Breaks up array into chunks to be sent
chunks = [[i for i in t if i is not None]
          for t in zip_longest(*(contractorEmailList, ) * 1)]
# print(chunks[2])
points = 0


def do_something(sc):
    try:
        global points
        print("Sending Email To..", chunks[points])
        with smtplib.SMTP('smtp.office365.com', 587) as smtp:
            smtp.ehlo()
            smtp.starttls()
            smtp.ehlo()

            #User name and password
            smtp.login('*****@*****.**', 'password')
Exemple #50
0
# Ejercicio 958: Usar las funciones chain() y zip_longest() para fusionar listas de diferente tamaño (longitud).

from itertools import chain, zip_longest

primos = [2, 3, 5, 7]
letras = ['A', 'B', 'C']
negativos = [-1, -2]
reales = [3.14, 2.71, 1.41, 0.73, 0.91]

# [2, 'A', -1, 3.14, 3, 'B', -2, 2.71, ...]

resultado = [
    e for e in chain(*zip_longest(primos, letras, negativos, reales))
    if e is not None
]

print(resultado)
Exemple #51
0
    def save_animation(self, filename, datasets=None, fps=10, fill_value=None,
                       batch_size=None, ignore_missing=False, **kwargs):
        """Helper method for saving to movie or GIF formats.

        Supported formats are dependent on the `imageio` library and are
        determined by filename extension by default.

        By default all datasets available will be saved to individual files
        using the first Scene's datasets metadata to format the filename
        provided. If a dataset is not available from a Scene then a black
        array is used instead (np.zeros(shape)).

        Args:
            filename (str): Filename to save to. Can include python string
                            formatting keys from dataset ``.attrs``
                            (ex. "{name}_{start_time:%Y%m%d_%H%M%S.gif")
            datasets (list): DatasetIDs to save (default: all datasets)
            fps (int): Frames per second for produced animation
            fill_value (int): Value to use instead creating an alpha band.
            batch_size (int): Group array computation in to this many arrays
                              at a time. This is useful to avoid memory
                              issues. Defaults to all of the arrays at once.
            ignore_missing (bool): Don't include a black frame when a dataset
                                   is missing from a child scene.
            kwargs: Additional keyword arguments to pass to
                   `imageio.get_writer`.

        """
        if imageio is None:
            raise ImportError("Missing required 'imageio' library")

        scenes = iter(self._scenes)
        first_scene = next(scenes)
        info_scenes = [first_scene]
        if 'end_time' in filename:
            # if we need the last scene to generate the filename
            # then compute all the scenes so we can figure it out
            log.debug("Generating scenes to compute end_time for filename")
            scenes = list(scenes)
            info_scenes.append(scenes[-1])
        scene_gen = _SceneGenerator(chain([first_scene], scenes))

        if not self.is_generator:
            available_ds = self.loaded_dataset_ids
        else:
            available_ds = list(first_scene.keys())
        dataset_ids = datasets or available_ds

        writers = []
        delayeds = []
        for dataset_id in dataset_ids:
            if not self.is_generator and not self._all_same_area([dataset_id]):
                raise ValueError("Sub-scene datasets must all be on the same "
                                 "area (see the 'resample' method).")

            all_datasets = scene_gen[dataset_id]
            info_datasets = [scn.get(dataset_id) for scn in info_scenes]
            this_fn, shape, this_fill = self._get_animation_info(info_datasets, filename, fill_value=fill_value)
            data_to_write = self._get_animation_frames(all_datasets, shape, this_fill, ignore_missing)

            writer = imageio.get_writer(this_fn, fps=fps, **kwargs)
            delayed = cascaded_compute(writer.append_data, data_to_write,
                                       batch_size=batch_size)
            # Save delayeds and writers to compute and close later
            delayeds.append(delayed)
            writers.append(writer)
        # compute all the datasets at once to combine any computations that can be shared
        iter_delayeds = [iter(x) for x in delayeds]
        for delayed_batch in zip_longest(*iter_delayeds):
            delayed_batch = [x for x in delayed_batch if x is not None]
            dask.compute(delayed_batch)
        for writer in writers:
            writer.close()
def interleave(*data):
    return list(
        it.filterfalse(lambda x: x is None,
                       it.chain.from_iterable(it.zip_longest(*data))))
Exemple #53
0
 def padding(self, l):
     return list(itertools.zip_longest(*l,fillvalue=self.zerovalue))
def interleave_pair(a, b):
    return list(
        it.filterfalse(lambda x: x is None,
                       it.chain.from_iterable(it.zip_longest(a, b))))
Exemple #55
0
 def zip_longest(self, second, *others, fill_value=None):
     return itertools.zip_longest(self,
                                  second,
                                  *others,
                                  fillvalue=fill_value)
Exemple #56
0
plt.subplots(figsize=(18, 15))
plt.subplot(4, 3, 1)
plt.subplots_adjust(wspace=0.2, hspace=0.5)
sns.countplot(x='Outcome', data=diabetes_df)
plt.subplot(4, 3, 2)
plt.subplots_adjust(wspace=0.2, hspace=0.5)
sns.barplot(x='Outcome', y='Age', data=diabetes_df)
plt.show()

#data analysis
columns = diabetes_df.columns[:8]
# print(columns)
plt.subplots(figsize=(18, 15))
length = len(columns)
for i, j in itertools.zip_longest(columns, range(length)):
    plt.subplot((length / 2), 3, j + 1)
    plt.subplots_adjust(wspace=0.2, hspace=0.5)
    diabetes_df[i].hist(bins=20, edgecolor='black')
    plt.title(i)
plt.show()

#analysis of diabetic classes
diabetes_class = diabetes_df[diabetes_df['Outcome'] == 1]
columns = diabetes_df.columns[:8]
plt.subplots(figsize=(18, 15))
length = len(columns)
for i, j in itertools.zip_longest(columns, range(length)):
    plt.subplot((length / 2), 3, j + 1)
    plt.subplots_adjust(wspace=0.2, hspace=0.5)
    diabetes_class[i].hist(bins=20, edgecolor='black')
Exemple #57
0
def zeroPadding(l, fillvalue=PAD_token):
    return list(itertools.zip_longest(*l, fillvalue=fillvalue))
Exemple #58
0
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return zip_longest(*args, fillvalue=fillvalue)
Exemple #59
0
from itertools import zip_longest

numbers = [1, 2, 3, 4, 5]
names = ["June", "July", "August", "Sep"]
somet = ('a', 'b', 'c')

z = zip(numbers, names, somet)
print(list(z))

# zipping due to longest

z_longest = zip_longest(numbers, names, somet, fillvalue=None)
print(list(z_longest))


def integer(n):
    for i in range(n):
        yield i


def squares(n):
    for i in range(n):
        yield i**2


iter1 = integer(5)
inter2 = squares(6)

# print(list(iter1), list(inter2))

z_ex = zip(iter1, inter2)
def grouper(iterable, n):
    """Split an iterable into chunks of length `n`, padded if required."""
    args = [iter(iterable)] * n
    return itertools.zip_longest(*args, fillvalue=None)