Example #1
0
def pretty_fmt_diff(diff, file1=None, file2=None):
	def iter_changed(changed):
		try:
			for (p, r, a) in changed:
				yield ((p, r), '-')
				yield ((p, a), '+')
		except ValueError:
			for (p, c) in changed:
				yield ((p, c), '>')

	removed = itertools.izip_longest(diff['removed'], [], fillvalue='-')
	added = itertools.izip_longest(diff['added'], [], fillvalue='+')
	changed = iter_changed(diff['changed'])
	combined = sorted(itertools.chain(added, removed, changed), key=lambda ((p, n), pre): (p, 127-ord(pre), n))

	ret = []
	if file1 is not None:
		ret.append('--- %s' % file1)
	if file2 is not None:
		ret.append('+++ %s' % file2)
	if file1 is not None or file2 is not None:
		ret.append('')

	for ((plist, node), prefix) in combined:
		if node is not None:
			ret.append('%s %s: %s' % (prefix, '->'.join(plist), dumps_json(node)))
		else:
			ret.append('%s %s' % (prefix, '->'.join(plist)))
	return '\n'.join(ret)
Example #2
0
def runUpdateDaemon(restartTimerFunc):
    updateDao = dao.dao()
    for userID in daemonMap.keys():
        if not userID in onlineFriends:
            onlineFriends[userID] = set([])
        
        friendListUpdate = set([])
        friendListOffline = set([])
        fullFriendList = updateDao.getFriends(userID)
        for (_, friendID) in fullFriendList:
            if friendID in onlineClients:
                friendListUpdate.add(friendID)
            else:
                friendListOffline.add(friendID)
        # Build lists (actually sets) of friends who went offline or online
        # Offline: Friend exists in old list but not in new one
        # Online: Friend exists in new list but not in old one
        offlinelist = onlineFriends[userID] & friendListOffline
        onlinelist = friendListUpdate - (onlineFriends[userID] & friendListUpdate)
        
        # Creating the actual data to send -- this uses some craziness with mapping functions
        # Basically, the first line does the offline list, and the second does the online one
        payload = ''.join(starmap(upFormat.pack, list(izip_longest(offlinelist, '', fillvalue=0))))
        payload = payload + ''.join(starmap(upFormat.pack, list(izip_longest(onlinelist, '', fillvalue=1))))
        
        onlineFriends[userID] = onlinelist
        sock = daemonMap[userID]
        sock.send(padToSize(pktFormat.pack(0, r_status_update, len(payload)) + payload, BUFSIZE))
    del updateDao
    restartTimerFunc()
Example #3
0
def thin_fasta(fasta, l, a, p, c, b, positions):
    if not positions:
        for fasta_file in fasta:
            with open(fasta_file, 'r') as f:
                for tag, sequence in itertools.izip_longest(f, f, fillvalue=None):
                    if (
                            (not l or l in tag) and
                            (not a or a in tag) and
                            (not p or p in tag) and
                            (not c or c in tag) and
                            (not b or b in tag)
                    ):
                        sys.stdout.write(tag)
                        sys.stdout.write(sequence)
    else:
        for fasta_file in fasta:
            with open(fasta_file, 'r') as f:
                for tag, sequence in itertools.izip_longest(f, f, fillvalue=None):
                    seq_list = list(sequence)
                    for i, aa in enumerate(seq_list):
                        if i not in positions:
                            seq_list[i] = '-'
                    sequence = ''.join(seq_list)
                    sys.stdout.write(tag)
                    sys.stdout.write('{0}\n'.format(sequence))
Example #4
0
  def load_from_files(self, tileset_name, tilesize):
    import tile
    ss = spritesheet(tileset_name + ".png", tilesize)
    types = None
    names = None
    with open(tileset_name + ".types", 'r') as types_f:
      types = types_f.readlines()
    with open(tileset_name + ".names", 'r') as names_f:
      names = names_f.readlines()

    if types is None:
      raise ValueError("Could not open types definition, {0}.types".format(tileset_name))

    for x, rows in enumerate(izip_longest(types, names)):
      type_row, name_row = rows
      type_row_tmp = type_row.strip().split(',')
      type_row = []
      for i in type_row_tmp:
        typ, sep, num = i.partition("*")
        if sep != "":
          for n in xrange(int(num)):
            type_row.append(getattr(tile, typ.strip()))
        else:
          type_row.append(getattr(tile, typ.strip()))
  
      if name_row is not None:
        name_row = name_row.strip().split(',')
      else:
        name_row = []
      for y, cols in enumerate(izip_longest(type_row, name_row)):
        type_col, name_col = cols
        typ = type_col(ss.image_pos((y, x)))
        self.tiles[x][y] = typ
        if name_col is not None:
          self.aliases[name_col.strip()] = (x, y)
Example #5
0
def itersplit_to_fields(str_,
                        fsep=DEFAULT_FSEP,
                        revtuple=None,
                        fields=[],
                        preparse=None):
    """
    Itersplit a string into a (named, if specified) tuple.

    Args:
        str_ (str): string to split
        fsep (str): field separator (delimiter to split by)
        revtuple (object): namedtuple (or class with a ``._fields`` attr)
            (optional)
        fields (list of str): field names (if revtuple is not specified)
        preparse (callable): function to parse str with before itersplitting

    Returns:
        tuple or revtuple: fields as a tuple or revtuple, if specified

    """
    if preparse:
        str_ = preparse(str_)
    _fields = itersplit(str_, fsep)

    if revtuple is not None:
        try:
            values = (t[1] for t in izip_longest(revtuple._fields, _fields))
            return revtuple(*values)
        except:
            log.error(revtuple)
            log.error(_fields)
            raise

    return tuple(izip_longest(fields, _fields, fillvalue=None))
Example #6
0
def ps_mult_karatsuba(M, N, kthresh=40):
    m = len(M)
    n = len(N)

    # Classical Multiplication is faster until a certain point.
    if m < kthresh or n < kthresh:
        return ps_mult_classical(M, N)

    # The algorithm works best when we split into halves
    k = max(m, n) // 2

    # write M = f1*x^k + f0, N = g1*x^k + g0
    f0 = M[0:k]
    f1 = M[k:]
    g0 = N[0:k]
    g1 = N[k:]

    # We want to compute MN = (f1*x^k + f0)(g1*x^k + g0).
    # We let z0 = f0*g0, z2 = f1*g1
    z0 = ps_mult_karatsuba(f0, g0, kthresh)
    z2 = ps_mult_karatsuba(f1, g1, kthresh)

    f1_f0 = [sum(e) for e in itertools.izip_longest(f1, f0, fillvalue=0)]
    g1_g0 = [sum(e) for e in itertools.izip_longest(g1, g0, fillvalue=0)]

    xxyy = ps_mult_karatsuba(f1_f0, g1_g0, kthresh)

    z1 = [e[0] - e[1] - e[2] for e in itertools.izip_longest(xxyy, z2, z0, fillvalue=0)]

    t2 = [0] * (2 * k) + z2
    t1 = [0] * (k) + z1
    xy = [sum(e) for e in itertools.izip_longest(t2, t1, z0, fillvalue=0)]
    return xy
