-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c47f209
commit 96f3059
Showing
3 changed files
with
26 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,32 @@ | ||
""" | ||
Tools to generate __repr__ strings | ||
""" | ||
|
||
from __future__ import unicode_literals | ||
|
||
def make_repr(class_name, *args, **kwargs): | ||
"""Generate a repr string.""" | ||
""" | ||
Generate a repr string. | ||
Positional arguments should be the positional arguments used to | ||
construct the class. Keyword arguments should consist of tuples of | ||
the attribute value and default. If the value is the default, then | ||
it won't be rendered in the output. | ||
Here's an example:: | ||
def __repr__(self): | ||
return make_repr('MyClass', 'foo', name=(self.name, None)) | ||
The output of this would be something line ``MyClass('foo', | ||
name='Will')``. | ||
""" | ||
arguments = [repr(arg) for arg in args] | ||
arguments.extend([ | ||
"{}={!r}".format(name, value) | ||
for name, (value, default) in kwargs.items() | ||
for name, (value, default) in sorted(kwargs.items()) | ||
if value != default | ||
]) | ||
return "{}({})".format(class_name, ', '.join(arguments)) | ||
return "{}({})".format(class_name, ', '.join(arguments)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters