Python collections.MutableSequence() Examples

The following are 30 code examples for showing how to use collections.MutableSequence(). 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: python-opencage-geocoder   Author: OpenCageData   File: geocoder.py    License: MIT License 6 votes vote down vote up
def floatify_latlng(input_value):
    """
    Work around a JSON dict with string, not float, lat/lngs.

    Given anything (list/dict/etc) it will return that thing again, *but* any
    dict (at any level) that has only 2 elements lat & lng, will be replaced
    with the lat & lng turned into floats.

    If the API returns the lat/lng as strings, and not numbers, then this
    function will 'clean them up' to be floats.
    """
    if isinstance(input_value, collections.Mapping):
        if len(input_value) == 2 and sorted(input_value.keys()) == ['lat', 'lng']:
            # This dict has only 2 keys 'lat' & 'lon'
            return {'lat': float_if_float(input_value["lat"]), 'lng': float_if_float(input_value["lng"])}
        else:
            return dict((key, floatify_latlng(value)) for key, value in input_value.items())
    elif isinstance(input_value, collections.MutableSequence):
        return [floatify_latlng(x) for x in input_value]
    else:
        return input_value 
Example 2
Project: python-zhmcclient   Author: zhmcclient   File: _utils.py    License: Apache License 2.0 6 votes vote down vote up
def repr_list(_list, indent):
    """Return a debug representation of a list or tuple."""
    # pprint represents lists and tuples in one row if possible. We want one
    # per row, so we iterate ourselves.
    if _list is None:
        return 'None'
    if isinstance(_list, MutableSequence):
        bm = '['
        em = ']'
    elif isinstance(_list, Iterable):
        bm = '('
        em = ')'
    else:
        raise TypeError("Object must be an iterable, but is a %s" %
                        type(_list))
    ret = bm + '\n'
    for value in _list:
        ret += _indent('%r,\n' % value, 2)
    ret += em
    ret = repr_text(ret, indent=indent)
    return ret.lstrip(' ') 
Example 3
Project: flocker   Author: ClusterHQ   File: _model.py    License: Apache License 2.0 6 votes vote down vote up
def _shell_join(seq):
    """
    Convert a nested list of strings to a shell command.

    Each string in the list is escaped as necessary to allow it to be
    passed to a shell as a single word. If an item is a list, it is a
    nested command, which will be escaped first, and then added as a
    single word to the top-level command.

    For example, ['su', 'root', '-c', ['apt-get', 'update']] becomes
    "su root -c 'apt-get update'".
    """
    result = []
    for word in seq:
        if isinstance(word, (tuple, MutableSequence)):
            word = _shell_join(word)
        escaped = shell_quote(word)
        result.append(escaped)
    return ' '.join(result) 
Example 4
Project: django-autotranslate   Author: ankitpopli1891   File: services.py    License: MIT License 6 votes vote down vote up
def translate_strings(self, strings, target_language, source_language='en', optimized=True):
        assert isinstance(strings, collections.MutableSequence), \
            '`strings` should be a sequence containing string_types'
        assert not optimized, 'optimized=True is not supported in `GoogleAPITranslatorService`'
        if len(strings) == 0:
            return []
        elif len(strings) <= self.max_segments:
            setattr(self, 'translated_strings', getattr(self, 'translated_strings', []))
            response = self.service.translations() \
                .list(source=source_language, target=target_language, q=strings).execute()
            self.translated_strings.extend([t.get('translatedText') for t in response.get('translations')])
            return self.translated_strings
        else:
            self.translate_strings(strings[0:self.max_segments], target_language, source_language, optimized)
            _translated_strings = self.translate_strings(strings[self.max_segments:],
                                                         target_language, source_language, optimized)

            # reset the property or it will grow with subsequent calls
            self.translated_strings = []
            return _translated_strings 
