Package pywbem :: Module cim_obj :: Class NocaseDict

[frames] | no frames]

# Class NocaseDict

source code

    object --+
             |
            NocaseDict

Yet another implementation of a case-insensitive dictionary.

Whenever keys are looked up, that is done case-insensitively. Whenever keys are returned, they are returned with the lexical case that was originally specified.

In addition to the methods listed, the dictionary supports:

> - Retrieval of values based on key: `val = d[key]`
> - Assigning values for a key: `d[key] = val`
> - Deleting a key/value pair: `del d[key]`
> - Equality comparison (`==`, `!=`)
> - Ordering comparison (`<`, `<=`, `>=`, `>`)
> - Containment test: `key in d`
> - For loops: `for key in d`
> - Determining length: `len(d)`

Instance Methods

__init__(self, *args, **kwargs) Initialize the new dictionary from at most one positional argument and optionally from additional keyword arguments. | source code

__getitem__(self, key) Invoked when retrieving the value for a key, using `val = d[key]`. | source code

__setitem__(self, key, value) Invoked when assigning a value for a key using `d[key] = val`. | source code

__delitem__(self, key) Invoked when deleting a key/value pair using `del d[key]`. | source code

__len__(self) Invoked when determining the number of key/value pairs in the dictionary using `len(d)`. | source code

has_key(self, key) Return a boolean indicating whether a specific key is in the dictionary. | source code

__contains__(self, key) Invoked when determining whether a specific key is in the dictionary using `key in d`. | source code

get(self, key, default=None) Get the value for a specific key, or the specified default value if the key does not exist. | source code

setdefault(self, key, default) Assign the specified default value for a specific key if the key did not exist and return the value for the key. | source code

keys(self) Return a copied list of the dictionary keys, in their original case. | source code

values(self) Return a copied list of the dictionary values. | source code

items(self) Return a copied list of the dictionary items, where each item is a tuple of its original key and its value. | source code

iterkeys(self) Return an iterator through the dictionary keys in their original case. | source code

itervalues(self) Return an iterator through the dictionary values. | source code

iteritems(self) Return an iterator through the dictionary items, where each item is a tuple of its original key and its value. | source code

__iter__(self) Invoked when iterating through the dictionary using `for key in d`. | source code

__repr__(self) Invoked when using `repr(d)`. | source code

update(self, *args, **kwargs) Update the dictionary from sequences of key/value pairs provided in any positional arguments, and from key/value pairs provided in any keyword arguments. | source code

clear(self) Remove all items from the dictionary. | source code

popitem(self) This function does nothing. | source code

copy(self) Return a shallow copy of the dictionary (i.e. | source code

__eq__(self, other) Invoked when two dictionaries are compared for equality or inequality. | source code

__cmp__(self, other) Invoked when two dictionaries are compared for equality, inequality, and greater-than/less-than comparisons. | source code

Inherited from `object`: `__delattr__`, `__format__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__setattr__`, `__sizeof__`, `__str__`, `__subclasshook__`

Properties
Inherited from `object`: `__class__`

Method Details

### __init__(self, *args, **kwargs)
(Constructor)

source code

Initialize the new dictionary from at most one positional argument and optionally from additional keyword arguments.

Initialization happens in two steps, first from the positional argument:

> - If no positional argument is provided, or if one argument with the value None is provided, the new dictionary will be left empty in this step.
> - If one positional argument of sequence type is provided, the items in that sequence must be tuples of key and value, respectively. The key/value pairs will be put into the new dictionary (without copying them).
> - If one positional argument of dictionary (mapping) or NocaseDict type is provided, its key/value pairs are put into the new dictionary (without copying them).
> - Otherwise, `TypeError` is raised.

After that, any provided keyword arguments are put into the so initialized dictionary as key/value pairs (without copying them).

Overrides: object.__init__

### __getitem__(self, key)
(Indexing operator)

source code

Invoked when retrieving the value for a key, using `val = d[key]`.

The key is looked up case-insensitively. Raises `KeyError` if the specified key does not exist. Note that __setitem__() ensures that only string typed keys will exist, so the key type is not tested here and specifying non-string typed keys will simply lead to a KeyError.

### __setitem__(self, key, value)
(Index assignment operator)

source code

Invoked when assigning a value for a key using `d[key] = val`.

The key is looked up case-insensitively. If the key does not exist, it is added with the new value. Otherwise, its value is overwritten with the new value.

Raises `TypeError` if the specified key does not have string type.

### __delitem__(self, key)
(Index deletion operator)

source code

Invoked when deleting a key/value pair using `del d[key]`.

The key is looked up case-insensitively. Raises `KeyError` if the specified key does not exist. Note that __setitem__() ensures that only string typed keys will exist, so the key type is not tested here and specifying non-string typed keys will simply lead to a KeyError.

### has_key(self, key)

source code

Return a boolean indicating whether a specific key is in the dictionary.

The key is looked up case-insensitively.

This method is deprecated in favor of using `key in d`.

### __contains__(self, key)
(In operator)

source code

Invoked when determining whether a specific key is in the dictionary using `key in d`.

The key is looked up case-insensitively.

### get(self, key, default=None)

source code

Get the value for a specific key, or the specified default value if the key does not exist.

The key is looked up case-insensitively.

### setdefault(self, key, default)

source code

Assign the specified default value for a specific key if the key did not exist and return the value for the key.

The key is looked up case-insensitively.

### __iter__(self)

source code

Invoked when iterating through the dictionary using `for key in d`.

The returned keys have their original case.

### __repr__(self)
(Representation operator)

source code

Invoked when using `repr(d)`.

The representation hides the implementation data structures and shows a dictionary that can be used for the constructor.

Overrides: object.__repr__

### update(self, *args, **kwargs)

source code

Update the dictionary from sequences of key/value pairs provided in any positional arguments, and from key/value pairs provided in any keyword arguments. The key/value pairs are not copied.

Each positional argument can be:

> - an object with a method items() that returns an iterable of tuples containing key and value.
> - an object without such a method, that is an iterable of tuples containing key and value.

Each keyword argument is a key/value pair.

### popitem(self)

source code

This function does nothing.

In a standard mapping implementation, it would remove and return an arbitrary item from the dictionary.

TODO: Why does popitem() do nothing; was it simply not implemented?

### copy(self)

source code

Return a shallow copy of the dictionary (i.e. the keys and values are not copied).

### __eq__(self, other)
(Equality operator)

source code

Invoked when two dictionaries are compared for equality or inequality.

The keys are looked up case-insensitively.

The comparison is delegated to equality comparison of matching key/value pairs.

### __cmp__(self, other)
(Comparison operator)

source code

Invoked when two dictionaries are compared for equality, inequality, and greater-than/less-than comparisons.

The keys are looked up case-insensitively.

`Self` is less than `other`, if:

> - a key in `self` is not in `other`, or
> - the value for a key in `self` is less than the value for that key in `other`, or
> - `self` has less key/value pairs than `other`.