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 #11

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
40 changes: 27 additions & 13 deletions cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -20,33 +20,39 @@ def write(self, x):
sys.stderr = flushfile(sys.stderr)
sys.stdout = flushfile(sys.stdout)

def get_char():
def get_char(prompt=None):
"""Read a line of text from standard input and return the equivalent char."""
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():
# 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."""
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():
# 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."""
while True:
s = get_string();
s = get_string(prompt);
if s is None:
return None
if re.search(r"^[+-]?\d+$", s):
@@ -56,25 +62,33 @@ 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():
def get_long(prompt=None):
"""Read a line of text from standard input and return the equivalent long."""
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():
# 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."""
try:
if prompt is not None:
print(prompt, end="")
s = sys.stdin.readline()
return re.sub(r"(?:\r|\r\n|\n)$", "", s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should return None if not s, since sys.stdin.readline returns "" on EOF?

f.readline() returns an empty string, the end of the file has been reached

https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects

except ValueError:
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)