Example 5
Project: virt-who   Author: candlepin   File: config.py    License: GNU General Public License v2.0 6 votes vote down vote up
def remove_key(self, key):
        # The parts of the ConfigSection that might contain "key"
        trackers = [self.validation_methods,
                    self._destinations,
                    self._restricted,
                    self._required_keys,
                    self.defaults,
                    self._values,
                    self._unvalidated_keys,
                    ]
        for tracker in trackers:
            if key in tracker:
                if isinstance(tracker, collections.MutableMapping):
                    del tracker[key]
                elif isinstance(tracker, collections.MutableSequence):
                    tracker.remove(key)
                elif isinstance(tracker, collections.MutableSet):
                    tracker.discard(key) 
Example 6
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_typing.py    License: GNU General Public License v3.0 6 votes vote down vote up
def test_collections_as_base(self):

        class M(collections.Mapping): ...
        self.assertIsSubclass(M, typing.Mapping)
        self.assertIsSubclass(M, typing.Iterable)

        class S(collections.MutableSequence): ...
        self.assertIsSubclass(S, typing.MutableSequence)
        self.assertIsSubclass(S, typing.Iterable)

        class I(collections.Iterable): ...
        self.assertIsSubclass(I, typing.Iterable)

        class A(collections.Mapping, metaclass=abc.ABCMeta): ...
        class B: ...
        A.register(B)
        self.assertIsSubclass(B, typing.Mapping) 
Example 7
Project: harpoon   Author: Te-k   File: opencage.py    License: GNU General Public License v3.0 6 votes vote down vote up
def floatify_latlng(input_value):
    """
    Work around a JSON dict with string, not float, lat/lngs.
    Given anything (list/dict/etc) it will return that thing again, *but* any
    dict (at any level) that has only 2 elements lat & lng, will be replaced
    with the lat & lng turned into floats.
    If the API returns the lat/lng as strings, and not numbers, then this
    function will 'clean them up' to be floats.
    """
    if isinstance(input_value, collections.Mapping):
        if len(input_value) == 2 and sorted(input_value.keys()) == ['lat', 'lng']:
            # This dict has only 2 keys 'lat' & 'lon'
            return {'lat': float_if_float(input_value["lat"]), 'lng': float_if_float(input_value["lng"])}
        else:
            return dict((key, floatify_latlng(value)) for key, value in input_value.items())
    elif isinstance(input_value, collections.MutableSequence):
        return [floatify_latlng(x) for x in input_value]
    else:
        return input_value 
Example 8
Project: json-spec   Author: johnnoone   File: bases.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def remove(self, pointer):
        """Remove element from sequence, member from mapping.

        :param pointer: the path to search in
        :return: resolved document
        :rtype: Target
        """
        doc = deepcopy(self.document)
        parent, obj = None, doc
        try:
            # fetching
            for token in Pointer(pointer):
                parent, obj = obj, token.extract(obj, bypass_ref=True)

            # removing
            if isinstance(parent, Mapping):
                del parent[token]

            if isinstance(parent, MutableSequence):
                parent.pop(int(token))
        except Exception as error:
            raise Error(*error.args)

        return Target(doc) 
Example 9
Project: misp42splunk   Author: remg427   File: sandbox.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def modifies_known_mutable(obj, attr):
    """This function checks if an attribute on a builtin mutable object
    (list, dict, set or deque) would modify it if called.  It also supports
    the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
    with Python 2.6 onwards the abstract base classes `MutableSet`,
    `MutableMapping`, and `MutableSequence`.

    >>> modifies_known_mutable({}, "clear")
    True
    >>> modifies_known_mutable({}, "keys")
    False
    >>> modifies_known_mutable([], "append")
    True
    >>> modifies_known_mutable([], "index")
    False

    If called with an unsupported object (such as unicode) `False` is
    returned.

    >>> modifies_known_mutable("foo", "upper")
    False
    """
    for typespec, unsafe in _mutable_spec:
        if isinstance(obj, typespec):
            return attr in unsafe
    return False 
