python类namedtuple()的实例源码

csv_reader.py 文件源码 项目:hmv-s16 作者: cmuphyscomp 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _read_data(self, stream, verbose = False):
        """Process frame data rows from the CSV stream."""

        # Note that the frame_num indices do not necessarily start from zero,
        # but the setter functions assume that the array indices do.  This
        # implementation just ignores the original frame numbers, the frames are
        # renumbered from zero.
        for row_num, row in enumerate(stream):
            frame_num = int(row[0])
            frame_t   = float(row[1])
            values    = row[2:]

            # if verbose: print "Processing row_num %d, frame_num %d, time %f." % (row_num, frame_num, frame_t)

            # add the new frame time to each object storing a trajectory
            for body in self.rigid_bodies.values():
                body._add_frame(frame_t)

            # process the columns of interest
            for mapping in self._column_map:
                # each mapping is a namedtuple with a setter method, column index, and axis name
                mapping.setter( row_num, mapping.axis, values[mapping.column] )

    # ================================================================
edit.py 文件源码 项目:notex 作者: adiultra 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def paragraph(self):
        """Return the index within self.text of the current paragraph and of
        the current line and current character (number of characters since the
        start of the paragraph) within the paragraph

        Returns: namedtuple (para_index, line_index, char_index)

        """
        idx_para = idx_buffer = idx_line = idx_char = 0
        done = False
        for para in self.text:
            for idx_line, line in enumerate(para):
                if idx_buffer == self.buffer_idx_y:
                    done = True
                    break
                idx_buffer += 1
            if done is True:
                break
            idx_para += 1
        idx_char = sum(map(len, self.text[idx_para][:idx_line])) + \
            self.buffer_idx_x
        p = namedtuple("para", ['para_index', 'line_index', 'char_index'])
        return p(idx_para, idx_line, idx_char)
test_csv_reader.py 文件源码 项目:pytablereader 作者: thombashi 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_exception_invalid_csv(self):
        table_text = """nan = float("nan")
inf = float("inf")
TEST_TABLE_NAME = "test_table"
TEST_DB_NAME = "test_db"
NOT_EXIT_FILE_PATH = "/not/existing/file/__path__"

NamedTuple = namedtuple("NamedTuple", "attr_a attr_b")
NamedTupleEx = namedtuple("NamedTupleEx", "attr_a attr_b attr_c")
"""
        loader = ptr.CsvTableTextLoader(table_text)
        loader.table_name = "dummy"

        with pytest.raises(ptr.InvalidDataError):
            for _tabletuple in loader.load():
                pass
hookenv.py 文件源码 项目:charm-nova-cloud-controller 作者: openstack 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def iter_units_for_relation_name(relation_name):
    """Iterate through all units in a relation

    Generator that iterates through all the units in a relation and yields
    a named tuple with rid and unit field names.

    Usage:
    data = [(u.rid, u.unit)
            for u in iter_units_for_relation_name(relation_name)]

    :param relation_name: string relation name
    :yield: Named Tuple with rid and unit field names
    """
    RelatedUnit = namedtuple('RelatedUnit', 'rid, unit')
    for rid in relation_ids(relation_name):
        for unit in related_units(rid):
            yield RelatedUnit(rid, unit)
httputil.py 文件源码 项目:noc-orchestrator 作者: DirceuSilvaLabs 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def parse_request_start_line(line):
    """Returns a (method, path, version) tuple for an HTTP 1.x request line.

    The response is a `collections.namedtuple`.

    >>> parse_request_start_line("GET /foo HTTP/1.1")
    RequestStartLine(method='GET', path='/foo', version='HTTP/1.1')
    """
    try:
        method, path, version = line.split(" ")
    except ValueError:
        raise HTTPInputError("Malformed HTTP request line")
    if not re.match(r"^HTTP/1\.[0-9]$", version):
        raise HTTPInputError(
            "Malformed HTTP version in HTTP Request-Line: %r" % version)
    return RequestStartLine(method, path, version)
httputil.py 文件源码 项目:noc-orchestrator 作者: DirceuSilvaLabs 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def parse_response_start_line(line):
    """Returns a (version, code, reason) tuple for an HTTP 1.x response line.

    The response is a `collections.namedtuple`.

    >>> parse_response_start_line("HTTP/1.1 200 OK")
    ResponseStartLine(version='HTTP/1.1', code=200, reason='OK')
    """
    line = native_str(line)
    match = re.match("(HTTP/1.[0-9]) ([0-9]+) ([^\r]*)", line)
    if not match:
        raise HTTPInputError("Error parsing response start line")
    return ResponseStartLine(match.group(1), int(match.group(2)),
                             match.group(3))

