Python collections.namedtuple() Examples

The following are 30 code examples of collections.namedtuple(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module collections , or try the search function .
Example #1
Source File: features.py    From NGU-scripts with GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_inventory_slots(slots :int) -> None:
        """Get coords for inventory slots from 1 to slots."""
        point = namedtuple("p", ("x", "y"))
        i = 1
        row = 1
        x_pos, y_pos = coords.INVENTORY_SLOTS
        res = []
        
        while i <= slots:
            x = x_pos + (i - (12 * (row - 1))) * 50
            y = y_pos + ((row - 1) * 50)
            res.append(point(x, y))
            if i % 12 == 0:
                row += 1
            i += 1
        return res 
Example #2
Source File: submissions.py    From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, datastore_client, storage_client, round_name):
    """Initializes CompetitionSubmissions.

    Args:
      datastore_client: instance of CompetitionDatastoreClient
      storage_client: instance of CompetitionStorageClient
      round_name: name of the round
    """
    self._datastore_client = datastore_client
    self._storage_client = storage_client
    self._round_name = round_name
    # each of the variables is a dictionary,
    # where key - submission ID
    # value - SubmissionDescriptor namedtuple
    self._attacks = None
    self._targeted_attacks = None
    self._defenses = None 
Example #3
Source File: test_yaml.py    From python-clean-architecture with MIT License 6 votes vote down vote up
def test_construct_namedtuple():
    """Original Loader has a problem of building an object which state is set
    by __new__, instead of __init__.
    """
    from collections import namedtuple

    class FooClass(serialization.yaml.yaml.YAMLObject, namedtuple('Foo', "x, y")):
        yaml_tag = 'foo'
        yaml_constructor = serialization.CustomYamlLoader

        def __setstate__(self, data):
            self.data = data

    contents = (
        "---\n"
        "foo: !<foo> {x: 1, y: 2}\n"
    )
    foo_object = serialization.load_yaml(contents)['foo']
    assert isinstance(foo_object, FooClass)
    assert foo_object.data == {'x': 1, 'y': 2} 
Example #4
Source File: poolmanager.py    From gist-alfred with MIT License 6 votes vote down vote up
def connection_from_pool_key(self, pool_key, request_context=None):
        """
        Get a :class:`ConnectionPool` based on the provided pool key.

        ``pool_key`` should be a namedtuple that only contains immutable
        objects. At a minimum it must have the ``scheme``, ``host``, and
        ``port`` fields.
        """
        with self.pools.lock:
            # If the scheme, host, or port doesn't match existing open
            # connections, open a new ConnectionPool.
            pool = self.pools.get(pool_key)
            if pool:
                return pool

            # Make a fresh ConnectionPool of the desired type
            scheme = request_context['scheme']
            host = request_context['host']
            port = request_context['port']
            pool = self._new_pool(scheme, host, port, request_context=request_context)
            self.pools[pool_key] = pool

        return pool 
Example #5
Source File: replay.py    From iSDX with Apache License 2.0 6 votes vote down vote up
def __init__(self, config, flows_dir, ports_dir, num_timesteps, debug=False):
        self.logger = logging.getLogger("LogHistory")
        if debug:
            self.logger.setLevel(logging.DEBUG)

        self.log_entry = namedtuple("LogEntry", "source destination type")
        self.ports = defaultdict(list)
        self.flows = defaultdict(list)

        self.data = defaultdict(lambda: defaultdict(lambda: defaultdict(int)))
        self.current_timestep = 0
        self.total_timesteps = num_timesteps

        self.parse_config(config)
        self.parse_logs(num_timesteps, flows_dir, ports_dir)
        self.info()

        pretty(self.data) 
