pyimmutable API¶
ImmutableDict¶
-
class
pyimmutable.ImmutableDict(mapping_or_iterable=(), **kwargs)¶ Return an
ImmutableDictobject initialized from a mapping or iterable (if given) and/or keyword arguments. If called with no arguments, returns the emptyImmutableDict.An
ImmutableDictis, as the name suggests, immutable. The data stored in it remains the same for all of its lifetime. AnImmutableDicthas methods to create furtherImmutableDictobjects as modifications of the current one.There is only ever one
ImmutableDictobject that contains the same data. For example:>>> d1 = ImmutableDict().set("a", 1).set("b", 2) >>> d2 = ImmutableDict(a=1, b=2) >>> d1 is d2 True
-
discard(key)¶ Return a copy with
keyremoved.Returns
selfifkeyis not present.
-
get(key, default=None)¶ Return the value for
keyifkeyis in the dictionary, elsedefault.
-
isImmutableJson¶ Trueifselfonly contains immutable, JSON-serializable data.For this to be True, the
ImmutableDictmust only have keys of typestr. Also, all values must be eitherNone, or of typebool,str,int,float, or be anImmutableDictor anImmutableListwhich itself hasisImmutableJsonset toTrue.
-
items()¶ Return an iterator over
(key, value)tuples.
-
keys()¶ Return an iterator over the keys in this
ImmutableDict.
-
meta¶ A unique dictionary attached to this
ImmutableDictThis property returns a Python dictionary that is attached to the
ImmutableDictobject. The same dictionary is used for the entire lifetime of thisImmutableDictobject. All references to anImmutableDictwith the same contents indeed refer to the sameImmutableDictobject, and therefore all share the samemetadictionary.ImmutableDictobjects may be garbage-collected when there no reference is kept to them, and at that point themetadictionary gets deleted, too.
-
pop(key)¶ Return a copy with
keyremoved.Raises
KeyErrorifkeyis not present.
-
set(key, value)¶ Return a copy with
keyset tovalue.
-
update(mapping_or_iterable=(), **kwargs)¶ Return a new
ImmutableDicton the basis of this one, with keys updated from a mapping or iterable of(key, value)tuples and/or keyword arguments.
-
values()¶ Return an iterator over the values in this
ImmutableDict.
-
ImmutableList¶
-
class
pyimmutable.ImmutableList(sequence=())¶ Return an
ImmutableListobject initialized fromsequence(if given). If called with no arguments, returns the emptyImmutableList.An
ImmutableListis, as the name suggests, immutable. The data stored in it remains the same for all of its lifetime. AnImmutableListhas methods to create furtherImmutableListobjects as modifications of the current one.There is only ever one
ImmutableListobject that contains the same data. For example:>>> l1 = ImmutableList().append('a').append(2) >>> l2 = ImmutableList(['a', 2]) >>> l1 is l2 True
-
append(value)¶ Return a copy with the given
valueappended.
-
count(value)¶ Return number of occurrences of
value.
-
extend(iterable)¶ Return a copy extended by appending elements from
iterable.
-
index(value, start=0, stop=9223372036854775807)¶ Return first index of
value.Raises
ValueErrorifvalueis not present.
-
isImmutableJson¶ Trueif thisImmutableListonly contains immutable, JSON-serializable data.For this to be
True, theImmutableListmust only contain values that are eitherNone, or of typebool,str,int,float, or are anImmutableDictor animmutableListwhich itself hasisImmutableJsonset toTrue.
-
meta¶ This property returns a Python dictionary that is attached to the
ImmutableListobject. The same dictionary is used for the entire lifetime of thisImmutableListobject. All references to anImmutableListwith the same contents indeed refer to the sameImmutableListobject, and therefore all share the samemetadictionary.ImmutableListobjects may be garbage-collected when there no reference is kept to them, and at that point themetadictionary gets deleted, too.
-
set(index, value)¶ Return a copy with item
indexset tovalue.Raises
IndexErrorifindexis outside the range of existing elements.
-
Auxiliary Functions¶
-
pyimmutable.json_dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)¶ Serialize obj` as a JSON and write to
fp.This function makes a deep copy of the object, replacing all sequences (including
ImmutableList) with lists and all mappings (includingImmutableDict) with dicts. The result is passed tojson.dump.All arguments are passed to
json.dump. This function may be replaced by an efficient, native implementation in a later version of pyimmutable, which may not support the same keyword arguments.
-
pyimmutable.json_dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)¶ Serialize
objto a JSON formattedstr.This function makes a deep copy of the object, replacing all sequences (including
ImmutableList) with lists and all mappings (includingImmutableDict) with dicts. The result is passed tojson.dumps.All arguments are passed to
json.dumps. This function may be replaced by an efficient, native implementation in a later version of pyimmutable, which may not support the same keyword arguments.
-
pyimmutable.json_load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)¶ Read JSON from
fpand deserialize as immutable data.This function calls
json.loadand then builds a (potentially nested) structure ofImmutableDict/ImmutableListobjects from the returned object.All arguments are passed to
json.load. This function may be replaced by an efficient, native implementation in a later version of pyimmutable, which may not support the same keyword arguments.
-
pyimmutable.json_loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)¶ Deserialize JSON string as immutable data.
This function calls
json.loadsand then builds a (potentially nested) structure ofImmutableDict/ImmutableListobjects from the returned object.All arguments are passed to
json.loads. This function may be replaced by an efficient, native implementation in a later version of pyimmutable, which may not support the same keyword arguments.
-
pyimmutable.make_immutable(object)¶ Make a deep copy of nested sequences/mappings using
listanddict.A nested structure using
ImmutableDict/ImmutableListobjects is turned into an equivalent structure usingdictandlist, e.g. for passing tojson.dump.
-
pyimmutable.make_mutable(object)¶ Make a deep copy using
ImmutableListandImmutableDict.A nested structure using is turned into one using
ImmutableListfor every sequence andImmutableDictfor every mapping.