# _parseparam and _parse_header are copied and modified from python2.7's cgi.py
# The original 2.7 version of this code did not correctly support some
# combinations of semicolons and double quotes.
# It has also been modified to support valueless parameters as seen in
# websocket extension negotiations.
httputil.py 文件源码 项目:noc-orchestrator 作者: DirceuSilvaLabs 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def parse_request_start_line(line):
    """Returns a (method, path, version) tuple for an HTTP 1.x request line.

    The response is a `collections.namedtuple`.

    >>> parse_request_start_line("GET /foo HTTP/1.1")
    RequestStartLine(method='GET', path='/foo', version='HTTP/1.1')
    """
    try:
        method, path, version = line.split(" ")
    except ValueError:
        raise HTTPInputError("Malformed HTTP request line")
    if not re.match(r"^HTTP/1\.[0-9]$", version):
        raise HTTPInputError(
            "Malformed HTTP version in HTTP Request-Line: %r" % version)
    return RequestStartLine(method, path, version)
httputil.py 文件源码 项目:noc-orchestrator 作者: DirceuSilvaLabs 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def parse_response_start_line(line):
    """Returns a (version, code, reason) tuple for an HTTP 1.x response line.

    The response is a `collections.namedtuple`.

    >>> parse_response_start_line("HTTP/1.1 200 OK")
    ResponseStartLine(version='HTTP/1.1', code=200, reason='OK')
    """
    line = native_str(line)
    match = re.match("(HTTP/1.[0-9]) ([0-9]+) ([^\r]*)", line)
    if not match:
        raise HTTPInputError("Error parsing response start line")
    return ResponseStartLine(match.group(1), int(match.group(2)),
                             match.group(3))

# _parseparam and _parse_header are copied and modified from python2.7's cgi.py
# The original 2.7 version of this code did not correctly support some
# combinations of semicolons and double quotes.
# It has also been modified to support valueless parameters as seen in
# websocket extension negotiations.
httputil.py 文件源码 项目:noc-orchestrator 作者: DirceuSilvaLabs 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def parse_response_start_line(line):
    """Returns a (version, code, reason) tuple for an HTTP 1.x response line.

    The response is a `collections.namedtuple`.

    >>> parse_response_start_line("HTTP/1.1 200 OK")
    ResponseStartLine(version='HTTP/1.1', code=200, reason='OK')
    """
    line = native_str(line)
    match = re.match("(HTTP/1.[0-9]) ([0-9]+) ([^\r]*)", line)
    if not match:
        raise HTTPInputError("Error parsing response start line")
    return ResponseStartLine(match.group(1), int(match.group(2)),
                             match.group(3))

# _parseparam and _parse_header are copied and modified from python2.7's cgi.py
# The original 2.7 version of this code did not correctly support some
# combinations of semicolons and double quotes.
# It has also been modified to support valueless parameters as seen in
# websocket extension negotiations.
test_plugins.py 文件源码 项目:sauna 作者: NicolasLM 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_get_network_data(self, time_mock, sleep_mock):
        time_mock.side_effect = [1, 2]

        Counter = namedtuple('Counter',
                             ['bytes_sent', 'bytes_recv', 'packets_sent',
                              'packets_recv'])

        first_counter = Counter(bytes_sent=54000, bytes_recv=12000,
                                packets_sent=50, packets_recv=100)
        second_counter = Counter(bytes_sent=108000, bytes_recv=36000,
                                 packets_sent=75, packets_recv=150)

        m = mock.Mock()
        m.side_effect = [
            {'eth0': first_counter}, {'eth0': second_counter}
        ]

        self.network.psutil.net_io_counters = m
        kb_ul, kb_dl, p_ul, p_dl = self.network.get_network_data(
            interface='eth0', delay=1)
        self.assertEqual(kb_ul, 54000)
        self.assertEqual(kb_dl, 24000)
        self.assertEqual(p_ul, 25)
        self.assertEqual(p_dl, 50)
dsb_a_eliasq6_mal2_s5_p8a1_all.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient,) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((None,))

    l = load_pretrained_model(l_in_rshp)

    #ins = penultimate_layer.output_shape[1]
    # l = conv3d(penultimate_layer, ins, filter_size=3, stride=2)
    # #l = feat_red(l)
    #
    #
    # l = nn.layers.DropoutLayer(l)
    # #
    # l = nn.layers.DenseLayer(l, num_units=256, W=nn.init.Orthogonal(),
    #                          nonlinearity=nn.nonlinearities.rectify)

    #l = nn.layers.DropoutLayer(l)

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1))

    l_out = nn_lung.LogMeanExp(l,r=16, axis=(1, 2), name='LME')

    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
dsb_a_eliasx29_relias10_s5_p8a1.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient, ) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((batch_size,))

    penultimate_layer = load_pretrained_model(l_in_rshp)

    l = dense(penultimate_layer, 128, name='dense_final')

    l = nn.layers.DenseLayer(l, num_units=1, W=nn.init.Orthogonal(),
                             nonlinearity=nn.nonlinearities.sigmoid, name='dense_p_benign')

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1), name='reshape2patients')

    l_out = nn_lung.LogMeanExp(l, r=8, axis=(1, 2), name='LME')


    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