Example #7
0
def translation_surface_cmp(s1, s2):
    r"""
    Compare two finite surfaces. 
    The surfaces will be considered equal if and only if there is a translation automorphism
    respecting the polygons and the base_labels.
    """
    if not s1.is_finite() or not s2.is_finite():
        raise NotImplementedError
    lw1=s1.walker()
    lw2=s2.walker()
    from itertools import izip_longest
    for p1,p2 in izip_longest(lw1.polygon_iterator(), lw2.polygon_iterator()):
        if p1 is None:
            # s2 has more polygons
            return -1
        if p2 is None:
            # s1 has more polygons
            return 1
        ret = polygon_compare(p1,p2)
        if ret != 0:
            return ret
    # Polygons are identical. Compare edge gluings.
    for pair1,pair2 in izip_longest(lw1.edge_iterator(), lw2.edge_iterator()):
        l1,e1 = s1.opposite_edge(pair1)
        l2,e2 = s2.opposite_edge(pair2)
        num1 = lw1.label_to_number(l1)
        num2 = lw2.label_to_number(l2)
        ret = cmp(num1,num2)
        if ret!=0:
            return ret
        ret = cmp(e1,e2)
        if ret!=0:
            return ret
    return 0
Example #8
0
def count_possib(block, isLast=False):
    """
    Подсчитывает вероятности равенства 0 для пары Alpha/Betta
    """
    _alpha = range(1, 16)
    _betta = dict(itertools.izip_longest(range(1, 8), '', fillvalue=0))
    if isLast:
        _betta = dict(itertools.izip_longest(range(1, 4), '', fillvalue=0))
    possib = dict([(item, _betta.copy()) for item in _alpha])
    # print possib
    for alpha in possib.keys():
        # print '*'*50
        # print "alpha = %d" % alpha
        for betta in possib[alpha].keys():
            # print '-'*50
            # print "Betta = %d" % betta
            for x_XOR_k in block.keys():
                left = (x_XOR_k & alpha)
                right = (block[x_XOR_k] & betta)
                Q = (
                    bin(left)[2:].zfill(4) + bin(right)[2:].zfill(3)
                    ).count('1') % 2
                if not Q:
                    possib[alpha][betta] += 1
                # print "(a, x_k) = %d * %d = %d" % (x_XOR_k, alpha, left)
                # print "(b, y) = %d * %d = %d" % (block[x_XOR_k], betta, right)
                # print "Q = %d ^ %d = %d" % (left, right, Q)
    return possib
Example #9
0
def format_output(counts, out_base, created_w_bed_fname):
    if out_base == None:
        for count in itertools.izip_longest(*counts):
            print "\t".join(str(i) for i in count)
    else:
        created_w_bed_fname = os.getcwd() + "/" + created_w_bed_fname

        # Chunk to write first column of the text file
        first_col = []
        first_col.append("segment")
        for line in open(created_w_bed_fname).readlines():
            line = line.strip()
            line = line.replace("\t", "_")
            first_col.append(line)

        out_mat = out_base + ".matrix"
        out_txt = out_base + ".txt"
        out1 = open(out_mat, "w")
        out2 = open(out_txt, "w")

        # Writing matrix
        for count in itertools.izip_longest(*counts):
            out1.write("\t".join(str(i) for i in count) + "\n")
        out1.close()

        # Writing text
        counts.insert(0, first_col)
        for count in itertools.izip_longest(*counts):
            out2.write("\t".join(str(i) for i in count) + "\n")
        out2.close()

    return None
Example #10
0
    def test_iter_confusion_matrices(self):
        expected = """\
        tp=5, fp=4, fn=0, tn=0
        tp=5, fp=3, fn=0, tn=1
        tp=5, fp=2, fn=0, tn=2
        tp=5, fp=1, fn=0, tn=3
        tp=4, fp=1, fn=1, tn=3
        tp=4, fp=0, fn=1, tn=4
        tp=3, fp=0, fn=2, tn=4
        tp=2, fp=0, fn=3, tn=4
        tp=1, fp=0, fn=4, tn=4
        tp=0, fp=0, fn=5, tn=4"""
        expected = ["BinaryConfusionMatrix(%s)" % line \
                    for line in dedent(expected).split("\n")]
        for (threshold, matrix), expected in \
                izip_longest(self.data.iter_confusion_matrices(), expected):
            self.assertEqual(repr(matrix), expected)
            self.assertEqual(matrix, self.data.get_confusion_matrix(threshold))

        expected = """\
        tp=5, fp=4, fn=0, tn=0
        tp=5, fp=2, fn=0, tn=2
        tp=4, fp=1, fn=1, tn=3
        tp=2, fp=0, fn=3, tn=4
        tp=0, fp=0, fn=5, tn=4"""
        expected = ["BinaryConfusionMatrix(%s)" % line \
                    for line in dedent(expected).split("\n")]
        for (threshold, matrix), expected in \
                izip_longest(self.data.iter_confusion_matrices(4), expected):
            self.assertEqual(repr(matrix), expected)
            self.assertEqual(matrix, self.data.get_confusion_matrix(threshold))
Example #11
0
def tableify(data, headers=None):
    from itertools import izip_longest
    '''ASCII table for a list of strings'''
    if not isinstance(data, list):
        return ''

    table = []

    columns = izip_longest(*data, fillvalue='')
    widths = []
    for column in columns:
        new_row = []
        max_len = len(max(column, key=len))
        max_len = max(max_len, Colors.red*2)
        widths.append(max_len)
        for item in column:
            new_row.append(item.ljust(max_len, ' '))
        table.append(new_row)

    table = izip_longest(*table)

    result = []
    result.append('+' + '+'.join(['-' * (width+2) for width in widths]) + '+')
    result.append('| ' + ' | '.join(table.next()) + ' |')
    result.append('+' + '+'.join(['-' * (width+2) for width in widths]) + '+')

    for row in table:
        result.append('| ' + ' | '.join(row) + ' |')

    result.append('+' + '+'.join(['-' * (width+2) for width in widths]) + '+')

    return result
Example #12
0
def compare_cluster_conf(conf1, conf2):
    """
    return: list of differences between conf1 and conf2 in the form of tuple(key, v1, v2)
    """

    diffs = []

    cluster_name = conf1['clusterName']
    diffs.extend(compare_dict(conf1['clusterData'], conf2['clusterData']))

    # Gateway
    for x, y in itertools.izip_longest(conf1['gwList'], conf2['gwList']):
        diffs.extend(map(lambda t: ('%s.gw.%d.%s' % (cluster_name, x['gwId'], t[0]), t[1], t[2]), 
            compare_dict(x, y)))

    # PG
    for x, y in itertools.izip_longest(conf1['pgList'], conf2['pgList']):
        diffs.extend(map(lambda t: ('%s.pg.%d.%s' % (cluster_name, x['pgId'], t[0]), t[1], t[2]), 
            compare_dict(x, y, ['master_Gen_Map', 'pgs_ID_List'])))

    # PGS
    for x, y in itertools.izip_longest(conf1['pgsList'], conf2['pgsList']):
        diffs.extend(map(lambda t: ('%s.pgs.%d.%s' % (cluster_name, x['pgsId'], t[0]), t[1], t[2]), 
            compare_dict(x, y, ['stateTimestamp', 'smr_Role', 'old_SMR_Role', 'master_Gen'])))

    # Gateway lookup
    for x, y in itertools.izip_longest(conf1['gwLookup'], conf2['gwLookup']):
        diffs.extend(map(lambda t: ('%s.gwLookup.%d.%s' % (cluster_name, x['gwId'], t[0]), t[1], t[2]), 
            compare_dict(x, y)))

    return diffs