Example 10
Project: misp42splunk   Author: remg427   File: sandbox.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def modifies_known_mutable(obj, attr):
    """This function checks if an attribute on a builtin mutable object
    (list, dict, set or deque) would modify it if called.  It also supports
    the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
    with Python 2.6 onwards the abstract base classes `MutableSet`,
    `MutableMapping`, and `MutableSequence`.

    >>> modifies_known_mutable({}, "clear")
    True
    >>> modifies_known_mutable({}, "keys")
    False
    >>> modifies_known_mutable([], "append")
    True
    >>> modifies_known_mutable([], "index")
    False

    If called with an unsupported object (such as unicode) `False` is
    returned.

    >>> modifies_known_mutable("foo", "upper")
    False
    """
    for typespec, unsafe in _mutable_spec:
        if isinstance(obj, typespec):
            return attr in unsafe
    return False 
Example 11
Project: recruit   Author: Frank-qlu   File: sandbox.py    License: Apache License 2.0 5 votes vote down vote up
def modifies_known_mutable(obj, attr):
    """This function checks if an attribute on a builtin mutable object
    (list, dict, set or deque) would modify it if called.  It also supports
    the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
    with Python 2.6 onwards the abstract base classes `MutableSet`,
    `MutableMapping`, and `MutableSequence`.

    >>> modifies_known_mutable({}, "clear")
    True
    >>> modifies_known_mutable({}, "keys")
    False
    >>> modifies_known_mutable([], "append")
    True
    >>> modifies_known_mutable([], "index")
    False

    If called with an unsupported object (such as unicode) `False` is
    returned.

    >>> modifies_known_mutable("foo", "upper")
    False
    """
    for typespec, unsafe in _mutable_spec:
        if isinstance(obj, typespec):
            return attr in unsafe
    return False 
Example 12
Project: recruit   Author: Frank-qlu   File: sandbox.py    License: Apache License 2.0 5 votes vote down vote up
def modifies_known_mutable(obj, attr):
    """This function checks if an attribute on a builtin mutable object
    (list, dict, set or deque) would modify it if called.  It also supports
    the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
    with Python 2.6 onwards the abstract base classes `MutableSet`,
    `MutableMapping`, and `MutableSequence`.

    >>> modifies_known_mutable({}, "clear")
    True
    >>> modifies_known_mutable({}, "keys")
    False
    >>> modifies_known_mutable([], "append")
    True
    >>> modifies_known_mutable([], "index")
    False

    If called with an unsupported object (such as unicode) `False` is
    returned.

    >>> modifies_known_mutable("foo", "upper")
    False
    """
    for typespec, unsafe in _mutable_spec:
        if isinstance(obj, typespec):
            return attr in unsafe
    return False 
Example 13
Project: jbox   Author: jpush   File: sandbox.py    License: MIT License 5 votes vote down vote up
def modifies_known_mutable(obj, attr):
    """This function checks if an attribute on a builtin mutable object
    (list, dict, set or deque) would modify it if called.  It also supports
    the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
    with Python 2.6 onwards the abstract base classes `MutableSet`,
    `MutableMapping`, and `MutableSequence`.

    >>> modifies_known_mutable({}, "clear")
    True
    >>> modifies_known_mutable({}, "keys")
    False
    >>> modifies_known_mutable([], "append")
    True
    >>> modifies_known_mutable([], "index")
    False

    If called with an unsupported object (such as unicode) `False` is
    returned.

    >>> modifies_known_mutable("foo", "upper")
    False
    """
    for typespec, unsafe in _mutable_spec:
        if isinstance(obj, typespec):
            return attr in unsafe
    return False 
