Skip to content

Commit b9a5647

Browse files
committed
Unify stackSenders and stackReceivers.
1 parent a3bc18e commit b9a5647

File tree

2 files changed

+20
-71
lines changed

2 files changed

+20
-71
lines changed

parsley.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,41 +122,24 @@ def makeProtocol(source, senderFactory, receiverFactory, bindings=None,
122122
ParserProtocol, grammar, senderFactory, receiverFactory, bindings)
123123

124124

125-
def stackSenders(*wrappers):
125+
def stack(*wrappers):
126126
"""
127-
Stack some senders for ease of wrapping.
127+
Stack some senders or receivers for ease of wrapping.
128128
129-
``stackSenders(x, y, z)`` will return a sender factory which will, when
130-
called with a transport, return ``x(y(z(transport)))``.
129+
``stack(x, y, z)`` will return a factory usable as a sender or receiver
130+
factory which will, when called with a transport or sender, return
131+
``x(y(z(transport-or-sender)))``.
131132
"""
132133
if not wrappers:
133134
raise TypeError('at least one argument is required')
134-
def senderFactory(transport):
135-
ret = wrappers[-1](transport)
135+
def factory(arg):
136+
ret = wrappers[-1](arg)
136137
for wrapper in wrappers[-2::-1]:
137138
ret = wrapper(ret)
138139
return ret
139-
return senderFactory
140-
141-
142-
def stackReceivers(*wrappers):
143-
"""
144-
Stack some receivers for ease of wrapping.
145-
146-
``stackReceivers(x, y, z)`` will return a receiver factory which, when
147-
called with a sender and parser, will return ``z(y(x(sender, parser)))``.
148-
"""
149-
if not wrappers:
150-
raise TypeError('at least one argument is required')
151-
def receiverFactory(sender, parser):
152-
ret = wrappers[-1](sender, parser)
153-
for wrapper in wrappers[-2::-1]:
154-
ret = wrapper(ret)
155-
return ret
156-
return receiverFactory
157-
140+
return factory
158141

159142
__all__ = [
160143
'makeGrammar', 'wrapGrammar', 'unwrapGrammar', 'term', 'quasiterm',
161-
'makeProtocol', 'stackSenders', 'stackReceivers',
144+
'makeProtocol', 'stack',
162145
]

test_parsley.py

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,68 +12,34 @@ def nullFactory(*args):
1212
return args
1313

1414

15-
class StackSendersTestCase(unittest.TestCase):
15+
class StackTestCase(unittest.TestCase):
1616
def test_onlyBase(self):
17-
"stackSenders can be called with no wrappers."
18-
fac = parsley.stackSenders(nullFactory)
17+
"stack can be called with no wrappers."
18+
fac = parsley.stack(nullFactory)
1919
self.assertEqual(fac('a'), ('a',))
2020

2121
def test_oneWrapper(self):
22-
"stackSenders can be called with one wrapper."
23-
fac = parsley.stackSenders(wrapperFactory(0), nullFactory)
22+
"stack can be called with one wrapper."
23+
fac = parsley.stack(wrapperFactory(0), nullFactory)
2424
self.assertEqual(fac('a'), (0, ('a',)))
2525

2626
def test_tenWrappers(self):
27-
"stackSenders can be called with ten wrappers."
27+
"stack can be called with ten wrappers."
2828
args = []
2929
result = 'a',
3030
for x in xrange(10):
3131
args.append(wrapperFactory(x))
3232
result = 9 - x, result
3333
args.append(nullFactory)
34-
fac = parsley.stackSenders(*args)
34+
fac = parsley.stack(*args)
3535
self.assertEqual(fac('a'), result)
3636

3737
def test_failsWithNoBaseSender(self):
38-
"stackSenders does require at least the base sender factory."
39-
self.assertRaises(TypeError, parsley.stackSenders)
38+
"stack does require at least the base factory."
39+
self.assertRaises(TypeError, parsley.stack)
4040

4141
def test_senderFactoriesTakeOneArgument(self):
42-
"The callable returned by stackSenders takes exactly one argument."
43-
fac = parsley.stackSenders(nullFactory)
42+
"The callable returned by stack takes exactly one argument."
43+
fac = parsley.stack(nullFactory)
4444
self.assertRaises(TypeError, fac)
4545
self.assertRaises(TypeError, fac, 'a', 'b')
46-
47-
48-
class StackReceiversTestCase(unittest.TestCase):
49-
def test_onlyBase(self):
50-
"stackReceivers can be called with no wrappers."
51-
fac = parsley.stackReceivers(nullFactory)
52-
self.assertEqual(fac('a', 'b'), ('a', 'b'))
53-
54-
def test_oneWrapper(self):
55-
"stackReceivers can be called with one wrapper."
56-
fac = parsley.stackReceivers(wrapperFactory(0), nullFactory)
57-
self.assertEqual(fac('a', 'b'), (0, ('a', 'b')))
58-
59-
def test_tenWrappers(self):
60-
"stackReceivers can be called with ten wrappers."
61-
args = []
62-
result = 'a', 'b'
63-
for x in xrange(10):
64-
args.append(wrapperFactory(x))
65-
result = 9 - x, result
66-
args.append(nullFactory)
67-
fac = parsley.stackReceivers(*args)
68-
self.assertEqual(fac('a', 'b'), result)
69-
70-
def test_failsWithNoBaseReceiver(self):
71-
"stackReceivers does require at least the base receiver factory."
72-
self.assertRaises(TypeError, parsley.stackReceivers)
73-
74-
def test_receiverFactoriesTakeTwoArguments(self):
75-
"The callable returned by stackReceivers takes exactly two arguments."
76-
fac = parsley.stackReceivers(nullFactory)
77-
self.assertRaises(TypeError, fac)
78-
self.assertRaises(TypeError, fac, 'a')
79-
self.assertRaises(TypeError, fac, 'a', 'b', 'c')

0 commit comments

Comments
 (0)