Marshmallow 在反序列化时创建重复的 Python 个自定义对象
Marshmallow Creating Duplicate Python Custom Objects on De-Serialization
我是初学者,我正在尝试通过在 Python 中编写一个简单的文本冒险来自学面向对象编程。我的目标是最终将游戏托管在网络上,这样我就可以邀请朋友来试玩。
游戏中的实体由 Python 个对象表示。例如,有一个包含世界状态属性 open_state(即门是否打开)的门 class。反过来,门对象是房间对象的属性。
应用程序在控制台运行没有任何问题。现在我的目标是重新安排网络使用的代码。为此,我将代码分成两部分:
收集用户输入的前端代码,将其发送到后端执行代码,然后呈现结果输出。
解释用户输入并确定用户操作结果的后端执行代码本身。作为后端编码的一部分,我还需要在后端执行调用之间保留我的自定义对象。
我最终计划使用 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, name, desc, 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, data, **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, Door):
print(obj, obj.open_state, 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, Door):
print(obj, obj.open_state, 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, "name": "front_gate", "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, "name": "front_gate", "desc": "An imposing iron front gate"}\n', '{"room_doors": [{"open_state": false, "name": "front_gate", "desc": "An imposing iron front gate"}], "name": "entrance", "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
我很想看到比我自己更权威的来源的答案,但测试表明 pickle 很好地解决了这个问题。我一直热衷于像 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, name, desc, 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, Door):
print(obj, obj.open_state, 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, Door):
print(obj, obj.open_state, 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
我是初学者,我正在尝试通过在 Python 中编写一个简单的文本冒险来自学面向对象编程。我的目标是最终将游戏托管在网络上,这样我就可以邀请朋友来试玩。
游戏中的实体由 Python 个对象表示。例如,有一个包含世界状态属性 open_state(即门是否打开)的门 class。反过来,门对象是房间对象的属性。
应用程序在控制台运行没有任何问题。现在我的目标是重新安排网络使用的代码。为此,我将代码分成两部分:
收集用户输入的前端代码,将其发送到后端执行代码,然后呈现结果输出。
解释用户输入并确定用户操作结果的后端执行代码本身。作为后端编码的一部分,我还需要在后端执行调用之间保留我的自定义对象。
我最终计划使用 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, name, desc, 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, data, **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, Door):
print(obj, obj.open_state, 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, Door):
print(obj, obj.open_state, 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, "name": "front_gate", "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, "name": "front_gate", "desc": "An imposing iron front gate"}\n', '{"room_doors": [{"open_state": false, "name": "front_gate", "desc": "An imposing iron front gate"}], "name": "entrance", "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
我很想看到比我自己更权威的来源的答案,但测试表明 pickle 很好地解决了这个问题。我一直热衷于像 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, name, desc, 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, Door):
print(obj, obj.open_state, 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, Door):
print(obj, obj.open_state, 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