forked from WolframResearch/WolframClientForPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder2.py
More file actions
37 lines (28 loc) · 933 Bytes
/
Copy pathencoder2.py
File metadata and controls
37 lines (28 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from wolframclient.language import wl
from wolframclient.serializers import export, wolfram_encoder
# define a hierarchy of classes.
class Animal(object):
pass
class Fish(Animal):
pass
class Tuna(Fish):
pass
# will not have its own encoder.
class Salmon(Fish):
pass
# register a new encoder for Animal.
@wolfram_encoder.dispatch(Animal)
def encode_animal(serializer, animal):
return serializer.encode(wl.Animal)
# register a new encoder for Fish.
@wolfram_encoder.dispatch(Fish)
def encode_fish(serializer, animal):
return serializer.encode(wl.Fish)
# register a new encoder for Tuna.
@wolfram_encoder.dispatch(Tuna)
def encode_tuna(serializer, animal):
# encode the class as a function using class name
return serializer.encode(wl.Tuna)
expr = {'fish' : Fish(), 'tuna': Tuna(), 'salmon': Salmon()}
result = export(expr)
print(result) # b'<|"fish" -> Fish, "tuna" -> Tuna, "salmon" -> Fish|>'