Example #13
0
def _recursive_etree(dictionary):
    """
    Helper function to create a list of etree elements from a dict.
    """
    from collections import OrderedDict
    import itertools

    contents = []
    for key, value in dictionary.iteritems():
        if key == "relaxation_parameter_list":
            # Wild iterator to arrive at the desired etree. If somebody else
            # ever reads this just look at the output and do it some other
            # way...
            contents.append(E.relaxation_parameter_list(
                *[getattr(E, i[0])(str(i[1][1]), number=str(i[1][0]))
                  for i in itertools.chain(*itertools.izip(
                      itertools.izip_longest(
                          [], enumerate(value["tau"]),  fillvalue="tau"),
                      itertools.izip_longest(
                          [], enumerate(value["w"]),  fillvalue="w")))]
            ))
            continue
        if isinstance(value, OrderedDict):
            contents.append(getattr(E, key)(*_recursive_etree(value)))
            continue
        if value is True:
            value = "true"
        elif value is False:
            value = "false"
        contents.append(getattr(E, key)(str(value)))
    return contents
Example #14
0
 def writerow(self, row, sep=None, *args, **kwargs):
     """
     Write General Data row to file
     """
     if not self.has_header:  # Write basic header for raw data
         self.file.write(self.encode.write_header() + '\n')
         self.has_header = True
     if sep is None:
         sep = self.default_sep
     if sep not in [',', ';', '|', ':']:
         raise AdtmError('Invalid Row Value Seperator ("{0:s}") provided, '
                         'must be ( "," | ";" | "|" | ":" )'.format(sep))
     self.offset += 1
     if self.offset - self.last_headerrow == 5*5:
         self.file.write("#" + sep.join((string.center(h, w) for h, w in
                                         izip_longest(self.headers,
                                                      self.col_width,
                                                      fillvalue=0)))[1:]
                         + "\n")
         self.last_headerrow = self.offset
         self.last_seprow = self.offset
         self.offset += 1
     elif self.offset - self.last_seprow == 5:
         self.file.write("#" + sep.join((string.center(" ", w) for w in
                                         self.col_width))[1:]
                         + "\n")
         self.last_seprow = self.offset
         self.offset += 1
     self.file.write(sep.join((string.center(self.encode(i), w) for i, w in
                               izip_longest(row,
                                            self.col_width,
                                            fillvalue=0)))
                     + "\n")
Example #15
0
def sent_message(sender, **kwargs):
    msg = kwargs["message"]
    resp = kwargs["response"]
    
    for recipient in (
        list(izip_longest(msg["To"].split(","), [], fillvalue='to')) +
        list(izip_longest(msg.get("Cc", "").split(","), [], fillvalue='cc')) +
        list(izip_longest(msg.get("Bcc", "").split(","), [], fillvalue='bcc'))):
        
        if not recipient[0]:
            continue
        
        timestamp, tz = resp["SubmittedAt"].rsplit("+", 1)
        tz_offset = int(tz.split(":", 1)[0])
        tz = timezone("Etc/GMT%s%d" % ("+" if tz_offset >= 0 else "-", tz_offset))
        submitted_at = tz.localize(datetime.strptime(timestamp[:26], POSTMARK_DATETIME_STRING)).astimezone(pytz.utc)
        
        
        emsg = EmailMessage(
            message_id=resp["MessageID"],
            submitted_at=submitted_at,
            status=resp["Message"],
            to=recipient[0],
            to_type=recipient[1],
            sender=msg["From"],
            reply_to=msg.get("ReplyTo", ""),
            subject=msg["Subject"],
            tag=msg.get("Tag", ""),
            text_body=msg["TextBody"],
            html_body=msg.get("HtmlBody", ""),
            headers=msg.get("Headers", ""),
            attachments=msg.get("Attachments", "")
        )
        emsg.save()
Example #16
0
def sent_message(sender, **kwargs):
    msg = kwargs["message"]
    resp = kwargs["response"]

    for recipient in (
        list(izip_longest(msg["To"].split(","), [], fillvalue="to"))
        + list(izip_longest(msg.get("Cc", "").split(","), [], fillvalue="cc"))
        + list(izip_longest(msg.get("Bcc", "").split(","), [], fillvalue="bcc"))
    ):

        if not recipient[0]:
            continue

        emsg = EmailMessage(
            message_id=resp["MessageID"],
            submitted_at=iso8601.parse_date(resp["SubmittedAt"]),
            status=resp["Message"],
            to=recipient[0],
            to_type=recipient[1],
            sender=msg["From"],
            reply_to=msg.get("ReplyTo", ""),
            subject=msg["Subject"],
            tag=msg.get("Tag", ""),
            text_body=msg["TextBody"],
            html_body=msg.get("HtmlBody", ""),
            headers=msg.get("Headers", ""),
            attachments=msg.get("Attachments", ""),
        )
        emsg.save()
Example #17
0
    def merge(self, other):
        if self == other:
            return self
        if self.shape == other.shape and self.shape != UNDEFINED and self.name == other.name and not self.dependent and not other.dependent and not self.has_missing and not other.has_missing:
            return self
        redim_cache = self._getRedimCache()
        key = (self,other)
        if not key in redim_cache:
            if(self.shape == UNDEFINED or other.shape == UNDEFINED) or self.shape != other.shape:
                rshape = UNDEFINED
                ndep = tuple([ldep or rdep for ldep, rdep in itertools.izip_longest(self.dependent, other.dependent,fillvalue=False)])
                if not ndep:
                    ndep = (True,) #FIXME, maybe more than 1 dependent dimension?
            else:
                rshape = self.shape
                ndep = tuple([ldep or rdep for ldep, rdep in itertools.izip_longest(self.dependent, other.dependent,fillvalue=False)])

            if(self.name == other.name):
                nname = self.name
            else:
                nname = self.name + "_" + other.name
            
            ndim = Dim(rshape, ndep, self.has_missing or other.has_missing, name=nname)
            redim_cache[key] = ndim
        return redim_cache[key]
Example #18
0
def test_vtx_iterator():
    #use vanilla izip as we"ll test using non-equal-length iterators
    izip = itertools.izip

    sm = Mesh(structured=True,
             structured_coords =[range(10,15), range(21,25), range(31,34)])
    it = sm.structured_set.iterate(iBase.Type.vertex, iMesh.Topology.point)

    # test the default order
    for (it_x, sm_x) in itertools.izip_longest(it, 
                                           sm.structured_iterate_vertex("zyx")):
        assert_equal(it_x,sm_x)

    # Do the same again, but use an arbitrary kwarg to structured_iterate_vertex 
    # to prevent optimization from kicking in
    it.reset()
    for (it_x, sm_x) in itertools.izip_longest(it,
                            sm.structured_iterate_vertex("zyx", no_opt=True)):
        assert_equal(it_x,sm_x)

    it.reset()
    for (it_x, sm_x) in izip(it, 
                               sm.structured_iterate_vertex("yx",z=sm.dims[2])):
        assert_equal(it_x,sm_x)

    it.reset()
    for (it_x, sm_x) in izip(it, sm.structured_iterate_vertex("x")):
        assert_equal(it_x,sm_x)