Example #6
Source File: tests.py    From psmqtt with MIT License 6 votes vote down vote up
def test_index_tuple_command_handler(self):
        r = [namedtuple('test', 'a b')(1, 2), namedtuple('test', 'a b')(3, 4)]
        handler = type("TestHandler", (IndexTupleCommandHandler, object),
                       {"get_value": lambda s: r})('test')
        # normal execution
        self.assertEqual([1, 3], handler.handle('a/*'))
        self.assertEqual("[1, 3]", handler.handle('a/*;'))
        self.assertEqual(3, handler.handle('a/1'))
        self.assertEqual({'a': 3, 'b': 4}, handler.handle('*/1'))
        self.assertEqual('{"a": 3, "b": 4}', handler.handle('*;/1'))
        # exceptions
        self.assertRaises(Exception, handler.handle, '')
        self.assertRaises(Exception, handler.handle, '*')
        self.assertRaises(Exception, handler.handle, '*;')
        self.assertRaises(Exception, handler.handle, 'a')
        self.assertRaises(Exception, handler.handle, 'a/')
        self.assertRaises(Exception, handler.handle, '/')
        self.assertRaises(Exception, handler.handle, '*/')
        self.assertRaises(Exception, handler.handle, '/*')
        self.assertRaises(Exception, handler.handle, 'blabla')
        self.assertRaises(Exception, handler.handle, 'bla/bla')
        self.assertRaises(Exception, handler.handle, 'bla/')
        self.assertRaises(Exception, handler.handle, '/bla') 
Example #7
Source File: tests.py    From psmqtt with MIT License 6 votes vote down vote up
def test_tuple_command_handler(self):
        handler = type("TestHandler", (TupleCommandHandler, object),
                       {"get_value": lambda s: namedtuple('test', 'a b')(10, 20)})('test')
        # normal execution
        self.assertEqual(10, handler.handle('a'))
        self.assertEqual({'a': 10, 'b': 20}, handler.handle('*'))
        self.assertEqual('{"a": 10, "b": 20}', handler.handle('*;'))
        # exceptions
        self.assertRaises(Exception, handler.handle, '')
        self.assertRaises(Exception, handler.handle, '/')
        self.assertRaises(Exception, handler.handle, '*/')
        self.assertRaises(Exception, handler.handle, '/*')
        self.assertRaises(Exception, handler.handle, 'blabla')
        self.assertRaises(Exception, handler.handle, 'bla/bla')
        self.assertRaises(Exception, handler.handle, 'bla/')
        self.assertRaises(Exception, handler.handle, '/bla') 
Example #8
Source File: test_module.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_forward_types():
    #Test forward with other data batch API
    Batch = namedtuple('Batch', ['data'])
    data = mx.sym.Variable('data')
    out = data * 2
    mod = mx.mod.Module(symbol=out, label_names=None)
    mod.bind(data_shapes=[('data', (1, 10))])
    mod.init_params()
    data1 = [mx.nd.ones((1, 10))]
    mod.forward(Batch(data1))
    assert mod.get_outputs()[0].shape == (1, 10)
    data2 = [mx.nd.ones((3, 5))]
    mod.forward(Batch(data2))
    assert mod.get_outputs()[0].shape == (3, 5)

    #Test forward with other NDArray and np.ndarray inputs
    data = mx.sym.Variable('data')
    out = data * 2
    mod = mx.mod.Module(symbol=out, label_names=None)
    mod.bind(data_shapes=[('data', (1, 10))])
    mod.init_params()
    data1 = mx.nd.ones((1, 10))
    assert mod.predict(data1).shape == (1, 10)
    data2 = np.ones((1, 10))
    assert mod.predict(data1).shape == (1, 10) 
Example #9
Source File: data_utils.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def read_names(names_path):
    """read data from downloaded file. See SmallNames.txt for example format
    or go to https://www.kaggle.com/kaggle/us-baby-names for full lists

    Args:
        names_path: path to the csv file similar to the example type
    Returns:
        Dataset: a namedtuple of two elements: deduped names and their associated
            counts. The names contain only 26 chars and are all lower case
    """
    names_data = pd.read_csv(names_path)
    names_data.Name = names_data.Name.str.lower()

    name_data = names_data.groupby(by=["Name"])["Count"].sum()
    name_counts = np.array(name_data.tolist())
    names_deduped = np.array(name_data.index.tolist())

    Dataset = collections.namedtuple('Dataset', ['Name', 'Count'])
    return Dataset(names_deduped, name_counts) 
