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

Respect pep8 and revert 659c8f4 #172

Merged
merged 3 commits into from
Jan 29, 2023
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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -18,5 +18,5 @@
package_dir={"": "src"},
packages=["cs50"],
url="https://github.com/cs50/python-cs50",
version="9.2.4"
version="9.2.5"
)
2 changes: 1 addition & 1 deletion src/cs50/cs50.py
Original file line number Diff line number Diff line change
@@ -135,7 +135,7 @@ def get_string(prompt):
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).
"""
if type(prompt) is not str:
if not isinstance(prompt, str):
raise TypeError("prompt must be of type str")
try:
return input(prompt)
32 changes: 16 additions & 16 deletions src/cs50/sql.py
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@ def connect(dbapi_connection, connection_record):

# Enable foreign key constraints
try:
if type(dbapi_connection) is sqlite3.Connection: # If back end is sqlite
if isinstance(dbapi_connection, sqlite3.Connection): # If back end is sqlite
cursor = dbapi_connection.cursor()
cursor.execute("PRAGMA foreign_keys=ON")
cursor.close()
@@ -350,11 +350,11 @@ def teardown_appcontext(exception):

# Coerce decimal.Decimal objects to float objects
# https://groups.google.com/d/msg/sqlalchemy/0qXMYJvq8SA/oqtvMD9Uw-kJ
if type(row[column]) is decimal.Decimal:
if isinstance(row[column], decimal.Decimal):
row[column] = float(row[column])

# Coerce memoryview objects (as from PostgreSQL's bytea columns) to bytes
elif type(row[column]) is memoryview:
elif isinstance(row[column], memoryview):
row[column] = bytes(row[column])

# Rows to be returned
@@ -432,52 +432,52 @@ def __escape(value):
import sqlalchemy

# bool
if type(value) is bool:
if isinstance(value, bool):
return sqlparse.sql.Token(
sqlparse.tokens.Number,
sqlalchemy.types.Boolean().literal_processor(self._engine.dialect)(value))

# bytes
elif type(value) is bytes:
elif isinstance(value, bytes):
if self._engine.url.get_backend_name() in ["mysql", "sqlite"]:
return sqlparse.sql.Token(sqlparse.tokens.Other, f"x'{value.hex()}'") # https://dev.mysql.com/doc/refman/8.0/en/hexadecimal-literals.html
elif self._engine.url.get_backend_name() == "postgresql":
return sqlparse.sql.Token(sqlparse.tokens.Other, f"'\\x{value.hex()}'") # https://dba.stackexchange.com/a/203359
else:
raise RuntimeError("unsupported value: {}".format(value))

# datetime.date
elif type(value) is datetime.date:
# datetime.datetime
elif isinstance(value, datetime.datetime):
return sqlparse.sql.Token(
sqlparse.tokens.String,
sqlalchemy.types.String().literal_processor(self._engine.dialect)(value.strftime("%Y-%m-%d")))
sqlalchemy.types.String().literal_processor(self._engine.dialect)(value.strftime("%Y-%m-%d %H:%M:%S")))

# datetime.datetime
elif type(value) is datetime.datetime:
# datetime.date
elif isinstance(value, datetime.date):
return sqlparse.sql.Token(
sqlparse.tokens.String,
sqlalchemy.types.String().literal_processor(self._engine.dialect)(value.strftime("%Y-%m-%d %H:%M:%S")))
sqlalchemy.types.String().literal_processor(self._engine.dialect)(value.strftime("%Y-%m-%d")))

# datetime.time
elif type(value) is datetime.time:
elif isinstance(value, datetime.time):
return sqlparse.sql.Token(
sqlparse.tokens.String,
sqlalchemy.types.String().literal_processor(self._engine.dialect)(value.strftime("%H:%M:%S")))

# float
elif type(value) is float:
elif isinstance(value, float):
return sqlparse.sql.Token(
sqlparse.tokens.Number,
sqlalchemy.types.Float().literal_processor(self._engine.dialect)(value))

# int
elif type(value) is int:
elif isinstance(value, int):
return sqlparse.sql.Token(
sqlparse.tokens.Number,
sqlalchemy.types.Integer().literal_processor(self._engine.dialect)(value))

# str
elif type(value) is str:
elif isinstance(value, str):
return sqlparse.sql.Token(
sqlparse.tokens.String,
sqlalchemy.types.String().literal_processor(self._engine.dialect)(value))
@@ -493,7 +493,7 @@ def __escape(value):
raise RuntimeError("unsupported value: {}".format(value))

# Escape value(s), separating with commas as needed
if type(value) in [list, tuple]:
if isinstance(value, (list, tuple)):
return sqlparse.sql.TokenList(sqlparse.parse(", ".join([str(__escape(v)) for v in value])))
else:
return __escape(value)