Example #19
0
 def diff(self, n):
     """Print the differences between the two files
     """
     if os.path.isfile(n[:-4]):
         diff1 = Utils().read_file(n[:-4]).splitlines()
     if os.path.isfile(n):
         diff2 = Utils().read_file(n).splitlines()
     lines, l, c = [], 0, 0
     for a, b in itertools.izip_longest(diff1, diff2):
         l += 1
         if a != b:
             for s1, s2 in itertools.izip_longest(str(a), str(b)):
                 c += 1
                 if s1 != s2:
                     break
             print("@@ -{0},{1} +{2},{3} @@\n".format(l, c, l, c))
             for line in lines[-3:]:
                 print("{0}".format(line))
             if a is None:
                 a = ""
             print("{0}{1}{2}{3}".format(self.red, "-", self.endc, a))
             if b is None:
                 b = ""
             print("{0}{1}{2}{3}".format(self.green, "+", self.endc, b))
             lines = []
             c = 0
         else:
             lines.append(a)
Example #20
0
def compare_zone_conf(servers, zone_conf1, zone_conf2):
    """
    return: list of differences between zone_conf1 and zone_conf2 in the form of tuple(key, v1, v2)
    """

    diffs = []

    # Confmaster
    for x, y in itertools.izip_longest(zone_conf1['cm']['leader'], zone_conf2['cm']['leader']):
        diffs.extend(map(lambda t: ('confmaster.leader.%s' % t[0], t[1], t[2]), compare_dict(x, y)))
    for x, y in itertools.izip_longest(zone_conf1['cm']['follower'], zone_conf2['cm']['follower']):
        diffs.extend(map(lambda t: ('confmaster.follower.%s' % t[0], t[1], t[2]), compare_dict(x, y)))
    for x, y in itertools.izip_longest(zone_conf1['cm']['heartbeat'], zone_conf2['cm']['heartbeat']):
        diffs.extend(map(lambda t: ('confmaster.heartbeat.%s' % t[0], t[1], t[2]), compare_dict(x, y)))

    # Cluster
    if len(zone_conf1['cluster']) != len(zone_conf2['cluster']):
        return [('cluster.count', len(zone_conf1['cluster']), len(zone_conf2['cluster']))]

    conf1 = sorted(zone_conf1['cluster'], lambda x: x['clusterName'])
    conf2 = sorted(zone_conf2['cluster'], lambda x: x['clusterName'])

    for i in xrange(len(conf1)):
        diffs.extend(compare_cluster_conf(conf1[i], conf2[i]))

    # PM
    if len(zone_conf1['pm']) != len(zone_conf2['pm']):
        return [('pm.count', len(zone_conf1['pm']), len(zone_conf2['pm']))]

    for i in xrange(len(zone_conf1['pm'])):
        diffs.extend(compare_pm_conf(zone_conf1['pm'][i], zone_conf2['pm'][i]))

    return diffs
Example #21
0
def sent_message(sender, **kwargs):
    msg = kwargs["message"]
    resp = kwargs["response"]
    
    for recipient in (
        list(izip_longest(msg["To"].split(","), [], fillvalue='to')) +
        list(izip_longest(msg.get("Cc", "").split(","), [], fillvalue='cc')) +
        list(izip_longest(msg.get("Bcc", "").split(","), [], fillvalue='bcc'))):
        
        if not recipient[0]:
            continue

        submitted_at = dateutil.parser.parse(resp["SubmittedAt"]).astimezone(pytz.utc)

        if POSTMARK_USE_TZ == False:
            submitted_at = submitted_at.replace(tzinfo=None)
        
        emsg = EmailMessage(
            message_id=resp["MessageID"],
            submitted_at=submitted_at,
            status=resp["Message"],
            to=recipient[0],
            to_type=recipient[1],
            sender=msg["From"],
            reply_to=msg.get("ReplyTo", ""),
            subject=msg["Subject"],
            tag=msg.get("Tag", ""),
            text_body=msg["TextBody"],
            html_body=msg.get("HtmlBody", ""),
            headers=msg.get("Headers", ""),
            attachments=msg.get("Attachments", "")
        )
        emsg.save()
Example #22
0
def zip_fn(srs):
    a=srs.pop()
    if type(a) in [ListType,StringType]:
        b=srs.pop()
        srs.push(map(list,[filter(lambda x:x is not None,zlist) for zlist in itertools.izip_longest(a,b)]))
    else:
        lists = [srs.pop() for i in range(a)]
        srs.push(map(list,[filter(lambda x:x is not None,zlist) for zlist in itertools.izip_longest(*lists)]))
Example #23
0
 def iter_by_weekdays(self):
     week1, week2 = AppendedWeeks(
         Subject.objects.iter_by_weekdays(week=1, semester=self.semester),
         Subject.objects.iter_by_weekdays(week=2, semester=self.semester)
         ).get()
     result = [list(izip_longest(w1, w2)) for w1, w2 in
               list(izip_longest(week1, week2))]
     return zip(WEEKDAYS, result)
Example #24
0
def student(request,dept_code):
    try:
        dept = Department.objects.get(dept_code = dept_code)
        context_dict['Department'] = dept
    except:
        raise Http404
    try:
        btech = Student.objects.filter(dept = dept_code, degree = 1).values('year_of_admission')
        idd = Student.objects.filter(dept = dept_code, degree = 2).values('year_of_admission')

        btech_years = list()
        idd_years = list()
        
        for i in btech: btech_years.append(i['year_of_admission'])
        for i in idd: idd_years.append(i['year_of_admission'])
        
        btech_years = sorted(list(OrderedDict.fromkeys(btech_years)),reverse=True)
        idd_years = sorted(list(OrderedDict.fromkeys(idd_years)),reverse=True)
        
        students_list_btech = list()
        counter = 0
        for i in btech_years:
            students_list_btech.append(Student.objects.filter(dept = dept_code, degree = 1, year_of_admission = btech_years[counter]).order_by('roll_no'))
            counter += 1

        students_list_idd = list()
        counter = 0 
        for i in idd_years: 
            students_list_idd.append(Student.objects.filter(dept = dept_code, degree = 2, year_of_admission = idd_years[counter]).order_by('roll_no'))
            counter += 1

        headings_btech  = [ "B.Tech Part - I",
                            "B.Tech Part - II",
                            "B.Tech Part - III",
                            "B.Tech Part - IV" ]
        headings_idd    = [ "IDD Part - I",
                            "IDD Part - II",
                            "IDD Part - III",
                            "IDD Part - IV",
                            "IDD Part - V" ]

        #Every value in each counter needs to be different.
        counter1 = [1,2,3,4]
        counter2 = [11,22,33,44,55]
        full_list_btech = izip_longest(  
                                counter1,
                                headings_btech,
                                students_list_btech  )
        full_list_idd   = izip_longest(  
                                counter2,
                                headings_idd,
                                students_list_idd  )
        context_dict['full_list_btech'] = full_list_btech
        context_dict['full_list_idd'] = full_list_idd

    except:
        raise Http404
    return render(request,'student.html',context_dict)
Example #25
0
 def init_queue(self, specs):
     sections = OrderedDict()
     for s in specs:
         if s.section_id not in sections:
             sections[s.section_id] = []
         sections[s.section_id].append(s)
     specs = [s for section_specs in izip_longest(*sections.values()) for s in section_specs if s]
     jobs = [[RunnerJob(s) for _ in xrange(s.number_of_iterations)] for s in specs]
     self.job_queue = [j for spec_jobs in izip_longest(*jobs) for j in spec_jobs if j]
def filt_func(c1, c2):
    print "c1,c2",c1,c2,c1[0] > c2[0]
    if c1[0] > c2[0]:
        return False
    print "c1,c2,izip",sorted(c1),sorted(c2),izip_longest(sorted(c1), sorted(c2))
    for a in izip_longest(sorted(c1), sorted(c2)): print a
    R = reduce(lambda x, y: x and (y[0] < y[1]), izip_longest(sorted(c1), sorted(c2)))  #, True)
    print "R", R
    return not R