Example #10
Source File: model_deploy.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def _optimize_clone(optimizer, clone, num_clones, regularization_losses,
                    **kwargs):
  """Compute losses and gradients for a single clone.

  Args:
    optimizer: A tf.Optimizer  object.
    clone: A Clone namedtuple.
    num_clones: The number of clones being deployed.
    regularization_losses: Possibly empty list of regularization_losses
      to add to the clone losses.
    **kwargs: Dict of kwarg to pass to compute_gradients().

  Returns:
    A tuple (clone_loss, clone_grads_and_vars).
      - clone_loss: A tensor for the total loss for the clone.  Can be None.
      - clone_grads_and_vars: List of (gradient, variable) for the clone.
        Can be empty.
  """
  sum_loss = _gather_clone_loss(clone, num_clones, regularization_losses)
  clone_grad = None
  if sum_loss is not None:
    with tf.device(clone.device):
      clone_grad = optimizer.compute_gradients(sum_loss, **kwargs)
  return sum_loss, clone_grad 
Example #11
Source File: model.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def create_loss(self, data, endpoints):
    """Creates all losses required to train the model.

    Args:
      data: InputEndpoints namedtuple.
      endpoints: Model namedtuple.

    Returns:
      Total loss.
    """
    # NOTE: the return value of ModelLoss is not used directly for the
    # gradient computation because under the hood it calls slim.losses.AddLoss,
    # which registers the loss in an internal collection and later returns it
    # as part of GetTotalLoss. We need to use total loss because model may have
    # multiple losses including regularization losses.
    self.sequence_loss_fn(endpoints.chars_logit, data.labels)
    total_loss = slim.losses.get_total_loss()
    tf.summary.scalar('TotalLoss', total_loss)
    return total_loss 
Example #12
Source File: fsns_test.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def test_decodes_example_proto(self):
    expected_label = range(37)
    expected_image, encoded = unittest_utils.create_random_image(
        'PNG', shape=(150, 600, 3))
    serialized = unittest_utils.create_serialized_example({
        'image/encoded': [encoded],
        'image/format': ['PNG'],
        'image/class':
        expected_label,
        'image/unpadded_class':
        range(10),
        'image/text': ['Raw text'],
        'image/orig_width': [150],
        'image/width': [600]
    })

    decoder = fsns.get_split('train', dataset_dir()).decoder
    with self.test_session() as sess:
      data_tuple = collections.namedtuple('DecodedData', decoder.list_items())
      data = sess.run(data_tuple(*decoder.decode(serialized)))

    self.assertAllEqual(expected_image, data.image)
    self.assertAllEqual(expected_label, data.label)
    self.assertEqual(['Raw text'], data.text)
    self.assertEqual([1], data.num_of_views) 
Example #13
Source File: minitaur_reactive_env.py    From soccer-matlab with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _reset(self):
    # TODO(b/73666007): Use composition instead of inheritance.
    # (http://go/design-for-testability-no-inheritance).
    init_pose = MinitaurPose(
        swing_angle_1=INIT_SWING_POS,
        swing_angle_2=INIT_SWING_POS,
        swing_angle_3=INIT_SWING_POS,
        swing_angle_4=INIT_SWING_POS,
        extension_angle_1=INIT_EXTENSION_POS,
        extension_angle_2=INIT_EXTENSION_POS,
        extension_angle_3=INIT_EXTENSION_POS,
        extension_angle_4=INIT_EXTENSION_POS)
    # TODO(b/73734502): Refactor input of _convert_from_leg_model to namedtuple.
    initial_motor_angles = self._convert_from_leg_model(list(init_pose))
    super(MinitaurReactiveEnv, self)._reset(
        initial_motor_angles=initial_motor_angles, reset_duration=0.5)
    return self._get_observation() 
