本文整理匯總了Python中collections.Sized方法的典型用法代碼示例。如果您正苦於以下問題:Python collections.Sized方法的具體用法?Python collections.Sized怎麽用?Python collections.Sized使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類collections
的用法示例。
在下文中一共展示了collections.Sized方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_Sized
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def test_Sized(self):
non_samples = [None, 42, 3.14, 1j,
(lambda: (yield))(),
(x for x in []),
]
for x in non_samples:
self.assertNotIsInstance(x, Sized)
self.assertFalse(issubclass(type(x), Sized), repr(type(x)))
samples = [str(),
tuple(), list(), set(), frozenset(), dict(),
dict().keys(), dict().items(), dict().values(),
]
for x in samples:
self.assertIsInstance(x, Sized)
self.assertTrue(issubclass(type(x), Sized), repr(type(x)))
self.validate_abstract_methods(Sized, '__len__')
self.validate_isinstance(Sized, '__len__')
示例2: test_abc_registry
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def test_abc_registry(self):
d = dict(a=1)
self.assertIsInstance(d.viewkeys(), collections.KeysView)
self.assertIsInstance(d.viewkeys(), collections.MappingView)
self.assertIsInstance(d.viewkeys(), collections.Set)
self.assertIsInstance(d.viewkeys(), collections.Sized)
self.assertIsInstance(d.viewkeys(), collections.Iterable)
self.assertIsInstance(d.viewkeys(), collections.Container)
self.assertIsInstance(d.viewvalues(), collections.ValuesView)
self.assertIsInstance(d.viewvalues(), collections.MappingView)
self.assertIsInstance(d.viewvalues(), collections.Sized)
self.assertIsInstance(d.viewitems(), collections.ItemsView)
self.assertIsInstance(d.viewitems(), collections.MappingView)
self.assertIsInstance(d.viewitems(), collections.Set)
self.assertIsInstance(d.viewitems(), collections.Sized)
self.assertIsInstance(d.viewitems(), collections.Iterable)
self.assertIsInstance(d.viewitems(), collections.Container)
示例3: __init__
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def __init__(self, criterion_fns, submodules, collect_fn=None, reduce_method="mean"):
super(GradAccumulator, self).__init__()
assert isinstance(submodules, (Sized, Iterable)), "invalid submodules"
if isinstance(criterion_fns, (Sized, Iterable)):
assert len(submodules) == len(criterion_fns)
assert all([isinstance(submodule, nn.Module) for submodule in submodules])
assert all([isinstance(criterion_fn, nn.Module) for criterion_fn in criterion_fns])
elif isinstance(criterion_fns, nn.Module):
criterion_fns = [criterion_fns for _ in range(len(submodules))]
elif criterion_fns is None:
criterion_fns = [criterion_fns for _ in range(len(submodules))]
else:
raise ValueError("invalid criterion function")
assert reduce_method in ("mean", "sum", None)
self.submodules = nn.ModuleList(submodules)
self.criterion_fns = nn.ModuleList(criterion_fns)
self.method = reduce_method
self.grad_buffer = None
self.func = GradAccumulatorFunction.apply
self.collect_fn = collect_fn
示例4: _sequential
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def _sequential(function: Callable, *iterables: Iterable, **kwargs: Any) -> Generator:
"""Returns a generator for a sequential map with a progress bar.
Arguments:
function(Callable): The function to apply to each element of the given Iterables.
iterables(Tuple[Iterable]): One or more Iterables containing the data to be mapped.
Returns:
A generator which will apply the function to each element of the given Iterables
sequentially in order with a progress bar.
"""
# Determine length of tqdm (equal to length of shortest iterable)
length = min(len(iterable) for iterable in iterables if isinstance(iterable, Sized))
# Create sequential generator
for item in tqdm(map(function, *iterables), total=length, **kwargs):
yield item
示例5: map
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def map(
self,
func, iterable,
chunksize=1, sort=True, total=None, desc='', callback=None,
use_tqdm=True, update_interval=0.1, update_iters=1, **kwargs
):
if total is None and isinstance(iterable, collections.Sized):
total = len(iterable)
if use_tqdm:
pbar = tqdm_pbar(total=total, **kwargs)
with pbar:
return super().map(func, iterable, chunksize, sort, callback=self._wrap_callback(
callback, pbar, desc, update_interval=update_interval, update_iters=update_iters
))
else:
return super().map(func, iterable, chunksize, sort, callback=callback)
示例6: to_players
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def to_players(self, *players):
"""
Set the destination of the chat message.
:param players: Player instance(s) or player login string(s). Can be a list, or a single entry.
:return: Self reference.
:rtype: pyplanet.contrib.chat.query.ChatQuery
"""
# Unpack list in unpacked list if given.
if len(players) == 1 and isinstance(players[0], collections.Iterable):
players = players[0]
# Replace logins.
if isinstance(players, Player):
self._logins = set()
self._logins.add(players.login)
elif isinstance(players, str):
self._logins = set()
self._logins.add(players)
elif isinstance(players, collections.Iterable) and isinstance(players, collections.Sized):
self._logins = set()
self.add_to(players)
return self
示例7: set_marker
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def set_marker(self, marker):
if (isinstance(marker, np.ndarray) and marker.ndim == 2 and
marker.shape[1] == 2):
self._marker_function = self._set_vertices
elif (isinstance(marker, Sized) and len(marker) in (2, 3) and
marker[1] in (0, 1, 2, 3)):
self._marker_function = self._set_tuple_marker
elif (not isinstance(marker, (np.ndarray, list)) and
marker in self.markers):
self._marker_function = getattr(
self, '_set_' + self.markers[marker])
elif isinstance(marker, six.string_types) and is_math_text(marker):
self._marker_function = self._set_mathtext_path
elif isinstance(marker, Path):
self._marker_function = self._set_path_marker
else:
try:
Path(marker)
self._marker_function = self._set_vertices
except ValueError:
raise ValueError('Unrecognized marker style'
' {0}'.format(marker))
self._marker = marker
self._recache()
示例8: assertEmpty
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def assertEmpty(self, container, msg=None):
"""Assert that an object has zero length.
Args:
container: Anything that implements the collections.Sized interface.
msg: Optional message to report on failure.
"""
if not isinstance(container, collections.Sized):
self.fail('Expected a Sized object, got: '
'{!r}'.format(type(container).__name__), msg)
# explicitly check the length since some Sized objects (e.g. numpy.ndarray)
# have strange __nonzero__/__bool__ behavior.
if len(container):
self.fail('{!r} has length of {}.'.format(container, len(container)), msg)
示例9: assertNotEmpty
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def assertNotEmpty(self, container, msg=None):
"""Assert that an object has non-zero length.
Args:
container: Anything that implements the collections.Sized interface.
msg: Optional message to report on failure.
"""
if not isinstance(container, collections.Sized):
self.fail('Expected a Sized object, got: '
'{!r}'.format(type(container).__name__), msg)
# explicitly check the length since some Sized objects (e.g. numpy.ndarray)
# have strange __nonzero__/__bool__ behavior.
if not len(container):
self.fail('{!r} has length of 0.'.format(container), msg)
示例10: __instancecheck__
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def __instancecheck__(self, instance):
return (isinstance(instance, collections.Iterable)
and isinstance(instance, collections.Sized)
and isinstance(instance, collections.Container)
and all(isinstance(x, self._type) for x in instance))
示例11: test_direct_subclassing
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def test_direct_subclassing(self):
for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
class C(B):
pass
self.assertTrue(issubclass(C, B))
self.assertFalse(issubclass(int, C))
示例12: test_registration
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def test_registration(self):
for B in Hashable, Iterable, Iterator, Sized, Container, Callable:
class C:
__metaclass__ = type
__hash__ = None # Make sure it isn't hashable by default
self.assertFalse(issubclass(C, B), B.__name__)
B.register(C)
self.assertTrue(issubclass(C, B))
示例13: infer_cardinalities
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def infer_cardinalities(value, type_spec):
"""Infers cardinalities from Python `value`.
Allows for any Python object to represent a federated value; enforcing
particular representations is not the job of this inference function, but
rather ingestion functions lower in the stack.
Args:
value: Python object from which to infer TFF placement cardinalities.
type_spec: The TFF type spec for `value`, determining the semantics for
inferring cardinalities. That is, we only pull the cardinality off of
federated types.
Returns:
Dict of cardinalities.
Raises:
ValueError: If conflicting cardinalities are inferred from `value`.
TypeError: If the arguments are of the wrong types, or if `type_spec` is
a federated type which is not `all_equal` but the yet-to-be-embedded
`value` is not represented as a Python `list`.
"""
py_typecheck.check_not_none(value)
py_typecheck.check_type(type_spec, computation_types.Type)
if type_spec.is_federated():
if type_spec.all_equal:
return {}
py_typecheck.check_type(value, collections.Sized)
return {type_spec.placement: len(value)}
elif type_spec.is_tuple():
anonymous_tuple_value = anonymous_tuple.from_container(
value, recursive=False)
cardinality_dict = {}
for idx, (_,
elem_type) in enumerate(anonymous_tuple.to_elements(type_spec)):
cardinality_dict = merge_cardinalities(
cardinality_dict,
infer_cardinalities(anonymous_tuple_value[idx], elem_type))
return cardinality_dict
else:
return {}
示例14: pad_input_tensor
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def pad_input_tensor(self, input_tensor):
if self.target_input_size is None:
return input_tensor
if (
isinstance(self.target_input_size, collections.Iterable) and
isinstance(self.target_input_size, collections.Sized)
):
assert len(self.target_input_size) == 2, "wrong target_input_size"
final_input_size = self.target_input_size
else:
final_input_size = [self.target_input_size] * 2
init_input_size = tmf.get_shape(input_tensor)[1:3]
assert math.isclose(final_input_size[0]/init_input_size[0], final_input_size[1]/init_input_size[1]), \
"enlarge ratio should be the same (for the simplicity of other implementation)"
assert final_input_size[0] >= init_input_size[0] and final_input_size[1] >= init_input_size[1], \
"target input size should not be smaller the actual input size"
if init_input_size[0] == final_input_size[0] and init_input_size[1] == final_input_size[1]:
return input_tensor
else:
the_pad_y_begin = (final_input_size[0] - init_input_size[0]) // 2
the_pad_x_begin = (final_input_size[1] - init_input_size[1]) // 2
the_padding = [
[0, 0],
[the_pad_y_begin, final_input_size[0] - init_input_size[0] - the_pad_y_begin],
[the_pad_x_begin, final_input_size[1] - init_input_size[1] - the_pad_x_begin],
[0] * 2,
]
paded_input_tensor = tmf.pad(
tensor=input_tensor, paddings=the_padding, mode="MEAN_EDGE",
geometric_axis=[1, 2]
)
return paded_input_tensor
示例15: _parallel
# 需要導入模塊: import collections [as 別名]
# 或者: from collections import Sized [as 別名]
def _parallel(ordered: bool, function: Callable, *iterables: Iterable, **kwargs: Any) -> Generator:
"""Returns a generator for a parallel map with a progress bar.
Arguments:
ordered(bool): True for an ordered map, false for an unordered map.
function(Callable): The function to apply to each element of the given Iterables.
iterables(Tuple[Iterable]): One or more Iterables containing the data to be mapped.
Returns:
A generator which will apply the function to each element of the given Iterables
in parallel in order with a progress bar.
"""
# Extract num_cpus
num_cpus = kwargs.pop('num_cpus', None)
# Determine num_cpus
if num_cpus is None:
num_cpus = cpu_count()
elif type(num_cpus) == float:
num_cpus = int(round(num_cpus * cpu_count()))
# Determine length of tqdm (equal to length of shortest iterable)
length = min(len(iterable) for iterable in iterables if isinstance(iterable, Sized))
# Create parallel generator
map_type = 'imap' if ordered else 'uimap'
pool = Pool(num_cpus)
map_func = getattr(pool, map_type)
for item in tqdm(map_func(function, *iterables), total=length, **kwargs):
yield item
pool.clear()