Example #27
0
def generate_files_for_editing():
    my_files = [f for f in os.listdir(u'./QA_files') if re.search(u'^Chapter\d+_data\.csv$', f)]
    chapters = []
    for f in my_files:
        cnumber = int(re.search(u'^Chapter(\d+)_data\.csv$', f).group(1))
        with open(u'./QA_files/{}'.format(f)) as fpointer:
            cfile = CSVChapter(fpointer, cnumber)
        chapters.append(cfile)
    chapters.sort(key=lambda x: x.number)
    start, end = -1, -1
    rows = []
    for chapter in chapters:
        if chapter.number % 50 == 1:
            start = chapter.number
        for num, (en_seg, he_seg) in enumerate(izip_longest(chapter.english_segments, chapter.hebrew_segments, fillvalue=u''), 1):
            rows.append({
                u'Chapter': chapter.number,
                u'Segment #': num,
                u'English': en_seg,
                u'Hebrew': he_seg
            })
        if chapter.number % 50 == 0 or chapter.number == chapters[-1].number:
            end = chapter.number
            with open('./manual_editing/Likutei_Moharan_{}-{}.csv'.format(start, end), 'w') as fp:
                writer = unicodecsv.DictWriter(fp, [u'Chapter', u'Segment #', u'English', u'Hebrew'])
                writer.writeheader()
                writer.writerows(rows)
            rows = []

    my_files = [f for f in os.listdir(u'./QA_files') if re.search(u'Part2_Chapter\d+_data\.csv$', f)]
    chapters = []
    for f in my_files:
        cnumber = int(re.search(u'^Part2_Chapter(\d+)_data\.csv$', f).group(1))
        with open(u'./QA_files/{}'.format(f)) as fpointer:
            cfile = CSVChapter(fpointer, cnumber, part2=True)
        chapters.append(cfile)
    chapters.sort(key=lambda x: x.number)
    start, end = -1, -1
    rows = []
    for chapter in chapters:
        if chapter.number % 50 == 1:
            start = chapter.number
        for num, (en_seg, he_seg) in enumerate(
                izip_longest(chapter.english_segments, chapter.hebrew_segments, fillvalue=u''), 1):
            rows.append({
                u'Chapter': chapter.number,
                u'Segment #': num,
                u'English': en_seg,
                u'Hebrew': he_seg
            })
        if chapter.number % 50 == 0 or chapter.number == chapters[-1].number:
            end = chapter.number
            with open('./manual_editing/Likutei_Moharan_Part2_{}-{}.csv'.format(start, end), 'w') as fp:
                writer = unicodecsv.DictWriter(fp, [u'Chapter', u'Segment #', u'English', u'Hebrew'])
                writer.writeheader()
                writer.writerows(rows)
            rows = []
 def msgSplit(self, msg, keywordLen):  
     # separate the message into tuples of every keywordLen letters 
     split = itertools.izip_longest(*[iter(list(msg))]*keywordLen)
     # transpose the result into columns
     transposed = map(list, itertools.izip_longest(*split))
     
     # if the message was uneven, the above method of splitting and 
     # transposing will add None values to even out the columns
     # the below filter will remove those None values
     return [filter(lambda a: a is not None, column) for column in transposed]
def check_fragments(oligo_file, design_fasta):
    design_aa_list = []
    with open(design_fasta, 'r') as f:
        for pdb, seq in izip_longest(f, f, fillvalue=None):
            if '4AC0' and 'B0' in pdb:
                block = seq[77:117]
            elif '4AC0' and 'B1' in pdb:
                block = seq[99:138]
            elif '2uxo' and 'B0' in pdb:
                block = seq[62:100]
            elif '2uxo' and 'B1' in pdb:
                block = seq[136:176]
            else:
                raise Exception('Unrecognized design name')
            design_aa_list.append(block)

    fragment_list = []
    with open(oligo_file, 'r') as o:
        for pdb, seq in izip_longest(o, o, fillvalue=None):
            if '4AC0' and 'B0' in pdb:
                seq_lower = seq.lower()
                seq_no_5p = seq_lower.split('gtgacccgtccctgggtctcaagat')[1]
                fragment = seq_no_5p.split('gccttgagaccgggcagaggtcgac')[0]
            elif '4AC0' and 'B1' in pdb:
                seq_lower = seq.lower()
                seq_no_5p = seq_lower.split('tgcccgctgtcttcaggtctcaagta')[1]
                fragment = seq_no_5p.split('catttgagacctgtagcccggcagtg')[0]
            elif '2uxo' and 'B0' in pdb:
                seq_lower = seq.lower()
                seq_no_5p = seq_lower.split('cgatcgtgcccacctggtctccactg')[1]
                fragment = seq_no_5p.split('gttctgagaccagttggagcccgcac')[0]
            elif '2uxo' and 'B1' in pdb:
                seq_lower = seq.lower()
                seq_no_5p = seq_lower.split('ctggtgcgtcgtctggtctctggat')[1]
                fragment = seq_no_5p.split('cgttggagaccggcgaacacttccc')[0]
            else:
                raise Exception('Unrecognized oligo name')
            fragment_list.append(fragment)

    missing_list = []
    for item in fragment_list:
        aa_fragment = Seq.translate(item)
        if aa_fragment in design_aa_list:
            design_aa_list.remove(aa_fragment)
        else:
            missing_list.append(aa_fragment)
    if missing_list:
        sys.stderr.write('Error: The following oligo sequences do not match a design amino acid sequence\n')
        for miss in missing_list:
            sys.stderr.write('{0}\n'.format(miss))
    if design_aa_list:
        sys.stderr.write('Error: The following design sequences do not match an oligo sequence\n')
        for design in design_aa_list:
            sys.stderr.write('{0}\n'.format(design))
    sys.stdout.write('done\n')
Example #30
0
def tableify(data, headers=None):
    '''ASCII table for a list of strings

    Args:
        data (list of lists): List of lists for each row of data
        headers (list) optional: List of column names

    Return:
        None
    '''
    from itertools import izip_longest

    if not isinstance(data, list):
        return ''
    if headers and not isinstance(headers, list):
        headers = [headers]
    if headers:
        data = [headers] + data

    # Example color in order to pad for colors
    red = "\x1b[31m"
    table = []

    columns = []
    for curr_row in data:
        x = izip_longest(*[wrap_string(item) for item in curr_row], fillvalue='')
        columns.extend(list(x))

    columns = izip_longest(*columns, fillvalue='')
    widths = []
    for column in columns:
        new_row = []
        # Need to be a string in order to get length
        # column = [str(item) for item in column]
        max_len = len(max(column, key=len))
        max_len = max(max_len, len(red)*2)
        widths.append(max_len)
        for item in column:
            new_row.append(item.ljust(max_len, ' '))
        table.append(new_row)

    table = izip_longest(*table)

    result = []
    result.append('+' + '+'.join(['-' * (width+2) for width in widths]) + '+')
    result.append('| ' + ' | '.join(table.next()) + ' |')
    result.append('+' + '+'.join(['-' * (width+2) for width in widths]) + '+')

    for row in table:
        result.append('| ' + ' | '.join(row) + ' |')

    result.append('+' + '+'.join(['-' * (width+2) for width in widths]) + '+')

    for line in result:
        print line
 def set_pan_lights(self, lights):
     for strip, light in izip_longest(self._channel_strips, lights or []):
         strip.pan_light.set_control_element(light)
 def set_solo_buttons(self, buttons):
     for strip, button in izip_longest(self._channel_strips, buttons or []):
         if button:
             button.set_on_off_values('Mixer.SoloOn', 'Mixer.SoloOff')
         strip.set_solo_button(button)
