如何解决Marshmallow 在反序列化时创建重复的 Python 自定义对象
我是一个初学者,我正在尝试通过用 Python 编写一个简单的文本冒险来自学面向对象编程。我的目标是最终在网络上托管游戏,以便我可以邀请朋友来尝试。
游戏中的实体由 Python 对象表示。例如,有一个 Door 类,它包含世界状态属性 open_state(即门是否打开)。反过来,Door 对象是 Room 对象的属性。
应用在控制台上运行没有问题。现在我的目标是重新安排网络使用的代码。为此,我将代码分成两部分:
-
前端代码收集用户输入,将其发送到后端执行代码,然后呈现结果输出。
-
后端执行代码本身,它解释用户输入并确定用户操作的结果。作为后端编码的一部分,我还需要在后端执行调用之间保留我的自定义对象。
我最终计划使用 sqlalchemy 来保持状态,但一开始我只是将我的自定义对象保存到文本文件中。我的方法是使用 Marshmallow 序列化和反序列化为 JSON。但是,因为我在我的对象中引用对象,所以当我从 Marshmallow 反序列化时,我最终会得到重复的对象。这对我的游戏造成了严重破坏并使其无法玩。
有没有一种方法可以使用 Marshmallow(或 Pickle 或任何类似的包)反序列化,它允许我只引用现有的对象实例而不是实例化新的、重复的对象实例?我只是以完全错误的方式解决这个问题吗?我在这个主题上做了很多搜索,但没有找到类似的帖子,所以我怀疑我做错了什么?
非常感谢任何人可以提供的任何帮助或智慧!
# Dark Castle - Minimum Workable Exampe
# Demonstrates Marshmallow duplication issue
# July 16,2021
# imports
from marshmallow import Schema,fields,post_load
import gc
# classes
class Door(object):
def __init__(self,name,desc,open_state):
self.name = name
self.desc = desc
self.open_state = open_state # True if door is open
def __repr__(self):
return f'Object { self.name } is of class { type(self).__name__ } '
class Room(object):
def __init__(self,room_doors):
self.name = name
self.desc = desc
self.room_doors = room_doors # list of door objs in room
def __repr__(self):
return f'Object { self.name } is of class { type(self).__name__ } '
# object instantiation
front_gate = Door('front_gate',"An imposing iron front gate",False)
entrance = Room('entrance',"You are at the castle entrance.",[front_gate])
# marshmallow schemas
class DoorSchema(Schema):
name = fields.String()
desc = fields.String()
open_state = fields.Boolean()
@post_load
def create_door(self,data,**kwargs):
return Door(**data)
class RoomSchema(Schema):
name = fields.String()
desc = fields.String()
room_doors = fields.List(fields.Nested(DoorSchema),allow_none=True)
@post_load
def create_room(self,**kwargs):
return Room(**data)
# check initial Door object count
print("Initial list of door objects:")
for obj in gc.get_objects():
if isinstance(obj,Door):
print(obj,obj.open_state,id(obj))
print()
# serialize to text file
schema_door = DoorSchema()
door_json = schema_door.dumps(front_gate)
schema_room = RoomSchema()
room_json = schema_room.dumps(entrance)
json_lst = [door_json,room_json]
with open('obj_json.txt','w') as f:
for item in json_lst:
f.write("%s\n" % item)
print("JSON output")
print(json_lst)
print()
# delete objects
del json_lst
del front_gate
del entrance
print("Door objects have been deleted:")
for obj in gc.get_objects():
if isinstance(obj,id(obj))
print()
# de-serialize from text file
with open('obj_json.txt','r') as f:
new_json_lst = f.readlines()
print("JSON input")
print(new_json_lst)
print()
new_door_json = new_json_lst[0]
new_room_json = new_json_lst[1]
front_gate = schema_door.loads(new_door_json)
entrance = schema_room.loads(new_room_json)
print("Duplicate de-serialized Door objects:")
for obj in gc.get_objects():
if isinstance(obj,id(obj))
输出:
Initial list of door objects:
Object front_gate is of class Door False 4648526904
JSON output
['{"open_state": false,"name": "front_gate","desc": "An imposing iron front gate"}','{"room_doors": [{"open_state": false,"desc": "An imposing iron front gate"}],"name": "entrance","desc": "You are at the castle entrance."}']
Door objects have been deleted:
JSON input
['{"open_state": false,"desc": "An imposing iron front gate"}\n',"desc": "You are at the castle entrance."}\n']
Duplicate de-serialized Door objects:
Object front_gate is of class Door False 4710446696
Object front_gate is of class Door False 4710446864
解决方法
我很想从比我自己更权威的来源看到答案,但测试表明泡菜很好地解决了这个问题。我一直热衷于人类可读的输出格式,如 JSON。但是如果您的自定义对象包含其他自定义对象,python-native 似乎是要走的路。附带的好处是,它的工作量也比 Marshmallow 少得多 - 无需定义模式或 @post_load 语句。
测试代码:
# Dark Castle - Minimum Viable Example 2
# Will attempt to solve Marshmallow to json object duplication issue w/ pickle
# July 24,2021
# imports
import pickle
import gc
# classes
class Door(object):
def __init__(self,name,desc,open_state):
self.name = name
self.desc = desc
self.open_state = open_state # True if door is open
def __repr__(self):
return f'Object { self.name } is of class { type(self).__name__ } '
class Room(object):
def __init__(self,room_doors):
self.name = name
self.desc = desc
self.room_doors = room_doors # list of door objs in room
def __repr__(self):
return f'Object { self.name } is of class { type(self).__name__ } '
# object instantiation
front_gate = Door('front_gate',"An imposing iron front gate",False)
entrance = Room('entrance',"You are at the castle entrance.",[front_gate])
obj_lst = [front_gate,entrance]
# check initial Door object count
print("Initial list of door objects:")
for obj in gc.get_objects():
if isinstance(obj,Door):
print(obj,obj.open_state,id(obj))
print()
# serialize to pickle file
with open('obj_pickle','wb') as f:
pickle.dump(obj_lst,f)
# delete objects
del obj_lst
del front_gate
del entrance
# check Door object count post delete
print("Door objects have been deleted:")
for obj in gc.get_objects():
if isinstance(obj,id(obj))
print()
# de-serialize from pickle file
with open('obj_pickle','rb') as f:
obj_lst_2 = pickle.load(f)
front_gate = obj_lst_2[0]
entrance = obj_lst_2[1]
# check initial Door object count post de-serialize:
print("de-serialized Door objects:")
for obj in gc.get_objects():
if isinstance(obj,id(obj))
输出:
Initial list of door objects:
Object front_gate is of class Door False 4624118840
Door objects have been deleted:
de-serialized Door objects:
Object front_gate is of class Door False 4624118840
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。