From b71329e560795a4df84cb419178ef660824f4c0d Mon Sep 17 00:00:00 2001 From: AKFish Date: Wed, 21 Aug 2013 22:25:20 +0800 Subject: [PATCH] Implement data persistence --- github/GithubObject.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/github/GithubObject.py b/github/GithubObject.py index 3fc89992..f20873b9 100644 --- a/github/GithubObject.py +++ b/github/GithubObject.py @@ -24,10 +24,13 @@ # # ################################################################################ +from __future__ import with_statement + import datetime import GithubException +import pickle class _NotSetType: def __repr__(self): @@ -96,6 +99,27 @@ class GithubObject(object): else: return datetime.datetime.strptime(s, "%Y-%m-%dT%H:%M:%SZ") + def save(self, file_name): + ''' + Save instance to a file + + :param file_name: the full path of target file + ''' + + with open(file_name, 'wb') as f: + pickle.dump(self, f) + + @classmethod + def load(cls, file_name): + ''' + Load saved instance from file + :param file_name: the full path to saved file + :rtype: saved instance. The type of loaded instance remains its orginal one and will not be affected by from which derived class the method is called. + ''' + with open(file_name, 'rb') as f: + return pickle.load(f) + + class NonCompletableGithubObject(GithubObject): def _completeIfNeeded(self):