Example #33
0
 def test_order(iter1, iter2):
     for i1, i2 in itertools.izip_longest(iter1, iter2):
         assert_equal(i1, i2)
Example #34
0
 def addStrings2(self, num1, num2):
     z = itertools.izip_longest(num1[::-1], num2[::-1], fillvalue='0')
     return (i[0] for i in z)
Example #35
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 izip_longest(fillvalue=fillvalue, *args)
 def compareVersion5(self, version1, version2):
     splits = (map(int, v.split('.')) for v in (version1, version2))
     return cmp(*zip(*itertools.izip_longest(*splits, fillvalue=0)))
Example #37
0
    def allocate_ip_address(self,
                            context,
                            new_addresses,
                            net_id,
                            port_id,
                            reuse_after,
                            segment_id=None,
                            version=None,
                            ip_addresses=None,
                            subnets=None,
                            **kwargs):
        elevated = context.elevated()
        subnets = subnets or []
        ip_addresses = ip_addresses or []

        ipam_log = kwargs.get('ipam_log', None)
        LOG.info("Starting a new IP address(es) allocation. Strategy "
                 "is {0} - [{1}]".format(
                     self.get_name(),
                     utils.pretty_kwargs(network_id=net_id,
                                         port_id=port_id,
                                         new_addresses=new_addresses,
                                         ip_addresses=ip_addresses,
                                         subnets=subnets,
                                         segment_id=segment_id,
                                         version=version)))

        def _try_reallocate_ip_address(ipam_log, ip_addr=None):
            new_addresses.extend(
                self.attempt_to_reallocate_ip(context,
                                              net_id,
                                              port_id,
                                              reuse_after,
                                              version=version,
                                              ip_address=ip_addr,
                                              segment_id=segment_id,
                                              subnets=subnets,
                                              **kwargs))

        def _try_allocate_ip_address(ipam_log, ip_addr=None, sub=None):
            for retry in xrange(CONF.QUARK.ip_address_retry_max):
                attempt = None
                if ipam_log:
                    attempt = ipam_log.make_entry("_try_allocate_ip_address")
                LOG.info("Allocating new IP attempt {0} of {1}".format(
                    retry + 1, CONF.QUARK.ip_address_retry_max))
                if not sub:
                    subnets = self._choose_available_subnet(
                        elevated,
                        net_id,
                        version,
                        segment_id=segment_id,
                        ip_address=ip_addr,
                        reallocated_ips=new_addresses)
                else:
                    subnets = [
                        self.select_subnet(context,
                                           net_id,
                                           ip_addr,
                                           segment_id,
                                           subnet_ids=[sub])
                    ]
                LOG.info("Subnet selection returned {0} viable subnet(s) - "
                         "IDs: {1}".format(
                             len(subnets),
                             ", ".join([str(s["id"]) for s in subnets if s])))

                try:
                    self._allocate_ips_from_subnets(context, new_addresses,
                                                    net_id, subnets, port_id,
                                                    reuse_after, ip_addr,
                                                    **kwargs)
                except q_exc.IPAddressRetryableFailure:
                    LOG.exception("Error in allocating IP")
                    if attempt:
                        LOG.debug("ATTEMPT FAILED")
                        attempt.failed()
                    remaining = CONF.QUARK.ip_address_retry_max - retry - 1
                    if remaining > 0:
                        LOG.info("{0} retries remain, retrying...".format(
                            remaining))
                    else:
                        LOG.info("No retries remaing, bailing")
                    continue
                finally:
                    if attempt:
                        attempt.end()

                break

        ip_addresses = [
            netaddr.IPAddress(ip_address) for ip_address in ip_addresses
        ]

        if ip_addresses:
            for ip_address in ip_addresses:
                _try_reallocate_ip_address(ipam_log, ip_address)
        else:
            _try_reallocate_ip_address(ipam_log)

        if self.is_strategy_satisfied(new_addresses):
            return
        else:
            LOG.info("Reallocated addresses {0} but still need more addresses "
                     "to satisfy strategy {1}. Falling back to creating "
                     "IPs".format(new_addresses, self.get_name()))

        if ip_addresses or subnets:
            for ip_address, subnet in itertools.izip_longest(
                    ip_addresses, subnets):
                _try_allocate_ip_address(ipam_log, ip_address, subnet)
        else:
            _try_allocate_ip_address(ipam_log)

        if self.is_strategy_satisfied(new_addresses, allocate_complete=True):
            self._notify_new_addresses(context, new_addresses)
            LOG.info(
                "IPAM for port ID {0} completed with addresses "
                "{1}".format(port_id,
                             [a["address_readable"] for a in new_addresses]))
            return
        ipam_log.failed()

        raise ip_address_failure(net_id)
Example #38
0
 def __eq__(self, other):
     if isinstance(other, Sequence):
         return all(a == b for (
             a,
             b) in it.izip_longest(self, other, fillvalue=fluid.UNDEFINED))
     return NotImplemented
Example #39
0
        return


