Skip to content

Commit 4f46104

Browse files
committed
styled
1 parent 86704e4 commit 4f46104

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/cs50/sql.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ def __init__(self, url, **kwargs):
2222
http://docs.sqlalchemy.org/en/latest/dialects/index.html
2323
"""
2424

25-
# log statements to standard error
25+
# Log statements to standard error
2626
logging.basicConfig(level=logging.DEBUG)
27+
self.logger = logging.getLogger(__name__)
2728

28-
# create engine, raising exception if back end's module not installed
29+
# Create engine, raising exception if back end's module not installed
2930
self.engine = sqlalchemy.create_engine(url, **kwargs)
3031

3132
def execute(self, text, **params):
@@ -36,11 +37,11 @@ class UserDefinedType(sqlalchemy.TypeDecorator):
3637
"""
3738
Add support for expandable values, a la https://bitbucket.org/zzzeek/sqlalchemy/issues/3953/expanding-parameter.
3839
"""
39-
4040
impl = sqlalchemy.types.UserDefinedType
4141

4242
def process_literal_param(self, value, dialect):
4343
"""Receive a literal parameter value to be rendered inline within a statement."""
44+
4445
def process(value):
4546
"""Render a literal value, escaping as needed."""
4647

@@ -83,48 +84,48 @@ def process(value):
8384
# unsupported value
8485
raise RuntimeError("unsupported value")
8586

86-
# process value(s), separating with commas as needed
87+
# Process value(s), separating with commas as needed
8788
if type(value) is list:
8889
return ", ".join([process(v) for v in value])
8990
else:
9091
return process(value)
9192

92-
# allow only one statement at a time
93+
# Allow only one statement at a time
9394
if len(sqlparse.split(text)) > 1:
9495
raise RuntimeError("too many statements at once")
9596

96-
# raise exceptions for warnings
97+
# Raise exceptions for warnings
9798
warnings.filterwarnings("error")
9899

99-
# prepare, execute statement
100+
# Prepare, execute statement
100101
try:
101102

102-
# construct a new TextClause clause
103+
# Construct a new TextClause clause
103104
statement = sqlalchemy.text(text)
104105

105-
# iterate over parameters
106+
# Iterate over parameters
106107
for key, value in params.items():
107108

108-
# translate None to NULL
109+
# Translate None to NULL
109110
if value is None:
110111
value = sqlalchemy.sql.null()
111112

112-
# bind parameters before statement reaches database, so that bound parameters appear in exceptions
113+
# Bind parameters before statement reaches database, so that bound parameters appear in exceptions
113114
# http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.text
114115
statement = statement.bindparams(sqlalchemy.bindparam(
115116
key, value=value, type_=UserDefinedType()))
116117

117-
# stringify bound parameters
118+
# Stringify bound parameters
118119
# http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html#how-do-i-render-sql-expressions-as-strings-possibly-with-bound-parameters-inlined
119120
statement = str(statement.compile(compile_kwargs={"literal_binds": True}))
120121

121-
# execute statement
122+
# Execute statement
122123
result = self.engine.execute(statement)
123124

124-
# log statement
125-
logging.getLogger("cs50").debug(re.sub(r"\n\s*", " ", sqlparse.format(statement, reindent=True)))
125+
# Log statement
126+
self.logger.debug(re.sub(r"\n\s*", " ", sqlparse.format(statement, reindent=True)))
126127

127-
# if SELECT (or INSERT with RETURNING), return result set as list of dict objects
128+
# If SELECT (or INSERT with RETURNING), return result set as list of dict objects
128129
if re.search(r"^\s*SELECT", statement, re.I):
129130

130131
# coerce any decimal.Decimal objects to float objects
@@ -136,21 +137,21 @@ def process(value):
136137
row[column] = float(row[column])
137138
return rows
138139

139-
# if INSERT, return primary key value for a newly inserted row
140+
# If INSERT, return primary key value for a newly inserted row
140141
elif re.search(r"^\s*INSERT", statement, re.I):
141142
if self.engine.url.get_backend_name() in ["postgres", "postgresql"]:
142143
result = self.engine.execute(sqlalchemy.text("SELECT LASTVAL()"))
143144
return result.first()[0]
144145
else:
145146
return result.lastrowid
146147

147-
# if DELETE or UPDATE, return number of rows matched
148+
# If DELETE or UPDATE, return number of rows matched
148149
elif re.search(r"^\s*(?:DELETE|UPDATE)", statement, re.I):
149150
return result.rowcount
150151

151-
# if some other statement, return True unless exception
152+
# If some other statement, return True unless exception
152153
return True
153154

154-
# if constraint violated, return None
155+
# If constraint violated, return None
155156
except sqlalchemy.exc.IntegrityError:
156157
return None

0 commit comments

Comments
 (0)