Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for optional args for parity with newest C library #17

Merged
merged 2 commits into from
May 21, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 55 additions & 18 deletions cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -20,33 +20,51 @@ 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):
try:
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,26 +74,45 @@ 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):
try:
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -15,5 +15,5 @@
name="cs50",
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="1.3.0"
version="2.0.0"
)
2 changes: 0 additions & 2 deletions test/python2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import cs50

from cs50 import SQL

l = cs50.get_long()
print(l)
2 changes: 0 additions & 2 deletions test/python3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import cs50
from cs50 import SQL

i = cs50.get_int()
print(i)