本文整理汇总了Python中collections.namedtuple方法的典型用法代码示例。如果您正苦于以下问题:Python collections.namedtuple方法的具体用法?Python collections.namedtuple怎么用?Python collections.namedtuple使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections
的用法示例。
在下文中一共展示了collections.namedtuple方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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)
示例2: __init__
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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
示例3: test_construct_namedtuple
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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}
示例4: test_forward_types
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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)
示例5: read_names
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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)
示例6: _optimize_clone
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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
示例7: create_loss
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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
示例8: test_decodes_example_proto
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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)
示例9: _reset
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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()
示例10: as_namedtuple
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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()))
示例11: get_inventory_slots
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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
示例12: next
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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
示例13: prev
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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
示例14: test_tuple_command_handler
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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')
示例15: test_index_tuple_command_handler
# 需要导入模块: import collections [as 别名]
# 或者: from collections import namedtuple [as 别名]
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')