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 eprint #16

Merged
merged 4 commits into from
May 21, 2017
Merged
Changes from 2 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
10 changes: 10 additions & 0 deletions cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import print_function
import inspect
import re
import sys

@@ -20,6 +21,15 @@ def write(self, x):
sys.stderr = flushfile(sys.stderr)
sys.stdout = flushfile(sys.stdout)

def eprint(*objects, end="\n", sep=" "):
Copy link
Member

Choose a reason for hiding this comment

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

Specifying *object first is incompatible with Python 2 https://docs.python.org/2/tutorial/controlflow.html#arbitrary-argument-lists.

Before the variable number of arguments, zero or more normal arguments may occur.

Maybe something like

def eprint(*objects, **kwargs):
    end = kwargs.get("end", "\n")
    sep = kwargs.get("sep", " ")
    
    # ...

would do it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, forgot about Python 2, thanks!

"""
Print an error message to standard error, prefixing it with
file name and line number from which method was called.
"""
(frame, filename, lineno, function, code_context, index) = inspect.stack()[1]
Copy link
Member

Choose a reason for hiding this comment

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

How about just

(filename, lineno) = inspect.stack()[1][1:3]

?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice.

print("{}:{}: ".format(filename, lineno), end="")
print(*objects, end=end, file=sys.stderr, sep=sep)

def get_char():
"""Read a line of text from standard input and return the equivalent char."""
while True: