Python collections.MutableSequence() Examples

The following are 30 code examples of collections.MutableSequence(). 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: geocoder.py    From python-opencage-geocoder with 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
Source File: _utils.py    From python-zhmcclient with 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
Source File: _model.py    From flocker with 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
Source File: services.py    From django-autotranslate with 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
Source File: config.py    From virt-who with 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
Source File: test_typing.py    From Project-New-Reign---Nemesis-Main with 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
Source File: opencage.py    From harpoon with 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
Source File: bases.py    From json-spec with 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
Source File: message_test.py    From lambda-packs with 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 #10
Source File: test_collections.py    From ironpython2 with 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 #11
Source File: message_test.py    From auto-alt-text-lambda-api with 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 #12
Source File: transform.py    From faces with 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 #13
Source File: transform.py    From faces with 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 #14
Source File: test_collections.py    From BinderFilter with 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 #15
Source File: sandbox.py    From RSSNewsGAE with 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 #16
Source File: test_collections.py    From oss-ftp with 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 #17
Source File: message_test.py    From sklearn-theano with 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 #18
Source File: fields.py    From aztk with 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 #19
Source File: validators.py    From aztk with 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 #20
Source File: schemas.py    From testplan with 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 #21
Source File: models.py    From n6 with 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 #22
Source File: test_generate_test_events.py    From n6 with 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 #23
Source File: test_generate_test_events.py    From n6 with 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 #24
Source File: test_generate_test_events.py    From n6 with 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 #25
Source File: test_manage_api.py    From n6 with 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) 
Example #26
Source File: patches.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def patched_populate_obj(self, obj, name):
    """
    A patched version of flask_admin's Field.populate_obj().

    This patch is needed to:

    * treat an empty or whitespace-only string value as NULL (for
      pragmatic reasons: in many cases accepting such a string,
      typically being a result of a GUI user's mistake, would be just
      confusing; at the same time, we do not see any cases when
      accepting such strings could be useful);

    * append a list of validation errors -- if an n6-specific model's
      validator (not a Flask-Admin validator) raised an exception -- to
      highlight invalid fields values in the GUI view.

    """

    # treating empty or whitespace-only text as NULL
    to_be_set = (None if (isinstance(self.data, basestring)
                          and not self.data.strip())
                 else self.data)

    # handling n6-specific model-level validation errors
    try:
        setattr(obj, name, to_be_set)
    except Exception as exc:
        invalid_field = getattr(exc, 'invalid_field', None)
        if invalid_field and isinstance(self.errors, MutableSequence):
            exc_message = get_exception_message(exc)
            if exc_message is not None:
                self.errors.append(exc_message)
            else:
                self.errors.append(u'Failed to create/update record.')
        raise 
Example #27
Source File: sandbox.py    From OpenXR-SDK-Source with 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 #28
Source File: utils.py    From yaql with Apache License 2.0 5 votes vote down vote up
def is_mutable(obj):
    return isinstance(obj, (collections.MutableSequence,
                            collections.MutableSet,
                            collections.MutableMapping)) 
Example #29
Source File: ssafile.py    From pysubs2 with MIT License 5 votes vote down vote up
def __repr__(self):
        if self.events:
            max_time = max(ev.end for ev in self)
            s = "<SSAFile with %d events and %d styles, last timestamp %s>" % \
                    (len(self), len(self.styles), ms_to_str(max_time))
        else:
            s = "<SSAFile with 0 events and %d styles>" % len(self.styles)

        if not PY3: s = s.encode("utf-8")
        return s

    # ------------------------------------------------------------------------
    # MutableSequence implementation + sort()
    # ------------------------------------------------------------------------ 
Example #30
Source File: sandbox.py    From pySINDy with 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