Python collections.Sized() Examples

The following are 30 code examples for showing how to use collections.Sized(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module collections , or try the search function .

Example 1
Project: ironpython2   Author: IronLanguages   File: test_collections.py    License: Apache License 2.0 6 votes vote down vote up
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__') 
Example 2
Project: ironpython2   Author: IronLanguages   File: test_dictviews.py    License: Apache License 2.0 6 votes vote down vote up
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) 
Example 3
Project: BinderFilter   Author: dxwu   File: test_collections.py    License: MIT License 6 votes vote down vote up
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__') 
Example 4
Project: oss-ftp   Author: aliyun   File: test_collections.py    License: MIT License 6 votes vote down vote up
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__') 
Example 5
Project: PPGNet   Author: svip-lab   File: common.py    License: MIT License 6 votes vote down vote up
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 
Example 6
Project: p_tqdm   Author: swansonk14   File: p_tqdm.py    License: MIT License 6 votes vote down vote up
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 
Example 7
Project: Jacinle   Author: vacancy   File: pool.py    License: MIT License 6 votes vote down vote up
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) 
Example 8
Project: gcblue   Author: gcblue   File: test_collections.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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__') 
Example 9
Project: PyPlanet   Author: PyPlanet   File: query.py    License: GNU General Public License v3.0 6 votes vote down vote up
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 
Example 10
Project: CTFCrackTools-V2   Author: Acmesec   File: test_collections.py    License: GNU General Public License v3.0 6 votes vote down vote up
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__') 
Example 11
Project: CTFCrackTools   Author: Acmesec   File: test_collections.py    License: GNU General Public License v3.0 6 votes vote down vote up
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__') 
Example 12
Project: twitter-stock-recommendation   Author: alvarobartt   File: markers.py    License: MIT License 6 votes vote down vote up
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() 
Example 13
Project: DOTA_models   Author: ringringyi   File: graph_builder_test.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 14
Project: DOTA_models   Author: ringringyi   File: graph_builder_test.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 15
Project: lambda-packs   Author: ryfeus   File: _typecheck.py    License: MIT License 5 votes vote down vote up
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)) 
Example 16
Project: ironpython2   Author: IronLanguages   File: test_collections.py    License: Apache License 2.0 5 votes vote down vote up
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)) 
Example 17
Project: ironpython2   Author: IronLanguages   File: test_collections.py    License: Apache License 2.0 5 votes vote down vote up
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)) 
Example 18
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: _typecheck.py    License: MIT License 5 votes vote down vote up
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)) 
Example 19
Project: BinderFilter   Author: dxwu   File: test_collections.py    License: MIT License 5 votes vote down vote up
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)) 
Example 20
Project: BinderFilter   Author: dxwu   File: test_collections.py    License: MIT License 5 votes vote down vote up
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)) 
Example 21
Project: oss-ftp   Author: aliyun   File: test_collections.py    License: MIT License 5 votes vote down vote up
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)) 
Example 22
Project: oss-ftp   Author: aliyun   File: test_collections.py    License: MIT License 5 votes vote down vote up
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)) 
Example 23
Project: yolo_v2   Author: rky0930   File: graph_builder_test.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 24
Project: yolo_v2   Author: rky0930   File: graph_builder_test.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 25
Project: federated   Author: tensorflow   File: cardinalities_utils.py    License: Apache License 2.0 5 votes vote down vote up
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 {} 
Example 26
Project: Gun-Detector   Author: itsamitgoel   File: graph_builder_test.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 27
Project: Gun-Detector   Author: itsamitgoel   File: graph_builder_test.py    License: Apache License 2.0 5 votes vote down vote up
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) 
Example 28
Project: lmdis-rep   Author: YutingZhang   File: generic_structure_encoder.py    License: Apache License 2.0 5 votes vote down vote up
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 
Example 29
Project: p_tqdm   Author: swansonk14   File: p_tqdm.py    License: MIT License 5 votes vote down vote up
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() 
Example 30
Project: executing   Author: alexmojaki   File: executing.py    License: MIT License 5 votes vote down vote up
def only(it):
    if isinstance(it, Sized):
        if len(it) != 1:
            raise NotOneValueFound('Expected one value, found %s' % len(it))
        # noinspection PyTypeChecker
        return list(it)[0]

    lst = tuple(islice(it, 2))
    if len(lst) == 0:
        raise NotOneValueFound('Expected one value, found 0')
    if len(lst) > 1:
        raise NotOneValueFound('Expected one value, found several')
    return lst[0]