fold.py
This commit is contained in:
parent
5669f2aff1
commit
6bb99b45c3
|
@ -6,5 +6,7 @@ from .error import DecodeError, EncodeError, ShortPacket
|
||||||
from .binary import Decoder, Encoder, decode, decode_with_annotations, encode
|
from .binary import Decoder, Encoder, decode, decode_with_annotations, encode
|
||||||
from .text import Parser, Formatter, parse, parse_with_annotations, stringify
|
from .text import Parser, Formatter, parse, parse_with_annotations, stringify
|
||||||
|
|
||||||
|
from . import fold
|
||||||
|
|
||||||
loads = parse
|
loads = parse
|
||||||
dumps = stringify
|
dumps = stringify
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
from .values import ImmutableDict, dict_kvs, Embedded, Record
|
||||||
|
|
||||||
|
def map_embeddeds(f, v):
|
||||||
|
def walk(v):
|
||||||
|
if isinstance(v, Embedded):
|
||||||
|
return f(v.embeddedValue)
|
||||||
|
elif isinstance(v, (list, tuple)):
|
||||||
|
return tuple(walk(w) for w in v)
|
||||||
|
elif isinstance(v, (set, frozenset)):
|
||||||
|
return frozenset(walk(w) for w in v)
|
||||||
|
elif isinstance(v, dict):
|
||||||
|
return ImmutableDict.from_kvs(walk(w) for w in dict_kvs(v))
|
||||||
|
elif isinstance(v, Record):
|
||||||
|
return Record(walk(v.key), walk(v.fields))
|
||||||
|
else:
|
||||||
|
return v
|
||||||
|
return walk(v)
|
Loading…
Reference in New Issue