{"action":"closed","issue":{"url":"https://api.github.com/repos/yihuang/cprotobuf/issues/12","labels_url":"https://api.github.com/repos/yihuang/cprotobuf/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/yihuang/cprotobuf/issues/12/comments","events_url":"https://api.github.com/repos/yihuang/cprotobuf/issues/12/events","html_url":"https://github.com/yihuang/cprotobuf/issues/12","id":101796831,"number":12,"title":" ProtoEntity对象print问题？","user":{"login":"fordoo","id":2094104,"avatar_url":"https://avatars.githubusercontent.com/u/2094104?v=3","gravatar_id":"","url":"https://api.github.com/users/fordoo","html_url":"https://github.com/fordoo","followers_url":"https://api.github.com/users/fordoo/followers","following_url":"https://api.github.com/users/fordoo/following{/other_user}","gists_url":"https://api.github.com/users/fordoo/gists{/gist_id}","starred_url":"https://api.github.com/users/fordoo/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/fordoo/subscriptions","organizations_url":"https://api.github.com/users/fordoo/orgs","repos_url":"https://api.github.com/users/fordoo/repos","events_url":"https://api.github.com/users/fordoo/events{/privacy}","received_events_url":"https://api.github.com/users/fordoo/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-08-19T03:07:47Z","updated_at":"2015-08-19T04:07:22Z","closed_at":"2015-08-19T04:07:22Z","body":" ProtoEntity对象print问题：\r\n\r\n    def __unicode__(self):\r\n        buf = []\r\n        d = self.__dict__\r\n        for f in self._fields:\r\n            value = d.get(f.name)\r\n            if value == None:\r\n                continue\r\n            if f.repeated:\r\n                buf.append(u'%s = [%s]' % (f.name, u','.join(map(unicode, value))))\r\n            else:\r\n                buf.append(u'%s = %s' % (f.name, unicode(value)))\r\n        return u'\\n'.join(buf)\r\n\r\n当repeated和嵌套稍微多点的时候，因为没有对齐，输出很难找到'['对应的']'，输出例子：\r\n\r\n\ta = [\r\n\tb = [\r\n\tc = 1\r\n\td = [\r\n\te = 2\r\n\t]\r\n\t]\r\n\tf = 3\r\n\t]\r\n\r\n输出看起来很费力，要不要考虑进行优化一下。\r\n\r\n另外我暂时的解决办法是为ProtoEntity增加一个todict的方法：\r\n\r\n    def todict(self):\r\n        data = {}\r\n        d = self.__dict__\r\n        for f in self._fields:\r\n            value = d.get(f.name)\r\n            if value == None:\r\n                continue\r\n            if f.repeated:\r\n                if len(value) < 1:\r\n                    continue\r\n                if isinstance(value[0], ProtoEntity):\r\n                    data[f.name] = [v.todict() for v in value]\r\n                else:\r\n                    data[f.name] = value\r\n            else:\r\n                if isinstance(value, ProtoEntity):\r\n                    data[f.name] = value.todict()\r\n                else:\r\n                    data[f.name] = value\r\n        return data\r\n"}}