Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Commit

Permalink
add __str__()
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiSakuma committed Jul 7, 2018
1 parent 291ae3e commit 705bcce
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
26 changes: 23 additions & 3 deletions alphatwirl/selection/modules/basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Tai Sakuma <tai.sakuma@gmail.com>
import itertools

##__________________________________________________________________||
class Base(object):
Expand All @@ -10,12 +11,31 @@ def __init__(self, name, selections):
self.name = name
self.selections = list(selections)

self._repr_pairs = [
('name', self.name),
('selections', self.selections),
]

def __repr__(self):
return '{}(name={!r}, selections={!r})'.format(
return '{}({})'.format(
self.__class__.__name__,
self.name,
self.selections
', '.join(['{}={!r}'.format(n, v) for n, v in self._repr_pairs]),
)

def __str__(self):
nwidth = max(len(n) for n, _ in self._repr_pairs)
nwidth += 4
nwidth_nested = 12
lines = [
'{}:'.format(self.__class__.__name__),
'{:>{}}: {!r}'.format('name', nwidth, self.name),
'{:>{}}:'.format('selections', nwidth),
]
nested_lines = list(itertools.chain(*[str(s).split('\n') for s in self.selections]))
lines.extend(
['{}{}'.format(' '*nwidth_nested, str(l)) for l in nested_lines]
)
return '\n'.join(lines)

def add(self, selection):
self.selections.append(selection)
Expand Down
28 changes: 24 additions & 4 deletions alphatwirl/selection/modules/with_count.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,33 @@ def __init__(self, name, selections):
self.selections = list(selections)
self.count = Count()

self._repr_pairs = [
('name', self.name),
('selections', self.selections),
('count', self.count),
]

def __repr__(self):
return '{}(name={!r}, selections={!r}, count={!r})'.format(
return '{}({})'.format(
self.__class__.__name__,
self.name,
self.selections,
self.count
', '.join(['{}={!r}'.format(n, v) for n, v in self._repr_pairs]),
)

def __str__(self):
nwidth = max(len(n) for n, _ in self._repr_pairs)
nwidth += 4
nwidth_nested = 12
lines = [
'{}:'.format(self.__class__.__name__),
'{:>{}}: {!r}'.format('name', nwidth, self.name),
'{:>{}}: {!r}'.format('count', nwidth, self.count),
'{:>{}}:'.format('selections', nwidth),
]
nested_lines = list(itertools.chain(*[str(s).split('\n') for s in self.selections]))
lines.extend(
['{}{}'.format(' '*nwidth_nested, str(l)) for l in nested_lines]
)
return '\n'.join(lines)

def add(self, selection):
self.selections.append(selection)
Expand Down
42 changes: 42 additions & 0 deletions tests/unit/selection/modules/test_str.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Tai Sakuma <tai.sakuma@gmail.com>
from __future__ import print_function
import pytest

try:
import unittest.mock as mock
except ImportError:
import mock

from alphatwirl.selection.modules import All, Any, Not
from alphatwirl.selection.modules import AllwCount, AnywCount, NotwCount

##__________________________________________________________________||
all_classes = [All, AllwCount]
all_classe_ids = [c.__name__ for c in all_classes]

any_classes = [Any, AnywCount]
any_classe_ids = [c.__name__ for c in any_classes]

not_classes = [Not, NotwCount]
not_classe_ids = [c.__name__ for c in not_classes]

##__________________________________________________________________||
allany_classes = all_classes + any_classes
allany_classe_ids = all_classe_ids + any_classe_ids

##__________________________________________________________________||
@pytest.fixture()
def mocksel():
ret = mock.MagicMock()
ret.__str__.return_value = '\n'.join(['Mocksel:', ' line1', ' line2'])
return ret

##__________________________________________________________________||
@pytest.mark.parametrize('Class', allany_classes, ids=allany_classe_ids)
def test_allany_str(Class, mocksel):
obj1 = Class(selections=[mocksel, mocksel])
obj0 = Class(selections=[obj1])
str(obj1)
str(obj0)

##__________________________________________________________________||

0 comments on commit 705bcce

Please sign in to comment.