Python collections.OrderedDict.__setitem__() Examples

The following are 30 code examples of collections.OrderedDict.__setitem__(). 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.OrderedDict , or try the search function .
Example #1
Source File: utils.py    From bamnostic with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __setitem__(self, key, value):
        """Basic setter that adds new item to dictionary, and then performs cull()
        to ensure max_cache has not been violated.

        Args:
            key (str): immutable dictionary key
            value (any): any dictionary value
        """

        OrderedDict.__setitem__(self, key, value)
        self.cull()

# The BAM format uses byte encoding to compress alignment data. One such
# compression is how operations are stored: they are stored and an
# array of integers. These integers are mapped to their respective
# operation identifier. Below is the mapping utility. 
Example #2
Source File: utils.py    From bamnostic with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def update(self, others):
        """ Same as a regular `dict.update`, however, since pypy's `dict.update`
        doesn't go through `dict.__setitem__`, this is used to ensure it does

        Args:
            others (iterable): a dictionary or iterable containing key/value pairs
        """

        if type(others) == dict:
            for k,v in others.items():
                self.__setitem__(k,v)
        elif type(others) in [list, tuple]:
            for k,v in others:
                self.__setitem__(k,v)
        else:
            raise ValueError('Iteratable/dict must be in key, value pairs') 
Example #3
Source File: options.py    From bootstrap.pytorch with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __setitem__(self, key, val):
        if key == '_{}__locked'.format(type(self).__name__):
            OrderedDict.__setitem__(self, key, val)
        elif hasattr(self, '_{}__locked'.format(type(self).__name__)):
            if self.__locked:
                raise PermissionError('Options\' dictionnary is locked and cannot be changed.')
            if type(val) == dict:
                val = OptionsDict(val)
                OrderedDict.__setitem__(self, key, val)
            elif isinstance(key, str) and '.' in key:
                first_key, other_keys = key.split('.', maxsplit=1)
                if first_key not in self:
                    self[first_key] = OptionsDict({})
                self[first_key][other_keys] = val
            else:
                OrderedDict.__setitem__(self, key, val)
        else:
            raise PermissionError('Tried to access Options\' dictionnary bypassing the lock feature.') 
Example #4
Source File: pgcollections.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, item, value):
        dict.__setitem__(self, item, value)
        dict.__setitem__(self, value, item) 
Example #5
Source File: utils.py    From SqlBeautifier with MIT License 5 votes vote down vote up
def __getitem__(self, key, *args, **kwargs):
            # Get the key and remove it from the cache, or raise KeyError
            value = OrderedDict.__getitem__(self, key)
            del self[key]

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value)

            # Return the value from the cache
            return value 
Example #6
Source File: pgcollections.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, val):
        kl = key.lower()
        if kl in self.keyMap:
            OrderedDict.__setitem__(self, self.keyMap[kl], val)
        else:
            OrderedDict.__setitem__(self, key, val)
            self.keyMap[kl] = key 
Example #7
Source File: pgcollections.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, attr, val):
        val = makeThreadsafe(val)
        self.lock()
        try:
            list.__setitem__(self, attr, val)
        finally:
            self.unlock() 
Example #8
Source File: pgcollections.py    From soapy with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, attr, val):
        if type(val) is dict:
            val = ThreadsafeDict(val)
        self.lock()
        try:
            dict.__setitem__(self, attr, val)
        finally:
            self.unlock() 
Example #9
Source File: pgcollections.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, val):
        kl = key.lower()
        if kl in self.keyMap:
            OrderedDict.__setitem__(self, self.keyMap[kl], val)
        else:
            OrderedDict.__setitem__(self, key, val)
            self.keyMap[kl] = key 
Example #10
Source File: flag_collection.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __setitem__(self, item, value, **kwargs):

        if isinstance(value, np.ndarray):
            if value.shape == self.shape:
                OrderedDict.__setitem__(self, item, value, **kwargs)
            else:
                raise ValueError("flags array shape {} does not match data "
                                 "shape {}".format(value.shape, self.shape))
        else:
            raise TypeError("flags should be given as a Numpy array") 
Example #11
Source File: opt.py    From pycbc with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        OrderedDict.__setitem__(self, key, value)
        self._check_size_limit() 
Example #12
Source File: utilities.py    From samacharbot2 with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):

        OrderedDict.__setitem__(self, key, value)
        self._check_size_limit() 
Example #13
Source File: environment_client.py    From active-qa with Apache License 2.0 5 votes vote down vote up
def __setitem__(self, key, value):
    OrderedDict.__setitem__(self, key, value)
    self._check_size_limit() 
