diff --git a/cs50/cs50.py b/cs50/cs50.py index 657f405..edcb52d 100644 --- a/cs50/cs50.py +++ b/cs50/cs50.py @@ -20,20 +20,31 @@ def write(self, x): sys.stderr = flushfile(sys.stderr) sys.stdout = flushfile(sys.stdout) -def get_char(): - """Read a line of text from standard input and return the equivalent char.""" +def get_char(prompt=None): + """ + Read a line of text from standard input and return the equivalent char; + if text is not a single char, user is prompted to retry. If line can't + be read, return None. + """ while True: - s = get_string() + s = get_string(prompt) if s is None: return None if len(s) == 1: return s[0] - print("Retry: ", end="") -def get_float(): - """Read a line of text from standard input and return the equivalent float.""" + # temporarily here for backwards compatibility + if prompt is None: + print("Retry: ", end="") + +def get_float(prompt=None): + """ + Read a line of text from standard input and return the equivalent float + as precisely as possible; if text does not represent a double, user is + prompted to retry. If line can't be read, return None. + """ while True: - s = get_string() + s = get_string(prompt) if s is None: return None if len(s) > 0 and re.search(r"^[+-]?\d*(?:\.\d*)?$", s): @@ -41,12 +52,19 @@ def get_float(): return float(s) except ValueError: pass - print("Retry: ", end="") -def get_int(): - """Read a line of text from standard input and return the equivalent int.""" + # temporarily here for backwards compatibility + if prompt is None: + print("Retry: ", end="") + +def get_int(prompt=None): + """ + Read a line of text from standard input and return the equivalent int; + if text does not represent an int, user is prompted to retry. If line + can't be read, return None. + """ while True: - s = get_string(); + s = get_string(prompt); if s is None: return None if re.search(r"^[+-]?\d+$", s): @@ -56,13 +74,20 @@ def get_int(): return i except ValueError: pass - print("Retry: ", end="") + + # temporarily here for backwards compatibility + if prompt is None: + print("Retry: ", end="") if sys.version_info.major != 3: - def get_long(): - """Read a line of text from standard input and return the equivalent long.""" + def get_long(prompt=None): + """ + Read a line of text from standard input and return the equivalent long; + if text does not represent a long, user is prompted to retry. If line + can't be read, return None. + """ while True: - s = get_string(); + s = get_string(prompt) if s is None: return None if re.search(r"^[+-]?\d+$", s): @@ -70,12 +95,24 @@ def get_long(): return long(s, 10) except ValueError: pass - print("Retry: ", end="") -def get_string(): - """Read a line of text from standard input and return it as a string.""" + # temporarily here for backwards compatibility + if prompt is None: + print("Retry: ", end="") + +def get_string(prompt=None): + """ + Read a line of text from standard input and return it as a string, + sans trailing line ending. Supports CR (\r), LF (\n), and CRLF (\r\n) + as line endings. If user inputs only a line ending, returns "", not None. + Returns None upon error or no input whatsoever (i.e., just EOF). + """ try: + if prompt is not None: + print(prompt, end="") s = sys.stdin.readline() + if not s: + return None return re.sub(r"(?:\r|\r\n|\n)$", "", s) except ValueError: return None diff --git a/setup.py b/setup.py index cee2266..09c4c5e 100644 --- a/setup.py +++ b/setup.py @@ -15,5 +15,5 @@ name="cs50", packages=["cs50"], url="https://github.com/cs50/python-cs50", - version="1.3.0" + version="2.0.0" ) diff --git a/test/python2.py b/test/python2.py index 10f19e4..69de822 100644 --- a/test/python2.py +++ b/test/python2.py @@ -1,6 +1,4 @@ import cs50 -from cs50 import SQL - l = cs50.get_long() print(l) diff --git a/test/python3.py b/test/python3.py index efef779..b545b98 100644 --- a/test/python3.py +++ b/test/python3.py @@ -1,6 +1,4 @@ import cs50 -from cs50 import SQL i = cs50.get_int() print(i) -