Example 14
Project: lambda-packs   Author: ryfeus   File: message_test.py    License: MIT License 5 votes vote down vote up
def testRepeatedFieldsAreSequences(self, message_module):
    m = message_module.TestAllTypes()
    self.assertIsInstance(m.repeated_int32, collections.MutableSequence)
    self.assertIsInstance(m.repeated_nested_message,
                          collections.MutableSequence) 
Example 15
Project: ironpython2   Author: IronLanguages   File: test_collections.py    License: Apache License 2.0 5 votes vote down vote up
def test_MutableSequence(self):
        for sample in [tuple, str]:
            self.assertNotIsInstance(sample(), MutableSequence)
            self.assertFalse(issubclass(sample, MutableSequence))
        for sample in [list]:
            self.assertIsInstance(sample(), MutableSequence)
            self.assertTrue(issubclass(sample, MutableSequence))
        self.assertFalse(issubclass(basestring, MutableSequence))
        self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
            '__len__', '__getitem__', '__setitem__', '__delitem__', 'insert') 
Example 16
Project: auto-alt-text-lambda-api   Author: abhisuri97   File: message_test.py    License: MIT License 5 votes vote down vote up
def testRepeatedFieldsAreSequences(self, message_module):
    m = message_module.TestAllTypes()
    self.assertIsInstance(m.repeated_int32, collections.MutableSequence)
    self.assertIsInstance(m.repeated_nested_message,
                          collections.MutableSequence) 
Example 17
Project: faces   Author: skarlekar   File: transform.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _transform_list(self, model, params, transformation, target_shape):
        if not isinstance(params, MutableSequence):
            return
        member_model = model.member
        member_shape = member_model.name
        for i, item in enumerate(params):
            if member_shape == target_shape:
                params[i] = transformation(item)
            else:
                self._transform_parameters(
                    member_model, params[i], transformation, target_shape) 
Example 18
Project: faces   Author: skarlekar   File: transform.py    License: GNU General Public License v2.0 5 votes vote down vote up
def _transform_list(self, model, params, transformation, target_shape):
        if not isinstance(params, MutableSequence):
            return
        member_model = model.member
        member_shape = member_model.name
        for i, item in enumerate(params):
            if member_shape == target_shape:
                params[i] = transformation(item)
            else:
                self._transform_parameters(
                    member_model, params[i], transformation, target_shape) 
Example 19
Project: BinderFilter   Author: dxwu   File: test_collections.py    License: MIT License 5 votes vote down vote up
def test_MutableSequence(self):
        for sample in [tuple, str]:
            self.assertNotIsInstance(sample(), MutableSequence)
            self.assertFalse(issubclass(sample, MutableSequence))
        for sample in [list]:
            self.assertIsInstance(sample(), MutableSequence)
            self.assertTrue(issubclass(sample, MutableSequence))
        self.assertFalse(issubclass(basestring, MutableSequence))
        self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
            '__len__', '__getitem__', '__setitem__', '__delitem__', 'insert') 
Example 20
Project: RSSNewsGAE   Author: liantian-cn   File: sandbox.py    License: Apache License 2.0 5 votes vote down vote up
def modifies_known_mutable(obj, attr):
    """This function checks if an attribute on a builtin mutable object
    (list, dict, set or deque) would modify it if called.  It also supports
    the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
    with Python 2.6 onwards the abstract base classes `MutableSet`,
    `MutableMapping`, and `MutableSequence`.

    >>> modifies_known_mutable({}, "clear")
    True
    >>> modifies_known_mutable({}, "keys")
    False
    >>> modifies_known_mutable([], "append")
    True
    >>> modifies_known_mutable([], "index")
    False

    If called with an unsupported object (such as unicode) `False` is
    returned.

    >>> modifies_known_mutable("foo", "upper")
    False
    """
    for typespec, unsafe in _mutable_spec:
        if isinstance(obj, typespec):
            return attr in unsafe
    return False 
