LSPMap Class¶
-
class
localsolver.modeler.
LSPMap
¶ An LSPMap is a data structure mapping keys to values. Keys and values can be of any type except nil. A map is both an associative table and an array. Setting a value to an existing key will overwrite any value previously set for that key.
When possible, this class tries to behave like a python dict with some particularities that reflect the behavior of the modeler and LSP files: For instance, this class never throws
KeyError
. In LSP querying a non-existent key never fails, but always returnsnil
(turned intoNone
in Python). In addition, settingNone
(nil
) for a value to a key, removes the key and the previous value from the map.Also note that the iterators returned by this class follow the fail-fast convention: if the map is structurally modified after the creation of an iterator (by using the methods
clear
,add
,__setitem__
,__delitem__
), the iterator will throw an exception. In other words, you can’t modify a map while you are iterating over it.Since: 10.0
Summary¶
clear |
Erases all elements from the map. |
append |
Adds the given value to the map. |
__len__ |
Returns the number of elements in the map. |
__contains__ |
Returns true if the given key is defined in the map, false otherwise. |
__setitem__ |
Sets the value associated with the given key. |
__getitem__ |
Returns the value associated with the given key. |
__delitem__ |
Unsets the given key in the map if present. |
__iter__ |
Returns an iterator to the contents of this map. |
Instance methods¶
-
LSPMap.
clear
()¶ Erases all elements from the map. After this call,
len(myMap)
returns zero.
-
LSPMap.
append
(value)¶ Adds the given value to the map. The associated key corresponds to the largest integer key in the map plus one. This function accepts the following types as input:
int
float
str
LSExpression
LSPMap
LSPFunction
LSPModule
Parameters: value – Value to add in the map.
Special operators and methods¶
-
LSPMap.
__len__
()¶ Returns the number of elements in the map. This method is automatically called by python when using special constructs like
len(myMap)
.Returns: The number of elements in the map. Return type: int
-
LSPMap.
__contains__
(key)¶ Returns true if the given key is defined in the map, false otherwise. This method is automatically called by python when using special constructs like
key in myMap
.Parameters: key (any supported type among NoneType
,int
,float
,str
,LSExpression
,LSPMap
,LSPFunction
,LSPModule
) – Key to search.
-
LSPMap.
__setitem__
(key, value)¶ Sets the value associated with the given key. This method accepts the following types for the key and for the value:
NoneType
(only for the value)int
float
str
LSExpression
LSPMap
LSPFunction
LSPModule
Setting a key to
None
has the same effect as calling__delitem__()
on the key.This method is automatically called by python when using special constructs like
myMap[key] = value
.Parameters: - key – Key to set.
- value – Value to set.
-
LSPMap.
__getitem__
(key)¶ Returns the value associated with the given key. If the key doesn’t exist in the map, None is returned. This method can return values of the following types:
NoneType
(nil in LSP)int
float
str
LSExpression
LSPMap
LSPFunction
LSPModule
This method is automatically called by python when using special constructs like
value = myMap[key]
.Parameters: key (any supported type among NoneType
,int
,float
,str
,LSExpression
,LSPMap
,LSPFunction
,LSPModule
) – Key to search
-
LSPMap.
__delitem__
(key)¶ Unsets the given key in the map if present. If the key doesn’t exist in the map, do nothing. This method is automatically called by python when using special constructs like
del myMap[key]
.After calling this method,
key in myMap
returns false andmyMap[key]
returnsNone
.Parameters: key (any supported type among NoneType
,int
,float
,str
,LSExpression
,LSPMap
,LSPFunction
,LSPModule
) – Key to unset.
-
LSPMap.
__iter__
()¶ Returns an iterator to the contents of this map. The iterator yields the (key, value) pairs contained in the map.