Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

可否提供 load 方法,用于载入 dump 方法导出的数据,以便于用户缓存数据 #492

Open
Ma233 opened this issue Aug 27, 2020 · 5 comments
Labels
enhancement next breaking changes

Comments

@Ma233
Copy link

Ma233 commented Aug 27, 2020

无法直接通过 __init__ 方法还原一个 dump 出来的数据,所以自己做了一个:

class Object:
    @classmethod
    def load(cls, attrs):
        obj, meta_date = cls(), dict()

        for k in ("createdAt", "updatedAt"):
            v = attrs.pop(k, None)
            if v is not None:
                meta_date[k] = v
                obj._attributes[k] = utils.decode(k, v)

        obj._merge_metadata(meta_date)

        # 此处不能直接 obj.set(attrs),否则 Pointer 类型会被当做字典设置到字段上
        for k, v in attrs.items():
            obj.set(k, v)

        return obj
@Ma233
Copy link
Author

Ma233 commented Aug 27, 2020

leancloud 对象的实现无法 pickle,忧桑...
基于上面的实现,可以将原生对象转为可 pickle 的数据:

class Object:
    def dump_picklable(self):
        return {"class": self._class_name, "dump": self.dump()}

    @classmethod
    def load_picklable(cls, data):
        return cls.extend(data["class"]).load(data["dump"])

@Ma233
Copy link
Author

Ma233 commented Aug 27, 2020

2.2.0 以前的 _merge_metadata 是坏掉的,上面那段代码仅能用于 2.2.0 以后~~o(>_<)o ~~

@weakish
Copy link
Contributor

weakish commented Aug 27, 2020

建议升级到最新版,2.2.0 和最新版之间基本没有破坏兼容性的改动。如果实在要用旧版的话,可以 fork 一份然后 cherry-pick 下 _merge_metadata 的修复 #419

@Ma233
Copy link
Author

Ma233 commented Sep 2, 2020

dump 中没有保存下来被 include 的 Pointer 字段的数据,可能需要单独实现一个 dump;
此外 load 也要可以识别被 dump 下来的 Pointer 字段,最上面那个 load 只会原封不动把 Pointer dump 成的字典存到对应字段上。(已作出修改并标注注释)

@weakish
Copy link
Contributor

weakish commented Sep 2, 2020

dump 中没有保存下来被 include 的 pointer 字段的数据

对,Pointer 不会展开 #399

@weakish weakish added the next breaking changes label Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement next breaking changes
Projects
None yet
Development

No branches or pull requests

2 participants