def _to_dict(self, serialize=False):
copy_dict = self.__dict__.copy()<EOL>for key, value in vars(self).items():<EOL><INDENT>if serialize and key == '<STR_LIT:id>':<EOL><INDENT>continue<EOL><DEDENT>if not serialize and isinstance(value, BaseObject):<EOL><INDENT>copy_dict[key] = copy_dict.pop(key).to_dict()<EOL><DEDENT>elif serialize and getattr(value, '<STR_LIT>', False):<EOL><INDENT>continue<EOL><DEDENT>elif serialize and key in self._always_dirty:<EOL><INDENT>continue<EOL><DEDENT>elif key in ('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>del copy_dict[key]<EOL><DEDENT>elif serialize and key not in self._dirty_attributes:<EOL><INDENT>del copy_dict[key]<EOL><DEDENT>elif key.startswith('<STR_LIT:_>'):<EOL><INDENT>copy_dict[key[<NUM_LIT:1>:]] = copy_dict[key]<EOL>del copy_dict[key]<EOL><DEDENT><DEDENT>return copy_dict<EOL>
This method works by copying self.__dict__, and removing everything that should not be serialized.