Skip to content

Commit 7b69487

Browse files
authoredMay 21, 2017
Merge pull request #17 from cs50/optional-args
added support for optional args for parity with newest C library
2 parents 93fa599 + d8a74a3 commit 7b69487

File tree

4 files changed

+56
-23
lines changed

4 files changed

+56
-23
lines changed
 

‎cs50/cs50.py

+55-18
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,51 @@ def write(self, x):
2020
sys.stderr = flushfile(sys.stderr)
2121
sys.stdout = flushfile(sys.stdout)
2222

23-
def get_char():
24-
"""Read a line of text from standard input and return the equivalent char."""
23+
def get_char(prompt=None):
24+
"""
25+
Read a line of text from standard input and return the equivalent char;
26+
if text is not a single char, user is prompted to retry. If line can't
27+
be read, return None.
28+
"""
2529
while True:
26-
s = get_string()
30+
s = get_string(prompt)
2731
if s is None:
2832
return None
2933
if len(s) == 1:
3034
return s[0]
31-
print("Retry: ", end="")
3235

33-
def get_float():
34-
"""Read a line of text from standard input and return the equivalent float."""
36+
# temporarily here for backwards compatibility
37+
if prompt is None:
38+
print("Retry: ", end="")
39+
40+
def get_float(prompt=None):
41+
"""
42+
Read a line of text from standard input and return the equivalent float
43+
as precisely as possible; if text does not represent a double, user is
44+
prompted to retry. If line can't be read, return None.
45+
"""
3546
while True:
36-
s = get_string()
47+
s = get_string(prompt)
3748
if s is None:
3849
return None
3950
if len(s) > 0 and re.search(r"^[+-]?\d*(?:\.\d*)?$", s):
4051
try:
4152
return float(s)
4253
except ValueError:
4354
pass
44-
print("Retry: ", end="")
4555

46-
def get_int():
47-
"""Read a line of text from standard input and return the equivalent int."""
56+
# temporarily here for backwards compatibility
57+
if prompt is None:
58+
print("Retry: ", end="")
59+
60+
def get_int(prompt=None):
61+
"""
62+
Read a line of text from standard input and return the equivalent int;
63+
if text does not represent an int, user is prompted to retry. If line
64+
can't be read, return None.
65+
"""
4866
while True:
49-
s = get_string();
67+
s = get_string(prompt);
5068
if s is None:
5169
return None
5270
if re.search(r"^[+-]?\d+$", s):
@@ -56,26 +74,45 @@ def get_int():
5674
return i
5775
except ValueError:
5876
pass
59-
print("Retry: ", end="")
77+
78+
# temporarily here for backwards compatibility
79+
if prompt is None:
80+
print("Retry: ", end="")
6081

6182
if sys.version_info.major != 3:
62-
def get_long():
63-
"""Read a line of text from standard input and return the equivalent long."""
83+
def get_long(prompt=None):
84+
"""
85+
Read a line of text from standard input and return the equivalent long;
86+
if text does not represent a long, user is prompted to retry. If line
87+
can't be read, return None.
88+
"""
6489
while True:
65-
s = get_string();
90+
s = get_string(prompt)
6691
if s is None:
6792
return None
6893
if re.search(r"^[+-]?\d+$", s):
6994
try:
7095
return long(s, 10)
7196
except ValueError:
7297
pass
73-
print("Retry: ", end="")
7498

75-
def get_string():
76-
"""Read a line of text from standard input and return it as a string."""
99+
# temporarily here for backwards compatibility
100+
if prompt is None:
101+
print("Retry: ", end="")
102+
103+
def get_string(prompt=None):
104+
"""
105+
Read a line of text from standard input and return it as a string,
106+
sans trailing line ending. Supports CR (\r), LF (\n), and CRLF (\r\n)
107+
as line endings. If user inputs only a line ending, returns "", not None.
108+
Returns None upon error or no input whatsoever (i.e., just EOF).
109+
"""
77110
try:
111+
if prompt is not None:
112+
print(prompt, end="")
78113
s = sys.stdin.readline()
114+
if not s:
115+
return None
79116
return re.sub(r"(?:\r|\r\n|\n)$", "", s)
80117
except ValueError:
81118
return None

‎setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
name="cs50",
1616
packages=["cs50"],
1717
url="https://github.com/cs50/python-cs50",
18-
version="1.3.0"
18+
version="2.0.0"
1919
)

‎test/python2.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import cs50
22

3-
from cs50 import SQL
4-
53
l = cs50.get_long()
64
print(l)

‎test/python3.py

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import cs50
2-
from cs50 import SQL
32

43
i = cs50.get_int()
54
print(i)
6-

0 commit comments

Comments
 (0)
Please sign in to comment.