type_name:valuebool:True
int:123
# input
this is
two lines
# output
str:2
this is
two lines
def save(writer, thing):
if isinstance(thing, bool):
print(f"bool:{thing}", file=writer)
elif isinstance(thing, float):
print(f"float:{thing}", file=writer)
elif isinstance(thing, int):
print(f"int:{thing}", file=writer)
# ...29 lines not shown...
else:
raise ValueError(f"unknown type of thing {type(thing)}")
elif isinstance(thing, list):
print(f"list:{len(thing)}", file=writer)
for item in thing:
save(writer, item)
save(sys.stdout, [False, 3.14, "hello", {"left": 1, "right": [2, 3]}])
list:4
bool:False
float:3.14
str:1
hello
dict:2
str:1
left
int:1
str:1
right
list:2
int:2
int:3
def load(reader):
line = reader.readline()[:-1]
assert line, "Nothing to read"
fields = line.split(":", maxsplit=1)
assert len(fields) == 2, f"Badly-formed line {line}"
key, value = fields
if key == "bool":
names = {"True": True, "False": False}
assert value in names, f"Unknown Boolean {value}"
return names[value]
elif key == "float":
return float(value)
elif key == "int":
return int(value)
# ...20 lines not shown...
else:
raise ValueError(f"unknown type of thing {line}")
elif key == "list":
return [load(reader) for _ in range(int(value))]
something,
provide a method _somethingclass SaveObjects:
def __init__(self, writer):
self.writer = writer
def save(self, thing):
typename = type(thing).__name__
method = f"save_{typename}"
assert hasattr(self, method), \
f"Unknown object type {typename}"
getattr(self, method)(thing)
shared = ["shared"]
fixture = [shared, shared]
Store a unique ID for every object using Python's id
Keep track of the objects seen so far
Write that ID the first time we see the object
Write a special entry when we see the object again
def save(self, thing):
thing_id = id(thing)
if thing_id in self.seen:
self._write("alias", thing_id, "")
return
self.seen.add(thing_id)
typename = type(thing).__name__
method = f"save_{typename}"
assert hasattr(self, method), \
f"Unknown object type {typename}"
getattr(self, method)(thing)
word = "word"
child = [word, word]
parent = []
parent.append(parent)
parent.append(child)
saver = SaveAlias(sys.stdout)
saver.save(parent)
list:4539747200:2
alias:4539747200:
list:4539552960:2
str:4539552048:1
word
alias:4539552048: