Skip to content

Commit 9bfcb16

Browse files
committed
Show a maximum of 20 characters in a string's repr() output.
1 parent 1a94271 commit 9bfcb16

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

github2/commits.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from github2.core import BaseData, GithubCommand, Attribute, DateAttribute
1+
from github2.core import (BaseData, GithubCommand, Attribute, DateAttribute,
2+
repr_string)
23

34

45
class Commit(BaseData):
@@ -20,7 +21,7 @@ class Commit(BaseData):
2021
"been modified since last commit.")
2122

2223
def __repr__(self):
23-
return "<Commit: %s %s>" % (self.id[:8], self.message[:64])
24+
return "<Commit: %s %s>" % (self.id[:8], repr_string(self.message))
2425

2526

2627
class Commits(GithubCommand):

github2/core.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,14 @@ def iterate(self):
245245

246246
class BaseData(object):
247247
__metaclass__ = BaseDataType
248+
249+
250+
def repr_string(string):
251+
"""Shorten string for use in repr() output
252+
253+
:param str string: string to operate on
254+
:return: string, with maximum length of 20 characters
255+
"""
256+
if len(string) > 20:
257+
string = string[:17] + '...'
258+
return string.encode('utf-8')

github2/issues.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import urllib
22

3-
from github2.core import GithubCommand, BaseData, Attribute, DateAttribute
3+
from github2.core import (GithubCommand, BaseData, Attribute, DateAttribute,
4+
repr_string)
45

56

67
class Issue(BaseData):
@@ -20,7 +21,7 @@ class Issue(BaseData):
2021
pull_request_url = Attribute("URL for the issue's related pull request.")
2122

2223
def __repr__(self):
23-
return "<Issue: %s>" % self.title.encode('utf-8')
24+
return "<Issue: %s>" % repr_string(self.title)
2425

2526

2627
class Comment(BaseData):
@@ -31,7 +32,7 @@ class Comment(BaseData):
3132
user = Attribute("The username of the user that created this comment.")
3233

3334
def __repr__(self):
34-
return "<Comment: %s>" % self.body
35+
return "<Comment: %s>" % repr_string(self.body)
3536

3637

3738
class Issues(GithubCommand):

github2/pull_requests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from github2.core import BaseData, GithubCommand, Attribute, DateAttribute
1+
from github2.core import (BaseData, GithubCommand, Attribute, DateAttribute,
2+
repr_string)
23

34

45
class PullRequest(BaseData):
@@ -35,7 +36,7 @@ class PullRequest(BaseData):
3536
mergeable = Attribute("Whether the pull request can be merge cleanly")
3637

3738
def __repr__(self):
38-
return "<PullRequest: %s>" % self.title.encode('utf-8')
39+
return "<PullRequest: %s>" % repr_string(self.title)
3940

4041

4142
class PullRequests(GithubCommand):

tests/test_unit.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from nose.tools import (assert_equals, assert_true)
1010

11+
from github2.core import repr_string
1112
from github2.issues import Issue
1213
from github2.client import Github
1314

@@ -48,3 +49,8 @@ def test_project_for_user_repo():
4849
client = Github()
4950
assert_equals(client.project_for_user_repo('JNRowe', 'misc-overlay'),
5051
'JNRowe/misc-overlay')
52+
53+
def test_repr_string():
54+
assert_equals(repr_string('test'), 'test')
55+
assert_equals(repr_string('abcdefghijklmnopqrst'), 'abcdefghijklmnopqrst')
56+
assert_equals(repr_string('abcdefghijklmnopqrstu'), 'abcdefghijklmnopq...')

0 commit comments

Comments
 (0)