Example #14
Source File: leveldb.py    From leveldb-py with MIT License 6 votes vote down vote up
def prev(self):
        """Backs the iterator up one step. Also returns the current value prior
        to moving the iterator.

        @rtype: Row (namedtuple of key, value) if keys_only=False, otherwise
                string (the key)

        @raise StopIteration: if called on an iterator that is not valid
        """
        if not self.valid():
            raise StopIteration()
        if self._keys_only:
            rv = self.key()
        else:
            rv = Row(self.key(), self.value())
        self._impl.prev()
        return rv 
Example #15
Source File: leveldb.py    From leveldb-py with MIT License 6 votes vote down vote up
def next(self):
        """Advances the iterator one step. Also returns the current value prior
        to moving the iterator

        @rtype: Row (namedtuple of key, value) if keys_only=False, otherwise
                string (the key)

        @raise StopIteration: if called on an iterator that is not valid
        """
        if not self.valid():
            raise StopIteration()
        if self._keys_only:
            rv = self.key()
        else:
            rv = Row(self.key(), self.value())
        self._impl.next()
        return rv 
Example #16
Source File: __init__.py    From aws-ops-automator with Apache License 2.0 6 votes vote down vote up
def as_namedtuple(name, d, deep=True, name_func=None, excludes=None):
    name_func = name_func if name_func is not None else tuple_name_func

    if not isinstance(d, dict) or getattr(d, "keys") is None:
        return d

    if excludes is None:
        excludes = []

    dest = {}

    if deep:
        # deep copy to avoid modifications on input dictionaries
        for key in list(d.keys()):
            key_name = name_func(key)
            if is_dict(d[key]) and key not in excludes:
                dest[key_name] = as_namedtuple(key, d[key], deep=True, name_func=name_func, excludes=excludes)
            elif is_array(d[key]) and key not in excludes:
                dest[key_name] = [as_namedtuple(key, i, deep=True, name_func=name_func, excludes=excludes) for i in d[key]]
            else:
                dest[key_name] = d[key]
    else:
        dest = {name_func(key): d[key] for key in list(d.keys())}

    return collections.namedtuple(name_func(name), list(dest.keys()))(*list(dest.values())) 
Example #17
Source File: replay_memory.py    From tensortrade with Apache License 2.0 5 votes vote down vote up
def head(self, batch_size) -> List[namedtuple]:
        return self.memory[:batch_size] 
Example #18
Source File: pipeline_common_test.py    From gcp-variant-transforms with Apache License 2.0 5 votes vote down vote up
def test_get_mode_small(self):
    args = self._create_mock_args(input_pattern='*', input_file=None)
    match_result = collections.namedtuple('MatchResult', ['metadata_list'])
    match = match_result([None for _ in range(100)])

    with mock.patch.object(FileSystems, 'match', return_value=[match]):
      self.assertEqual(self._get_pipeline_mode(args), PipelineModes.SMALL) 
Example #19
Source File: pipeline_common_test.py    From gcp-variant-transforms with Apache License 2.0 5 votes vote down vote up
def _create_mock_args(self, **args):
    return collections.namedtuple('MockArgs', args.keys())(*args.values()) 
Example #20
Source File: bq_to_vcf_test.py    From gcp-variant-transforms with Apache License 2.0 5 votes vote down vote up
def _create_mock_args(self, **args):
    return collections.namedtuple('MockArgs', args.keys())(*args.values()) 
Example #21
Source File: vcf_to_bq_test.py    From gcp-variant-transforms with Apache License 2.0 5 votes vote down vote up
def _create_mock_args(self, **args):
    return collections.namedtuple('MockArgs', args.keys())(*args.values()) 
