@@ -22,11 +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 )
2727 self .logger = logging .getLogger ("cs50" )
2828
29- # create engine, raising exception if back end's module not installed
29+ # Create engine, raising exception if back end's module not installed
3030 self .engine = sqlalchemy .create_engine (url , ** kwargs )
3131
3232 def execute (self , text , ** params ):
@@ -81,54 +81,54 @@ def process(value):
8181 elif isinstance (value , sqlalchemy .sql .elements .Null ):
8282 return sqlalchemy .types .NullType ().literal_processor (dialect )(value )
8383
84- # unsupported value
84+ # Unsupported value
8585 raise RuntimeError ("unsupported value" )
8686
87- # process value(s), separating with commas as needed
87+ # Process value(s), separating with commas as needed
8888 if type (value ) is list :
8989 return ", " .join ([process (v ) for v in value ])
9090 else :
9191 return process (value )
9292
93- # allow only one statement at a time
93+ # Allow only one statement at a time
9494 if len (sqlparse .split (text )) > 1 :
9595 raise RuntimeError ("too many statements at once" )
9696
97- # raise exceptions for warnings
97+ # Raise exceptions for warnings
9898 warnings .filterwarnings ("error" )
9999
100- # prepare , execute statement
100+ # Prepare , execute statement
101101 try :
102102
103- # construct a new TextClause clause
103+ # Construct a new TextClause clause
104104 statement = sqlalchemy .text (text )
105105
106- # iterate over parameters
106+ # Iterate over parameters
107107 for key , value in params .items ():
108108
109- # translate None to NULL
109+ # Translate None to NULL
110110 if value is None :
111111 value = sqlalchemy .sql .null ()
112112
113- # 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
114114 # http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.text
115115 statement = statement .bindparams (sqlalchemy .bindparam (
116116 key , value = value , type_ = UserDefinedType ()))
117117
118- # stringify bound parameters
118+ # Stringify bound parameters
119119 # http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html#how-do-i-render-sql-expressions-as-strings-possibly-with-bound-parameters-inlined
120120 statement = str (statement .compile (compile_kwargs = {"literal_binds" : True }))
121121
122- # execute statement
122+ # Execute statement
123123 result = self .engine .execute (statement )
124124
125- # log statement
125+ # Log statement
126126 self .logger .debug (re .sub (r"\n\s*" , " " , sqlparse .format (statement , reindent = True )))
127127
128- # 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
129129 if re .search (r"^\s*SELECT" , statement , re .I ):
130130
131- # coerce any decimal.Decimal objects to float objects
131+ # Coerce any decimal.Decimal objects to float objects
132132 # https://groups.google.com/d/msg/sqlalchemy/0qXMYJvq8SA/oqtvMD9Uw-kJ
133133 rows = [dict (row ) for row in result .fetchall ()]
134134 for row in rows :
@@ -137,21 +137,21 @@ def process(value):
137137 row [column ] = float (row [column ])
138138 return rows
139139
140- # if INSERT, return primary key value for a newly inserted row
140+ # If INSERT, return primary key value for a newly inserted row
141141 elif re .search (r"^\s*INSERT" , statement , re .I ):
142142 if self .engine .url .get_backend_name () in ["postgres" , "postgresql" ]:
143143 result = self .engine .execute (sqlalchemy .text ("SELECT LASTVAL()" ))
144144 return result .first ()[0 ]
145145 else :
146146 return result .lastrowid
147147
148- # if DELETE or UPDATE, return number of rows matched
148+ # If DELETE or UPDATE, return number of rows matched
149149 elif re .search (r"^\s*(?:DELETE|UPDATE)" , statement , re .I ):
150150 return result .rowcount
151151
152- # if some other statement, return True unless exception
152+ # If some other statement, return True unless exception
153153 return True
154154
155- # if constraint violated, return None
155+ # If constraint violated, return None
156156 except sqlalchemy .exc .IntegrityError :
157157 return None
0 commit comments