#*********************************************************************************************
if __name__ == "__main__":

    arg_names = [
        'me', 'LOG_DIR', 'python_test_name', 'cloudShutdownIsError',
        'sandboxIgnoreErrors', 'verbose'
    ]
    # https://docs.python.org/2/library/itertools.html
    # Nice..Learning itertools stuff:
    # izip_longest: Make an iterator that aggregates elements from each of the iterables.
    # If the iterables are of uneven length, missing values are filled-in with fillvalue.
    # Iteration continues until the longest iterable is exhausted.
    args = dict(itertools.izip_longest(arg_names, sys.argv))
    # if you're running this from the command line, remove any existing doneToLine markers
    if not args['LOG_DIR']:
        LOG_DIR = './sandbox'
    else:
        LOG_DIR = args['LOG_DIR']

    if os.path.exists(LOG_DIR):
        print "Checking for any marker files to remove first.. (multi-test cloud log scrape uses and we always leave the droppings)"
        for f in glob.glob(LOG_DIR + '/*doneToLine*'):
            print "cleaning marker file:", f
            os.remove(f)

    # if you call from the command line, we'll just pass the first two positionally.
    # here's a low budget argsparse :) (args are optional!)
    errorMessage = check_sandbox_for_errors(
 def set_volume_controls(self, controls):
     for control, channel_strip in izip_longest(controls or [],
                                                self._channel_strips):
         if channel_strip:
             channel_strip.set_volume_control(control)
Example #41
0
#!/usr/bin/env python

import sys

from itertools import izip_longest

args = [iter([int(x) for x in line.split()] for line in sys.stdin)] * 3

print sum(
    sum(y[0] + y[1] > y[2] for y in [sorted(tri) for tri in zip(*x)])
    for x in izip_longest(*args))
Example #42
0
def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ['ABC', 'DEF', 'Gxx']"
    args = [iter(iterable)] * n
    return [''.join(x) for x in izip_longest(fillvalue=fillvalue, *args)]
Example #43
0
 def _calc_maxes(self):
     array = [self.header] + self.rows + [self.footer]
     return [
         max(self._unicode_width(s) for s in ss)
         for ss in izip_longest(*array, fillvalue='')
     ]
Example #44
0
import itertools

dna = 'CTGAAATGTTCCGCGAGCCGAACCGATTCACCGCCTAGAAACGTATTGTGCTGGTGTGCGGCGGTTAGAGATATTAGGTAGCGCCGTTACTCTAACATTTCGAATCAACCTTTCAGGGGAGTCACTGCCATCGTAAGTAGAGTACTTAGCATCGATGGCCATGCCTACTAATTACAGGCTGAATGACACTAAACCTTAGTTCACTGACCCGTTTTGTCATGTACTCTTGTGGTATGGGTCTTCAAATTGATCTGATTGGGAAGATAGAAAAACGGCTCTATCCTGGGTCGAGCCTCCCATGAAGCAGTCAAGGGGCCGCGAGGACTTCGATACTTGCCCTGCTCGAGCACATTTTAAAGCTTATTCCACATACTAGACTTACCCCCCGGCGTGTCGTACTGGAAGGTTAAACCTCTTGAGTTGATCTGACAACCTAGACGCGTGCCACGTTGTGTGGGATAGGTCACTCTCATTTCCACGAGGGACCAGAACCTTTGGCAATCCAGTTATTCTGCACTCGTGGCCGCCTCTCCTGGCAGGGGACCGGTAAGTTTGCGTATTCGCCGGGGAGTGGAGACGGATCGTCGTACACTGTTTCGAAAATTTTTGAGGATGGAGAGCAGAGCTATTGGATAAACGCTTGTACAGGTTCAATACTATTAGCAACGTGCCACCGGCACAGCTATCTCTGTTTCGCATGAAAGAGCCGTTAATCACGACGTTTAATCGAAACACATACCGATGGTCTACGAATATTATATCCGATACTAAGTCGGCCGCCGCAGTCCAGACGCCATATCGCTTTGAAGACCCCAAGGCGAACATTAACCGGTACGAGCAACTGCGGAGTGCCCTGCAATAGTCCGTCTGTAAAGGGCCCAGGCTAGGGCAAATAGTCCCTAAAACTAGAGATGGTCAACCGCTATGTGGGGCATTCTCCGTGAGACTCAGCCGTATTACAGTGAGCGTATTCCCAAACTCCCCTTCTGTGTATGACCAGTGTCGCTGCAAATGGACCGAGCAG'
codons = ['A', 'T', 'C', 'G']
p = list(itertools.permutations(['00', '01', '10', '11']))


def decode_binary_string(s):
    return ''.join(chr(int(s[i * 8:i * 8 + 8], 2)) for i in range(len(s) // 8))


f = open('bins.txt', 'wb')

for i in p:
    k = list(itertools.izip_longest(codons, i))
    print k
    cp = '%s' % dna
    for (c, r) in k:
        cp = cp.replace(c, r)
    print cp
    f.write(cp)
    f.write('\n')

f.close()
Example #45
0
    def _ast_to_dictsql(self, input_ast):
        """
        """
        # Add implicit AND operator between expressions if there is no explicit
        # operator specified.
        ast = []
        for token, lookahead in izip_longest(input_ast, input_ast[1:]):
            if token.getName() == "boolean":
                # only add boolean operator if it is NOT the last token
                if lookahead is not None:
                    ast.append(token)
                continue
            else:
                # add non-boolean token
                ast.append(token)
                # if next token is boolean, continue so it can be added
                if lookahead is None or lookahead.getName() == "boolean":
                    continue
                # if next token is NOT a boolean, add implicit AND
                ast.append(ParseResults('and', 'boolean'))

        # dictSql stack
        dss = {'operator': None, 'val1': None, 'val2': None}
        success = True
        dse = None
        for part, lookahead in izip_longest(ast, ast[1:]):
            self._logger.debug("part: %s %s" % (part, part.getName()))

            # handle operators joining together expressions
            if part.getName() == 'boolean':
                op = part[0].lower()
                dss['operator'] = op
                dss['interpretation'] = {
                    'interpretation': op,
                    'operator': op,
                    'error': False
                }
                continue

            # string expr that we expand to dictsql expression
            elif part.getName() == 'expression':
                if part.operator in self.match_operators:
                    tmp_success, dse = self._parse_expr(part)
                    success = success and tmp_success
                else:
                    tmp_success, dse = self._ast_to_dictsql(part)
                    success = success and tmp_success
            elif part.getName() == 'nested':
                tmp_success, dse = self._ast_to_dictsql(part)
                success = success and tmp_success
            elif part.getName() in ('ipv6_prefix', 'ipv6_address', 'word',
                                    'tag', 'vrf_rt', 'quoted_string'):
                # dict sql expression
                dse = self._string_to_dictsql(part)
                self._logger.debug('string part: %s  => %s' % (part, dse))
            else:
                raise ParserError("Unhandled part in AST: %s %s" %
                                  (part, part.getName()))

            if dss['val1'] is None:
                self._logger.debug('val1 not set, using dse: %s' %
                                   unicode(dse))
                dss['val1'] = dse
            else:
                self._logger.debug(
                    "val1 is set, operator is '%s', val2 = dst: %s" %
                    (dss['operator'], unicode(dse)))
                dss['val2'] = dse

            if lookahead is not None:
                if dss['val1'] is not None and dss['val2'] is not None:
                    dss = {'operator': None, 'val1': dss, 'val2': None}

        # special handling when AST is only one expression, then we overwrite
        # the dss with dse
        if len(ast) == 1:
            dss = dse
        if len(ast) == 0:
            dss = self._string_to_dictsql(ParseResults('', 'word'))

        # return the final composed stack of dictsql expressions
        return success, dss
Example #46
0
    def seek(self, offset, whence=None, partition=None):
        """
        Alter the current offset in the consumer, similar to fseek

        Arguments:
            offset: how much to modify the offset
            whence: where to modify it from, default is None

                * None is an absolute offset
                * 0    is relative to the earliest available offset (head)
                * 1    is relative to the current offset
                * 2    is relative to the latest known offset (tail)

            partition: modify which partition, default is None.
                If partition is None, would modify all partitions.
        """

        if whence is None: # set an absolute offset
            if partition is None:
                for tmp_partition in self.offsets:
                    self.offsets[tmp_partition] = offset
            else:
                self.offsets[partition] = offset
        elif whence == 1:  # relative to current position
            if partition is None:
                for tmp_partition, _offset in self.offsets.items():
                    self.offsets[tmp_partition] = _offset + offset
            else:
                self.offsets[partition] += offset
        elif whence in (0, 2):  # relative to beginning or end
            reqs = []
            deltas = {}
            if partition is None:
                # divide the request offset by number of partitions,
                # distribute the remained evenly
                (delta, rem) = divmod(offset, len(self.offsets))
                for tmp_partition, r in izip_longest(self.offsets.keys(),
                                                     repeat(1, rem),
                                                     fillvalue=0):
                    deltas[tmp_partition] = delta + r

                for tmp_partition in self.offsets.keys():
                    if whence == 0:
                        reqs.append(OffsetRequestPayload(self.topic, tmp_partition, -2, 1))
                    elif whence == 2:
                        reqs.append(OffsetRequestPayload(self.topic, tmp_partition, -1, 1))
                    else:
                        pass
            else:
                deltas[partition] = offset
                if whence == 0:
                    reqs.append(OffsetRequestPayload(self.topic, partition, -2, 1))
                elif whence == 2:
                    reqs.append(OffsetRequestPayload(self.topic, partition, -1, 1))
                else:
                    pass

            resps = self.client.send_offset_request(reqs)
            for resp in resps:
                self.offsets[resp.partition] = \
                    resp.offsets[0] + deltas[resp.partition]
        else:
            raise ValueError('Unexpected value for `whence`, %d' % (whence,))

        # Reset queue and fetch offsets since they are invalid
        self.fetch_offsets = self.offsets.copy()
        self.count_since_commit += 1
        if self.auto_commit:
            self.commit()

        self.queue = queue.Queue()
Example #47
0
def zeroPadding(l, fillvalue=PAD_token):
    return list(itertools.izip_longest(*l, fillvalue=fillvalue))
Example #48
0
#!coding=utf-8
"""
    同时迭代多个序列
"""

# 想同时迭代多个序列,每次分别从一个序列中取一个元素。
# 可以使用zip()函数
xpts = [1, 5, 4, 2, 10, 7]
ypts = [101, 78, 37, 15, 62, 99]
for x, y in zip(xpts, ypts):  # 迭代的长度和最短序列长度一致
    print x, y

# 如果不像迭代的长度和最短序列一致,可以使用itertools.zip_longest()
from itertools import izip_longest
a = [1, 2, 3]
b = ['w', 'x', 'y', 'z']
for i in izip_longest(a, b, fillvalue=0):  # 默认填充的是Nonne
    print i

# 可以使用zip()打包生成一个字典
headers = ['name', 'shares', 'price']
values = ['ACME', 100, 490.1]
s = dict(zip(headers, values))
print s

# 虽然不常见,但是 zip() 可以接受多于两个的序列的参数。 这时候所生成的
# 结果元组中元素个数跟输入序列个数一样。

# 最后强调一点就是, zip() 会创建一个迭代器来作为结果返回。 如果你需要将
# 结对的值存储在列表中,要使用 list() 函数
Example #49
0
 def __iter__(self):
     return itertools.izip_longest(self.X, self.y)
def grouper(n, iterable, padvalue=None):
	"""grouper(3, 'abcdefg', 'x') -->
	('a','b','c'), ('d','e','f'), ('g','x','x')"""
	return izip_longest(*[iter(iterable)]*n, fillvalue=padvalue)
Example #51
0
def grouper(iterable, n, fillvalue=None):
    args = [iter(iterable)] * n
    return izip_longest(*args, fillvalue=fillvalue)
 def grouper(iterable, n, fillvalue=None):
     args = [iter(iterable)] * n
     return (filter(None, values)
             for values in izip_longest(fillvalue=fillvalue, *args))
Example #53
0
 def test_equal(ijk_list, miter):
     for ijk, i in itertools.izip_longest(ijk_list, miter):
         assert_equal(sm.structured_get_hex(*ijk), i)
def handle_data():
    string_to_search = request.form['searchstring']
    file_path = request.form['filepath']
    x_percentage = request.form['xpercentage']
    column_numbers = request.form['column_numbers']
    column_numbers = map(int, column_numbers.split(','))
    x_percentage=float(x_percentage)
    c=0
    corpus=[]

    response = urllib2.urlopen(file_path)

    reader = csv.reader(response, dialect=csv.excel_tab, skipinitialspace=True, delimiter=',', quotechar='"')
    header =[]
    for row in reader:
        
        st=''
        
        if c!=0:
                if  row[column_numbers[0]].startswith('INC') :
                    i=0
                    while( i< len(column_numbers)):
                        st = st + row[column_numbers[i]]+';;;' 
                        i +=1
                    corpus.append(st)
        else:
            header = row            

        c = c+1  


    
        
    st = string_to_search
    corpus.insert(0,st) 

    # Removing special characters 
    new_header=[]
    for i in column_numbers:
        element = header[i]
        new_header.append(element)
    lower_case_corpus=[]
    for sentence in corpus:
        lower_case_corpus.append(sentence.lower())
  
    corpus_without_stopwords = [] 
    i=0
    l=len(lower_case_corpus)
    while i < l:
        lower_case_corpus[i] = lower_case_corpus[i].encode('utf-8')
        i+=1

    corpus_without_punctuation = remove_punctuation(lower_case_corpus)
    
    corpus_without_stopwords, my_stop_words = remove_stop_words(corpus_without_punctuation)
    my_stop_words = set(my_stop_words)
    # corpus_without_stopwords is corpus without punctuation also
    # corpus_without_stopwords is Array Of Sets
    # Add to every set their own 2 and 3 every subsets.
    new_corpus_without_stopwords = []

    for word, word_with_stopwords in itertools.izip_longest( corpus_without_stopwords, corpus_without_punctuation ):
        # s in notebook is now word in our proj
        

        #lower_case_corpus is a list of strings
        #and word_with_stopwords is a string
        word_to_string = word_with_stopwords   
        # Replacing punctuation marks with whitespace

        #word_to_string is a string    
        
        BagOf2words = ConsecutiveBagOfWords(word_to_string, 2)
        BagOf3words = ConsecutiveBagOfWords(word_to_string, 3)
        BagOf2words =  '-' + BagOf2words
        BagOf3words =  '-' + BagOf3words
        
        pat = r'(?<=\-).+?(?=\-)' 
        Array2 = re.findall(pat, BagOf2words)
        Array3 = re.findall(pat, BagOf3words)

        for word2,word3 in itertools.izip_longest(Array2,Array3):
            
            if word2 !=None:
                word2=word2.strip()
                setWord2 = set(word2.split(' '))
                if len(setWord2 & my_stop_words) == 0:
                    word.append(word2)

            if word3 !=None and len(word_to_string.split(' '))!=2:    
                word3 = word3.strip()
                setWord3 = set(word3.split(' '))
                if len(setWord3 & my_stop_words) == 0:
                    word.append(word3)
                    
        new_corpus_without_stopwords.append(word)
           
    if not new_corpus_without_stopwords[0]:
        return render_template('NotFound.html')
    
    values =  BagOfWords(new_corpus_without_stopwords)
    
    final_output_dict = FinalOutput(values, corpus, x_percentage, st )  

    dict_copy_list = list(final_output_dict)
    dict_new_copy_list = []
    for item in dict_copy_list:
        arr = item.split(';;;')
        dict_new_copy_list.append(arr)
    if not dict_copy_list:
        return render_template('NotFound.html')

    else:
        return render_template('print.html',dict_copy_list = dict_new_copy_list,st=st,header = new_header,column_numbers=column_numbers)
Example #55
0
def line_pairs(lines):
  return itertools.izip_longest(
      lines, itertools.islice(lines, 1, None), fillvalue=None)
Example #56
0
def grouper(size, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * size
    return itertools.izip_longest(fillvalue=fillvalue, *args)
 def set_track_select_buttons(self, buttons):
     for strip, button in izip_longest(self._channel_strips, buttons or []):
         if button:
             button.set_on_off_values('Mixer.TrackSelected',
                                      'Mixer.TrackUnselected')
         strip.set_select_button(button)
Example #58
0
def object_info(**kw):
    """Make an object info dict with all fields present."""
    infodict = dict(izip_longest(info_fields, [None]))
    infodict.update(kw)
    return infodict
Example #59
0
 def _get_printable_row(self, row, maxes):
     return '| ' + \
            ' | '.join([self._format(r, m)
                        for r, m in izip_longest(row, maxes, fillvalue='')]) + ' |'
Example #60
0
def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)