Example #22
Source File: replay_memory.py    From tensortrade with Apache License 2.0 5 votes vote down vote up
def tail(self, batch_size) -> List[namedtuple]:
        return self.memory[-batch_size:] 
Example #23
Source File: tests.py    From psmqtt with MIT License 5 votes vote down vote up
def test_index_or_total_tuple_command_handler(self):
        total = namedtuple('test', 'a b')(10, 20)
        single = [namedtuple('test', 'a b')(1, 2), namedtuple('test', 'a b')(3, 4)]
        handler = type("TestHandler", (IndexOrTotalTupleCommandHandler, object),
                       {"get_value": lambda s, t: total if t else single})('test')
        # normal execution
        self.assertEqual({'a': 10, 'b': 20}, handler.handle('*'))
        self.assertEqual('{"a": 10, "b": 20}', handler.handle('*;'))
        self.assertEqual(10, handler.handle('a'))
        self.assertEqual([1, 3], handler.handle('a/*'))
        self.assertEqual("[1, 3]", handler.handle('a/*;'))
        self.assertEqual(3, handler.handle('a/1'))
        self.assertEqual({'a': 3, 'b': 4}, handler.handle('*/1'))
        self.assertEqual('{"a": 3, "b": 4}', handler.handle('*;/1'))
        # exceptions
        self.assertRaisesRegexp(Exception, "Element '' in '' is not supported", handler.handle, '')
        self.assertRaises(Exception, handler.handle, '/')
        self.assertRaisesRegexp(Exception, "Cannot list all elements and parameters at the same.*", handler.handle, '*/*')
        self.assertRaises(Exception, handler.handle, '*-')
        self.assertRaises(Exception, handler.handle, '/*')
        self.assertRaises(Exception, handler.handle, '3')
        self.assertRaises(Exception, handler.handle, '/3')
        self.assertRaises(Exception, handler.handle, 'blabla')
        self.assertRaises(Exception, handler.handle, 'bla/bla')
        self.assertRaises(Exception, handler.handle, 'bla/')
        self.assertRaises(Exception, handler.handle, '/bla')
        self.assertRaises(Exception, handler.handle, '*/5') 
Example #24
Source File: report.py    From zun with Apache License 2.0 5 votes vote down vote up
def get_provider_traits(self, context, rp_uuid):
        """Queries the placement API for a resource provider's traits.

        :param context: The security context
        :param rp_uuid: UUID of the resource provider to grab traits for.
        :return: A namedtuple comprising:
                    * .traits: A set() of string trait names, which may be
                      empty if the specified provider has no traits.
                    * .generation: The resource provider generation.
        :raise: ResourceProviderTraitRetrievalFailed on errors.  In particular,
                we raise this exception (as opposed to returning None or the
                empty set()) if the specified resource provider does not exist.
        :raise: keystoneauth1.exceptions.ClientException if placement API
                communication fails.
        """
        resp = self.get("/resource_providers/%s/traits" % rp_uuid,
                        version='1.6', global_request_id=context.global_id)

        if resp.status_code == 200:
            json = resp.json()
            return TraitInfo(traits=set(json['traits']),
                             generation=json['resource_provider_generation'])

        placement_req_id = get_placement_request_id(resp)
        LOG.error(
            "[%(placement_req_id)s] Failed to retrieve traits from "
            "placement API for resource provider with UUID %(uuid)s. Got "
            "%(status_code)d: %(err_text)s.",
            {'placement_req_id': placement_req_id, 'uuid': rp_uuid,
             'status_code': resp.status_code, 'err_text': resp.text})
        raise exception.ResourceProviderTraitRetrievalFailed(uuid=rp_uuid) 
Example #25
Source File: pipeline_common_test.py    From gcp-variant-transforms with Apache License 2.0 5 votes vote down vote up
def _create_mock_args(self, **args):
    return collections.namedtuple('MockArgs', args.keys())(*args.values()) 
