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

removed get_char, tidied comments #32

Closed
wants to merge 6 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
Prev Previous commit
Next Next commit
removed get_char, tidied comments
dmalan committed Oct 23, 2017
commit c1c61a7dd4fc2c7169c108a42ef607dff27ec46c
28 changes: 9 additions & 19 deletions src/cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import re
import sys


class flushfile():
"""
Disable buffering for standard output and standard error.
@@ -18,9 +19,12 @@ def __getattr__(self, name):
def write(self, x):
self.f.write(x)
self.f.flush()


sys.stderr = flushfile(sys.stderr)
sys.stdout = flushfile(sys.stdout)


def eprint(*args, **kwargs):
"""
Print an error message to standard error, prefixing it with
@@ -32,22 +36,6 @@ def eprint(*args, **kwargs):
print("{}:{}: ".format(filename, lineno), end="")
print(*args, end=end, file=sys.stderr, sep=sep)

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(prompt)
if s is None:
return None
if len(s) == 1:
return s[0]

# temporarily here for backwards compatibility
if prompt is None:
print("Retry: ", end="")

def get_float(prompt=None):
"""
@@ -69,6 +57,7 @@ def get_float(prompt=None):
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;
@@ -82,12 +71,12 @@ def get_int(prompt=None):
if re.search(r"^[+-]?\d+$", s):
try:
i = int(s, 10)
if type(i) is int: # could become long in Python 2
if type(i) is int: # Could become long in Python 2
return i
except ValueError:
pass

# temporarily here for backwards compatibility
# Temporarily here for backwards compatibility
if prompt is None:
print("Retry: ", end="")

@@ -108,10 +97,11 @@ def get_long(prompt=None):
except ValueError:
pass

# temporarily here for backwards compatibility
# 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,
21 changes: 21 additions & 0 deletions test/cs50/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .cs50 import *

class CustomImporter(object):
"""
Import cs50.SQL lazily.

http://dangerontheranger.blogspot.com/2012/07/how-to-use-sysmetapath-with-python.html
"""
def find_module(self, fullname, path=None):
if fullname == "cs50.SQL":
return self
return None
def load_module(self, fullname):
print("1")
if fullname != "cs50.SQL":
raise ImportError(fullname)
print("2")
from .sql import SQL
print("3")
return SQL
sys.meta_path.append(CustomImporter())
81 changes: 81 additions & 0 deletions test/cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from __future__ import print_function
import re
import sys

class flushfile():
"""
Disable buffering for standard output and standard error.

http://stackoverflow.com/a/231216
"""
def __init__(self, f):
self.f = f

def __getattr__(self, name):
return object.__getattribute__(self.f, name)

def write(self, x):
self.f.write(x)
self.f.flush()
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."""
while True:
s = get_string()
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."""
while True:
s = get_string()
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."""
while True:
s = get_string();
if s is None:
return None
if re.search(r"^[+-]?\d+$", s):
try:
i = int(s, 10)
if type(i) is int: # could become long in Python 2
return i
except ValueError:
pass
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."""
while True:
s = get_string();
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."""
try:
s = sys.stdin.readline()
return re.sub(r"(?:\r|\r\n|\n)$", "", s)
except ValueError:
return None
43 changes: 43 additions & 0 deletions test/cs50/sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import sqlalchemy

class SQL(object):
"""TODO"""

def __init__(self, url):
"""TODO"""
try:
self.engine = sqlalchemy.create_engine(url)
except Exception as e:
raise RuntimeError(e)

def execute(self, text, *multiparams, **params):
"""TODO"""
try:

# http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.text
# https://groups.google.com/forum/#!topic/sqlalchemy/FfLwKT1yQlg
# http://docs.sqlalchemy.org/en/latest/core/connections.html#sqlalchemy.engine.Engine.execute
# http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html#how-do-i-render-sql-expressions-as-strings-possibly-with-bound-parameters-inlined
statement = sqlalchemy.text(text).bindparams(*multiparams, **params)
result = self.engine.execute(str(statement.compile(compile_kwargs={"literal_binds": True})))

# SELECT
if result.returns_rows:
rows = result.fetchall()
return [dict(row) for row in rows]

# INSERT
elif result.lastrowid is not None:
return result.lastrowid

# DELETE, UPDATE
else:
return result.rowcount

except sqlalchemy.exc.IntegrityError:
return None

except Exception as e:
raise RuntimeError(e)