Example #14
Source File: lrucache.py    From pyfilesystem2 with MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
        # type: (_K, _V) -> None
        """Store a new views, potentially discarding an old value.
        """
        if key not in self:
            if len(self) >= self.cache_size:
                self.popitem(last=False)
        OrderedDict.__setitem__(self, key, value) 
Example #15
Source File: lrucache.py    From pyfilesystem2 with MIT License 5 votes vote down vote up
def __getitem__(self, key):
        # type: (_K) -> _V
        """Get the item, but also makes it most recent.
        """
        _super = typing.cast(OrderedDict, super(LRUCache, self))
        value = _super.__getitem__(key)
        _super.__delitem__(key)
        _super.__setitem__(key, value)
        return value 
Example #16
Source File: pgcollections.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, attr, val):
        val = makeThreadsafe(val)
        self.lock()
        try:
            list.__setitem__(self, attr, val)
        finally:
            self.unlock() 
Example #17
Source File: cryptoengine.py    From joinmarket-clientserver with GNU General Public License v3.0 5 votes vote down vote up
def __getitem__(self, item):
        e = OrderedDict.__getitem__(self, item)
        del self[item]
        OrderedDict.__setitem__(self, item, e)
        return e 
Example #18
Source File: cryptoengine.py    From joinmarket-clientserver with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        OrderedDict.__setitem__(self, key, value)
        self._adjust_size() 
Example #19
Source File: utils.py    From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __setitem__(self, key, value, *args, **kwargs):
            # Reset the cache if we have too much cached entries and start over
            if len(self) >= self._maxsize:
                self.clear()

            # Insert the (key, value) pair on the front of the cache
            dict.__setitem__(self, key, value, *args, **kwargs) 
Example #20
Source File: utils.py    From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __setitem__(self, key, value, *args, **kwargs):
            # Key was inserted before, remove it so we put it at front later
            if key in self:
                del self[key]

            # Too much items on the cache, remove the least recent used
            elif len(self) >= self._maxsize:
                self.popitem(False)

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value, *args, **kwargs) 
Example #21
Source File: utils.py    From uroboroSQL-formatter with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __getitem__(self, key, *args, **kwargs):
            # Get the key and remove it from the cache, or raise KeyError
            value = OrderedDict.__getitem__(self, key)
            del self[key]

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value)

            # Return the value from the cache
            return value 
Example #22
Source File: streams.py    From convis with GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        _OrderedDict.__setitem__(self, key, value)
        self._check_size_limit() 
Example #23
Source File: pg.py    From slack-sql with MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
            if self._read_only:
                self._read_only_error()
            OrderedDict.__setitem__(self, key, value) 
Example #24
Source File: pg.py    From slack-sql with MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
            if self._read_only:
                self._read_only_error()
            dict.__setitem__(self, key, value) 
Example #25
Source File: pg.py    From slack-sql with MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
            if self._read_only:
                self._read_only_error()
            OrderedDict.__setitem__(self, key, value) 
Example #26
Source File: pg.py    From slack-sql with MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
            if self._read_only:
                self._read_only_error()
            dict.__setitem__(self, key, value) 
Example #27
Source File: server.py    From optimus with Apache License 2.0 5 votes vote down vote up
def __setitem__(self, key, value):
    OrderedDict.__setitem__(self, key, value)
    self._check_size_limit() 
Example #28
Source File: terrain.py    From pycraft with GNU General Public License v2.0 5 votes vote down vote up
def __setitem__(self, key, value):
        OrderedDict.__setitem__(self, key, value)
        self._check_limit() 
Example #29
Source File: utils.py    From SqlBeautifier with MIT License 5 votes vote down vote up
def __setitem__(self, key, value, *args, **kwargs):
            # Key was inserted before, remove it so we put it at front later
            if key in self:
                del self[key]

            # Too much items on the cache, remove the least recent used
            elif len(self) >= self._maxsize:
                self.popitem(False)

            # Insert the (key, value) pair on the front of the cache
            OrderedDict.__setitem__(self, key, value, *args, **kwargs) 
Example #30
Source File: analytic_for.py    From netharn with Apache License 2.0 5 votes vote down vote up
def __setitem__(self, key, value):
        if getattr(value, 'hidden', None) is not None:
            # When setting a value to an OutputShape object, if that object has
            # a hidden shape, then use that instead.
            value = value.hidden
        return OrderedDict.__setitem__(self, key, value)