Converting Dictionary¶
At the moment, the only class contained in this module is a key converting dictionary, which applies some function (e.g. type conversion function) to all arguments used as keys.
AUTHORS:
Martin von Gagern (2015-01-31): initial version
EXAMPLES:
A KeyConvertingDict
will apply a conversion function to all method
arguments which are keys:
sage: from sage.misc.converting_dict import KeyConvertingDict
sage: d = KeyConvertingDict(int)
sage: d["3"] = 42
sage: list(d.items())
[(3, 42)]
>>> from sage.all import *
>>> from sage.misc.converting_dict import KeyConvertingDict
>>> d = KeyConvertingDict(int)
>>> d["3"] = Integer(42)
>>> list(d.items())
[(3, 42)]
This is used e.g. in the result of a variety, to allow access to the result no matter how a generator is identified:
sage: # needs sage.libs.singular sage.rings.number_field
sage: K.<x,y> = QQ[]
sage: I = ideal([x^2 + 2*y - 5, x + y + 3])
sage: V = sorted(I.variety(AA), key=str)
sage: v = V[0]
sage: v['x'], v['y']
(-2.464101615137755?, -0.535898384862246?)
sage: list(v)[0].parent()
Multivariate Polynomial Ring in x, y over Algebraic Real Field
>>> from sage.all import *
>>> # needs sage.libs.singular sage.rings.number_field
>>> K = QQ['x, y']; (x, y,) = K._first_ngens(2)
>>> I = ideal([x**Integer(2) + Integer(2)*y - Integer(5), x + y + Integer(3)])
>>> V = sorted(I.variety(AA), key=str)
>>> v = V[Integer(0)]
>>> v['x'], v['y']
(-2.464101615137755?, -0.535898384862246?)
>>> list(v)[Integer(0)].parent()
Multivariate Polynomial Ring in x, y over Algebraic Real Field
- class sage.misc.converting_dict.KeyConvertingDict(key_conversion_function, data=None)[source]¶
Bases:
dict
A dictionary which automatically applies a conversions to its keys.
The most common application is the case where the conversion function is the object representing some category, so that key conversion means a type conversion to adapt keys to that category. This allows different representations for keys which in turn makes accessing the correct element easier.
INPUT:
key_conversion_function
– a function which will be applied to all method arguments which represent keysdata
– (optional) dictionary or sequence of key-value pairs to initialize this mapping
EXAMPLES:
sage: from sage.misc.converting_dict import KeyConvertingDict sage: d = KeyConvertingDict(int) sage: d["3"] = 42 sage: list(d.items()) [(3, 42)] sage: d[5.0] = 64 sage: d["05"] 64
>>> from sage.all import * >>> from sage.misc.converting_dict import KeyConvertingDict >>> d = KeyConvertingDict(int) >>> d["3"] = Integer(42) >>> list(d.items()) [(3, 42)] >>> d[RealNumber('5.0')] = Integer(64) >>> d["05"] 64
- pop(key, *args)[source]¶
Remove and retrieve a given element from the dictionary.
INPUT:
key
– a value identifying the element, will be converteddefault
– the value to return if the element is not mapped, optional
EXAMPLES:
sage: from sage.misc.converting_dict import KeyConvertingDict sage: d = KeyConvertingDict(int) sage: d[3] = 42 sage: d.pop("3") 42 sage: d.pop("3", 33) 33 sage: d.pop("3") Traceback (most recent call last): ... KeyError: ...
>>> from sage.all import * >>> from sage.misc.converting_dict import KeyConvertingDict >>> d = KeyConvertingDict(int) >>> d[Integer(3)] = Integer(42) >>> d.pop("3") 42 >>> d.pop("3", Integer(33)) 33 >>> d.pop("3") Traceback (most recent call last): ... KeyError: ...
- setdefault(key, default=None)[source]¶
Create a given mapping unless there already exists a mapping for that key.
INPUT:
key
– a value identifying the element, will be converteddefault
– the value to associate with the key
EXAMPLES:
sage: from sage.misc.converting_dict import KeyConvertingDict sage: d = KeyConvertingDict(int) sage: d.setdefault("3") sage: list(d.items()) [(3, None)]
>>> from sage.all import * >>> from sage.misc.converting_dict import KeyConvertingDict >>> d = KeyConvertingDict(int) >>> d.setdefault("3") >>> list(d.items()) [(3, None)]
- update(*args, **kwds)[source]¶
Update the dictionary with key-value pairs from another dictionary, sequence of key-value pairs, or keyword arguments.
INPUT:
key
– a value identifying the element, will be convertedargs
– a single dict or sequence of pairskwds
– named elements require that the conversion function accept strings
EXAMPLES:
sage: from sage.misc.converting_dict import KeyConvertingDict sage: d = KeyConvertingDict(int) sage: d.update([("3",1),(4,2)]) sage: d[3] 1 sage: d.update({"5": 7, "9": 12}) sage: d[9] 12 sage: d = KeyConvertingDict(QQ['x']) sage: d.update(x=42) sage: d {x: 42}
>>> from sage.all import * >>> from sage.misc.converting_dict import KeyConvertingDict >>> d = KeyConvertingDict(int) >>> d.update([("3",Integer(1)),(Integer(4),Integer(2))]) >>> d[Integer(3)] 1 >>> d.update({"5": Integer(7), "9": Integer(12)}) >>> d[Integer(9)] 12 >>> d = KeyConvertingDict(QQ['x']) >>> d.update(x=Integer(42)) >>> d {x: 42}