dsb_a_eliasx2_c3_s2_p8a1.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient, 1,) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((batch_size,))

    penultimate_layer = load_pretrained_model(l_in_rshp)

    l = drop(penultimate_layer, name='drop_final2')

    l = dense(l, 256, name='dense_final1')

    l = drop(l, name='drop_final2')

    l = dense(l, 256, name='dense_final2')

    l = nn.layers.DenseLayer(l, num_units=1, W=nn.init.Orthogonal(),
                             nonlinearity=None, name='dense_p_benign')

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1), name='reshape2patients')

    l_out = nn_lung.AggAllBenignExp(l, name='aggregate_all_nodules_benign')

    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
dsb_a_eliasx1_c3_s2_p8a1.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient, 1,) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((batch_size,))

    penultimate_layer = load_pretrained_model(l_in_rshp)

    l = drop(penultimate_layer, name='drop_final')

    l = dense(l, 128, name='dense_final')

    l = nn.layers.DenseLayer(l, num_units=1, W=nn.init.Orthogonal(),
                             nonlinearity=None, name='dense_p_benign')

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1), name='reshape2patients')

    l_out = nn_lung.AggAllBenignExp(l, name='aggregate_all_nodules_benign')

    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
dsb_a_eliasq1_mal2_s5_p8a1.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient,) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((batch_size,))

    l = load_pretrained_model(l_in_rshp)

    #ins = penultimate_layer.output_shape[1]
    # l = conv3d(penultimate_layer, ins, filter_size=3, stride=2)
    # #l = feat_red(l)
    #
    #
    # l = nn.layers.DropoutLayer(l)
    # #
    # l = nn.layers.DenseLayer(l, num_units=256, W=nn.init.Orthogonal(),
    #                          nonlinearity=nn.nonlinearities.rectify)

    #l = nn.layers.DropoutLayer(l)

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1))

    l_out = nn_lung.LogMeanExp(l,r=16, axis=(1, 2), name='LME')

    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
dsb_a_eliasx27_relias10_s5_p8a1.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient, ) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((batch_size,))

    penultimate_layer = load_pretrained_model(l_in_rshp)

    l = dense(penultimate_layer, 128, name='dense_final')

    l = nn.layers.DenseLayer(l, num_units=1, W=nn.init.Orthogonal(),
                             nonlinearity=nn.nonlinearities.sigmoid, name='dense_p_benign')

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1), name='reshape2patients')

    l_out = nn_lung.LogMeanExp(l, r=8, axis=(1, 2), name='LME')


    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
dsb_a_eliasx28_relias10_s5_p8a1.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient, ) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((batch_size,))

    penultimate_layer = load_pretrained_model(l_in_rshp)

    l = dense(penultimate_layer, 128, name='dense_final')

    l = nn.layers.DenseLayer(l, num_units=1, W=nn.init.Orthogonal(),
                             nonlinearity=nn.nonlinearities.sigmoid, name='dense_p_benign')

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1), name='reshape2patients')

    l_out = nn_lung.LogMeanExp(l, r=8, axis=(1, 2), name='LME')


    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
dsb_a_eliasx36_relias18_s5_p8a1.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient, ) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((batch_size,))

    penultimate_layer = load_pretrained_model(l_in_rshp)

    l = nn.layers.DenseLayer(penultimate_layer, num_units=1, W=nn.init.Orthogonal(),
                             nonlinearity=nn.nonlinearities.sigmoid, name='dense_p_benign')

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1), name='reshape2patients')

    l_out = nn_lung.LogMeanExp(l, r=8, axis=(1, 2), name='LME')


    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)
dsb_a_eliasx35_relias18_s5_p8a1.py 文件源码 项目:dsb3 作者: EliasVansteenkiste 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def build_model():
    l_in = nn.layers.InputLayer((None, n_candidates_per_patient, ) + p_transform['patch_size'])
    l_in_rshp = nn.layers.ReshapeLayer(l_in, (-1, 1,) + p_transform['patch_size'])
    l_target = nn.layers.InputLayer((batch_size,))

    penultimate_layer = load_pretrained_model(l_in_rshp)

    l = nn.layers.DenseLayer(penultimate_layer, num_units=1, W=nn.init.Orthogonal(),
                             nonlinearity=nn.nonlinearities.sigmoid, name='dense_p_benign')

    l = nn.layers.ReshapeLayer(l, (-1, n_candidates_per_patient, 1), name='reshape2patients')

    l_out = nn_lung.LogMeanExp(l, r=8, axis=(1, 2), name='LME')


    return namedtuple('Model', ['l_in', 'l_out', 'l_target'])(l_in, l_out, l_target)


问题


面经


文章

微信
公众号

扫码关注公众号