Example 21
Project: oss-ftp   Author: aliyun   File: test_collections.py    License: MIT License 5 votes vote down vote up
def test_MutableSequence(self):
        for sample in [tuple, str]:
            self.assertNotIsInstance(sample(), MutableSequence)
            self.assertFalse(issubclass(sample, MutableSequence))
        for sample in [list]:
            self.assertIsInstance(sample(), MutableSequence)
            self.assertTrue(issubclass(sample, MutableSequence))
        self.assertFalse(issubclass(basestring, MutableSequence))
        self.validate_abstract_methods(MutableSequence, '__contains__', '__iter__',
            '__len__', '__getitem__', '__setitem__', '__delitem__', 'insert') 
Example 22
Project: sklearn-theano   Author: sklearn-theano   File: message_test.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def testRepeatedFieldsAreSequences(self, message_module):
    m = message_module.TestAllTypes()
    self.assertIsInstance(m.repeated_int32, collections.MutableSequence)
    self.assertIsInstance(m.repeated_nested_message,
                          collections.MutableSequence) 
Example 23
Project: aztk   Author: Azure   File: fields.py    License: MIT License 5 votes vote down vote up
def __set__(self, instance, value):
        if isinstance(value, collections.MutableSequence):
            value = self._resolve(value)
        if value is None:
            value = []
        super().__set__(instance, value) 
Example 24
Project: aztk   Author: Azure   File: validators.py    License: MIT License 5 votes vote down vote up
def validate(self, value):
        if not value:
            return

        if not isinstance(value, collections.MutableSequence):
            raise InvalidModelFieldError("should be a list")

        for i in value:
            for validator in self.validators:
                validator(i) 
Example 25
Project: testplan   Author: Morgan-Stanley   File: schemas.py    License: Apache License 2.0 5 votes vote down vote up
def _deserialize(self, value, attr, obj, recurse_lvl=0):
        """
        Check deeply to see if there is a {'bytes': [...]} dict and if so
        convert it to a bytes object
        """
        if recurse_lvl == 0:
            valued = super(EntriesField, self)._deserialize(value, attr, obj)
        else:
            valued = value
        if isinstance(valued, MutableMapping):
            for key in six.iterkeys(valued):
                if key == self._BYTES_KEY:
                    return self._hex_list_to_binary(valued[key])
                valued[key] = self._deserialize(
                    value=valued[key],
                    attr=attr,
                    obj=obj,
                    recurse_lvl=(recurse_lvl + 1),
                )
            return valued
        if isinstance(valued, MutableSequence):
            for i in range(len(valued)):
                valued[i] = self._deserialize(
                    value=valued[i],
                    attr=attr,
                    obj=obj,
                    recurse_lvl=(recurse_lvl + 1),
                )
            return valued
        return valued 
Example 26
Project: n6   Author: CERT-Polska   File: models.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def create_new(cls, context, **kwargs):
        session = context.db_session
        validated_kwargs = {key: ([val] if (is_property_list(getattr(cls, key)) and
                                            not isinstance(val, MutableSequence))
                                  else val)
                            for key, val in kwargs.iteritems()}
        new_record = cls(**validated_kwargs)
        session.add(new_record)
        return new_record 
Example 27
Project: n6   Author: CERT-Polska   File: test_generate_test_events.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_params(self, category, label):
        """
        Check if params are included in the event.

        Two cases: 1st - category in 'dip categories', 2nd case
        - category not in 'dip categories'.
        """
        params = {}
        params.update(category)
        params.update(self._STANDARD_PARAMS)
        params.update(self._SPECIAL_PARAMS)
        with standard_config_patch:
            instance = RandomEvent(params=params)
        random_event = instance.event
        for param, values in self._STANDARD_PARAMS.iteritems():
            if ((label == 'non_dip_categories' and param in ['dip', 'proto']) or
                    (label == 'dip_categories' and param in ['url', 'fqdn'])):
                self.assertNotIn(param, random_event)
            else:
                self.assertIn(param, random_event)
                event_attr = random_event.get(param)
                if isinstance(event_attr, MutableSequence):
                    self.assertEqual(values, event_attr)
                else:
                    self.assertIn(param, random_event)
                    event_attr = random_event.get(param)
                    if isinstance(event_attr, MutableSequence):
                        self.assertEqual(values, event_attr)
                    else:
                        self.assertIn(random_event.get(param), values)
            self._check_time_attribute(random_event.get('time'))
            if 'modified' in random_event:
                self._compare_modified_attribute(
                    random_event.get('time'), random_event.get('modified')) 
