【发布时间】:2013-11-05 23:00:15
【问题描述】:
我是一名非程序员,从事简单的 Python 编程,以便在工作中自动执行与 GIS 相关的任务。然而,我发现我真的很喜欢它,并且在工作之外“扩展”了我的技能。
我目前正在使用 Pickle 模块来保存类实例,并意识到我有一些用于构建保存数据的选项。我的问题涉及哪个选项是“更好”或更“可接受/最佳实践”选项,或者是否有更好的选项。
我将使用“playstation-esque”示例,只是为了好玩:
有多个用户。每个用户都有一组特定游戏的奖杯。每个用户还可能有多个特定游戏的玩家资料,每个角色都可以保存多个游戏实例。
因此,当有人登录时,他们会从已保存的用户对象列表中选择他们是哪个用户(或创建一个新对象 - 但现在我将忽略创建新场景)。根据您登录的用户,可以从保存文件以及与该用户关联的任何播放器配置文件中加载适当的奖杯对象。当用户去加载保存的游戏时,他们会选择适当的玩家资料,并且可以列出与该角色相关的适当的保存游戏信息。用户可以选择他们想要的存档,然后加载游戏数据。
在我看来,我有几个选择:
1) 根据类分隔保存文件。例如:
A) "users" - this could contain a list of the User() objects
B) "<username>_trophies" - this could contain a list of the Trophy() objects assotiated with the user indicated in the filename
C) "<username>_characters" - this could contain a list of the Player() objects assotiated with the user indicated in the filename
D) "<username>_<charactername>_save<#>" - this could contain a save of the game info (probably as a dictionary with various info) assotiated with the username and charactername indicated in the filename
使用此设置,可以根据需要取消选择每个文件,因此如果用户从不选择查看他们的奖杯,则无需加载奖杯信息。
但是,我想知道这是否将其分解太多并创建了太多文件。使用这个选项会更好吗?:
2) 拥有一个全面的保存文件,其中包含字典形式的所有信息。例如:
{User:[[Trophy,Trophy,Trophy], {Character: [(game info),
(game info),
(game info)];
Character: [(game info),
(game info),
(game info)]}];
User:[[Trophy,Trophy,Trophy], {Character: [(game info),
(game info),
(game info)];
Character: [(game info),
(game info),
(game info)]}]}
那么这些中哪个“更好”?都好吗?都没有?
感谢所有帮助/意见!
【问题讨论】:
-
您需要并发访问数据吗?您预计总共有多少用户?
-
目前这仅适用于我,用于学习目的,因此“在现实世界中”用户的数量将是一个,或者我为测试/学习目的而创建的用户数量。但我认为“假装”它是为了更多地为了学习如何以“正确”的方式去做,这永远不会有坏处。