Skip to content

Commit

Permalink
Merge pull request #229 from cs50/regex_module
Browse files Browse the repository at this point in the history
Move regex to module
  • Loading branch information
Jelleas committed Jul 14, 2020
2 parents edba0af + fd73d91 commit 2da62f6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 32 deletions.
6 changes: 3 additions & 3 deletions check50/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ def _setup_translation():
exists,
hash,
include,
regex,
run,
log, _log,
hidden,
Failure, Mismatch
Failure, Mismatch, Missing
)


from . import regex
from .runner import check
from pexpect import EOF

__all__ = ["import_checks", "data", "exists", "hash", "include", "regex",
"run", "log", "Failure", "Mismatch", "check", "EOF"]
"run", "log", "Failure", "Mismatch", "Missing", "check", "EOF"]
30 changes: 1 addition & 29 deletions check50/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import pexpect
from pexpect.exceptions import EOF, TIMEOUT

from . import internal
from . import internal, regex

_log = []
internal.register.before_every(_log.clear)
Expand Down Expand Up @@ -137,34 +137,6 @@ def import_checks(path):
return mod


class regex:
@staticmethod
def decimal(number):
"""
Create a regular expression to match the number exactly:
In case of a positive number:
(?<![\d\-])number(?!(\.?\d))
In case of a negative number:
number(?!(\.?\d))
(?<![\d\-]) = negative lookbehind, \
asserts that there are no digits and no - in front of the number.
(?!(\.?\d)) = negative lookahead, \
asserts that there are no digits and no additional . followed by digits after the number.
:param number: the number to match in the regex
:type number: any numbers.Number (such as int, float, ...)
:rtype: str
Example usage::
# Check that 7.0000 is printed with 5 significant figures
check50.run("./prog").stdout(check50.regex.decimal("7.0000"))
"""
negative_lookbehind = fr"(?<![\d\-])" if number >= 0 else ""
return fr"{negative_lookbehind}{re.escape(str(number))}(?!(\.?\d))"


class run:
"""
Expand Down
33 changes: 33 additions & 0 deletions check50/regex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import re


def decimal(number):
"""
Create a regular expression to match the number exactly:
In case of a positive number::
(?<![\d\-])number(?!(\.?\d))
In case of a negative number::
number(?!(\.?\d))
:code:`(?<![\d\-])` = negative lookbehind, \
asserts that there are no digits and no - in front of the number.
:code:`(?!(\.?\d))` = negative lookahead, \
asserts that there are no digits and no additional . followed by digits after the number.
:param number: the number to match in the regex
:type number: any numbers.Number (such as int, float, ...)
:rtype: str
Example usage::
# Check that 7.0000 is printed with 5 significant figures
check50.run("./prog").stdout(check50.regex.decimal("7.0000"))
"""
negative_lookbehind = fr"(?<![\d\-])" if number >= 0 else ""
return fr"{negative_lookbehind}{re.escape(str(number))}(?!(\.?\d))"
9 changes: 9 additions & 0 deletions docs/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ API docs

.. _check50:


check50
*******

.. automodule:: check50
:members:


check50.c
**********

.. automodule:: check50.c
:members:


check50.flask
****************

Expand All @@ -30,6 +33,12 @@ check50.py
:members:


check50.regex
**************
.. automodule:: check50.regex
:members:


check50.internal
*****************

Expand Down

0 comments on commit 2da62f6

Please sign in to comment.