Example #26
Source File: pipeline_common_test.py    From gcp-variant-transforms with Apache License 2.0 5 votes vote down vote up
def test_get_mode_small_still_large(self):
    with temp_dir.TempDir() as tempdir:
      filename = tempdir.create_temp_file(lines=self.SAMPLE_LINES)
      args = self._create_mock_args(input_pattern=None, input_file=filename)
      match_result = collections.namedtuple('MatchResult', ['metadata_list'])

      match = match_result([None for _ in range(100)])
      with mock.patch.object(FileSystems, 'match', return_value=[match]):
        self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE) 
Example #27
Source File: pipeline_common_test.py    From gcp-variant-transforms with Apache License 2.0 5 votes vote down vote up
def test_get_mode_large(self):
    with temp_dir.TempDir() as tempdir:
      filename = tempdir.create_temp_file(lines=self.SAMPLE_LINES)
      args = self._create_mock_args(input_pattern=None, input_file=filename)
      match_result = collections.namedtuple('MatchResult', ['metadata_list'])

      match = match_result(range(50001))
      with mock.patch.object(FileSystems, 'match', return_value=[match]):
        self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE)

      matches = [match_result(range(25000)),
                 match_result(range(25000)),
                 match_result(range(1))]
      with mock.patch.object(FileSystems, 'match', return_value=matches):
        self.assertEqual(self._get_pipeline_mode(args), PipelineModes.LARGE) 
Example #28
Source File: trajGen.py    From quadcopter-simulation with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def genLine(t):
    v_max = 2.0;
    a_max = 2.0;
    yaw = 0.0;
    yawdot = 0.0;
    # 1x3 matrix x,y,z
    initial_pos = np.zeros(3)
    acc = np.zeros(3)
    vel = np.zeros(3)

    # accelarate
    if t <= v_max / a_max:
        dt = t
        acc[2] = a_max
        vel = acc * dt
        pos = 0.5 * acc * dt**2
    # constant velocity
    elif t <= 2 * v_max / a_max:
        dt = t - v_max / a_max
        vel[2] = v_max
        pos = np.array([0, 0, v_max**2 / (2 * a_max)]) + np.array([0, 0, v_max * dt])
    # decelarate
    elif t <= 3 * v_max / a_max:
        dt = t - 2 * v_max / a_max
        acc[2] = -a_max
        vel = np.array([0, 0, v_max]) + acc * dt
        pos = np.array([0, 0, 3 * v_max**2 / (2 * a_max)]) + np.array([0, 0, v_max]) * dt + 0.5 * acc * dt**2
    # hover
    else:
        pos = np.array([0, 0, 2 * v_max**2 / a_max])

    pos += initial_pos
    DesiredState = namedtuple('DesiredState', 'pos vel acc yaw yawdot')
    return DesiredState(pos, vel, acc, yaw, yawdot) 
Example #29
Source File: sina.py    From backtrader-cn with GNU General Public License v3.0 5 votes vote down vote up
def _json_object_hook(d):
    class_name = d.pop('_class_name', 'NamedTuple')
    return namedtuple(class_name, d.keys())(*d.values()) 
Example #30
Source File: pipeline_common_test.py    From gcp-variant-transforms with Apache License 2.0 5 votes vote down vote up
def test_get_mode_medium(self):
    args = self._create_mock_args(input_pattern='*', input_file=None)
    match_result = collections.namedtuple('MatchResult', ['metadata_list'])

    match = match_result(range(101))
    with mock.patch.object(FileSystems, 'match', return_value=[match]):
      self.assertEqual(self._get_pipeline_mode(args), PipelineModes.MEDIUM)

    match = match_result(range(50000))
    with mock.patch.object(FileSystems, 'match', return_value=[match]):
      self.assertEqual(self._get_pipeline_mode(args), PipelineModes.MEDIUM)