Skip to content

Commit 5a26cdf

Browse files
authored
Merge pull request #66 from unode/master
Allow specifying a default value to optional()
2 parents d73aada + f7e81c0 commit 5a26cdf

File tree

5 files changed

+13
-5
lines changed

5 files changed

+13
-5
lines changed

docs/history.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ History and release notes
99

1010
* Dropped support for Python < 3.6
1111
* Added :meth:`Parser.until`. Thanks `@mcdeoliveira <https://github.com/mcdeoliveira>`_!
12+
* `Parser.optional` now supports an optional default argument to be returned instead of ``None``.
1213

1314
1.4.0 - 2021-11-15
1415
------------------

docs/ref/methods_and_combinators.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,17 +130,20 @@ can be used and manipulated as below.
130130
131131
.. versionadded:: 2.0
132132

133-
.. method:: optional()
133+
.. method:: optional([default=None])
134134

135135
Returns a parser that expects the initial parser zero or once, and maps
136-
the result to ``None`` in the case of no match.
136+
the result to a given default value in the case of no match. If no default
137+
value is given, ``None`` is used.
137138

138139
.. code:: python
139140
140141
>>> string('A').optional().parse('A')
141142
'A'
142143
>>> string('A').optional().parse('')
143144
None
145+
>>> string('A').optional('Oops').parse('')
146+
'Oops'
144147
145148
.. method:: map(fn)
146149

docs/tutorial.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,8 @@ failing the whole parse.
296296
multiples of the parser - including :meth:`Parser.many`, :meth:`Parser.times`,
297297
:meth:`Parser.at_most` and :meth:`Parser.at_least`. There is also
298298
:meth:`Parser.optional` which allows matching zero times (in which case the
299-
parser will return ``None``), or exactly once - just what we need in this case.
299+
parser will return the default value specified or ``None`` otherwise),
300+
or exactly once - just what we need in this case.
300301

301302
We also need to do checking on the month and the day. We’ll take a shortcut and
302303
use the built-in ``datetime.date`` class to do the validation for us. However,

src/parsy/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ def at_most(self, n):
178178
def at_least(self, n):
179179
return self.times(n) + self.many()
180180

181-
def optional(self):
182-
return self.times(0, 1).map(lambda v: v[0] if v else None)
181+
def optional(self, default=None):
182+
return self.times(0, 1).map(lambda v: v[0] if v else default)
183183

184184
def until(self, other, min=0, max=float("inf"), consume_other=False):
185185
@Parser

tests/test_parsy.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ def test_optional(self):
418418
p = string("a").optional()
419419
self.assertEqual(p.parse("a"), "a")
420420
self.assertEqual(p.parse(""), None)
421+
p = string("a").optional("b")
422+
self.assertEqual(p.parse("a"), "a")
423+
self.assertEqual(p.parse(""), "b")
421424

422425
def test_sep_by(self):
423426
digit_list = digit.map(int).sep_by(string(","))

0 commit comments

Comments
 (0)