Example 28
Project: n6   Author: CERT-Polska   File: test_generate_test_events.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _get_test_asn_cc_method(self, opt_primary):
        """
        Test a behavior, when user attaches asn and cc params.

        It is a method returning a proper test method.
        When one of them or both are attached - include both of them in
        attributes (otherwise it is randomly chosen whether to include
        asn and cc in address attribute).
        """
        output_params = {}
        if opt_primary:
            assert_method = self.assertNotIn
            output_params['opt.primary'] = True
        else:
            assert_method = self.assertIn

        def test_method(params):
            output_params.update(params)
            with standard_config_patch:
                random_event = RandomEvent(params=output_params).event
            self.assertIn('address', random_event)
            address_attr = random_event.get('address')
            self.assertIsInstance(address_attr, MutableSequence)
            for address in address_attr:
                assert_method('asn', address)
                assert_method('cc', address)
        return test_method 
Example 29
Project: n6   Author: CERT-Polska   File: test_generate_test_events.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_inside_zone(self):
        """Access zone: 'inside' - 'client' attribute: client_id."""
        clients = [self._CLIENT_ID, None]
        for client in clients:
            with standard_config_patch:
                random_event = RandomEvent(access_zone='inside', client_id=client).event
            self.assertIn('client', random_event)
            client_attr = random_event.get('client')
            self.assertIsInstance(client_attr, MutableSequence)
            self.assertEqual(len(client_attr), 1)
            self.assertEqual(client_attr[0], client) 
Example 30
Project: n6   Author: CERT-Polska   File: test_manage_api.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_convert_attr_to_list_if_relationship(self):
        group_obj_one = self.system_group_db_model.create_new(self.context_mock,
                                                              **{'name': 'group one'})
        group_obj_two = self.system_group_db_model.create_new(self.context_mock,
                                                              **{'name': 'group two'})
        self.load_ca(self.context_mock, 'n6-client-ca', 'client')
        cert_pem = _load_cert_pem('n6-client-ca', '0000000000000000abcd')
        some_cert_obj = self.cert_db_model.create_new(self.context_mock,
                                                      **{'serial_hex': '0000000000000000abcd',
                                                         'ca_cert_label': 'n6-client-ca',
                                                         'certificate': cert_pem})
        org_obj = self.org_db_model.create_new(self.context_mock, **{'org_id': 'example.com'})
        tested_init_kwargs = {
            'login': 'user@example.com',
            'org': org_obj,
            'system_groups': [group_obj_one, group_obj_two],
            'created_certs': some_cert_obj,
            'revoked_certs': [some_cert_obj],
        }
        self.user_db_model.create_new(self.context_mock, **tested_init_kwargs)
        collection = self.context_mock.db_session.collection
        self.assertIn('org', collection)
        self.assertIn('cert', collection)
        self.assertIn('ca_cert', collection)
        self.assertIn('system_group', collection)
        self.assertIn('user', collection)
        user_obj_from_db = collection['user'][0]
        self.assertIsInstance(user_obj_from_db.org, self.org_db_model)
        self.assertIsInstance(user_obj_from_db.created_certs, MutableSequence)
        self.assertIsInstance(user_obj_from_db.revoked_certs, MutableSequence)
        self.assertIsInstance(user_obj_from_db.system_groups, MutableSequence)