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

Improved implementation of resource's repr method #92

Open
wants to merge 1 commit into
base: devel
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions transifex/api/jsonapi/resources.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals

import reprlib
from copy import deepcopy

import requests
Expand Down Expand Up @@ -70,7 +71,7 @@ def _overwrite(
# Ignored
type=None,
# Magic
**kwargs
**kwargs,
):
"""Write to the basic attributes of Resource. Used by '__init__',
'reload', '__copy__' and 'save'
Expand Down Expand Up @@ -862,23 +863,32 @@ def __eq__(self, other):
return self.as_resource_identifier() == other.as_resource_identifier()

def __repr__(self):
r = reprlib.Repr()
r.maxlevel = 2
r.maxdict = 2
r.maxlist = 2
r.maxstring = 20

Comment on lines +866 to +871
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw, this probably should be a module-level const variable, since it's immutable.

if self.__class__ is Resource:
class_name = "Unknown Resource"
else:
class_name = self.__class__.__name__

args = []
if self.id is not None:
details = self.id
args.append(f"id={self.id!r}")
else:
details = "Unsaved"

args.append("id=<Unsaved>")
if self.attributes:
args.append(f"attributes={r.repr(self.attributes)}")
else:
args.append("attributes=<Unfetched>")
if self.relationships:
args.append(
f"relationships={r.repr({key: '...' for key in self.relationships})}"
)
if self.redirect is not None:
details += " (redirect ready)"

if not self.attributes and not self.relationships:
details += " (Unfetched)"

return repr("<{}: {}>".format(class_name, details))
args.append(f"redirect={r.repr(self.redirect)}")
return f"{class_name}({', '.join(args)})"

def __copy__(self):
# Will eventually call `_overwrite` so `deepcopy` will be used
Expand Down