1- import datetime
2- import decimal
3- import importlib
4- import logging
5- import os
6- import re
7- import sqlalchemy
8- import sqlite3
9- import sqlparse
10- import sys
11- import termcolor
12- import warnings
13-
14-
151class SQL (object ):
162 """Wrap SQLAlchemy to provide a simple SQL API."""
173
@@ -25,6 +11,13 @@ def __init__(self, url, **kwargs):
2511 http://docs.sqlalchemy.org/en/latest/dialects/index.html
2612 """
2713
14+ # Lazily import
15+ import logging
16+ import os
17+ import re
18+ import sqlalchemy
19+ import sqlite3
20+
2821 # Get logger
2922 self ._logger = logging .getLogger ("cs50" )
3023
@@ -74,6 +67,13 @@ def connect(dbapi_connection, connection_record):
7467 def execute (self , sql , * args , ** kwargs ):
7568 """Execute a SQL statement."""
7669
70+ # Lazily import
71+ import decimal
72+ import sqlalchemy
73+ import sqlparse
74+ import termcolor
75+ import warnings
76+
7777 # Allow only one statement at a time, since SQLite doesn't support multiple
7878 # https://docs.python.org/3/library/sqlite3.html#sqlite3.Cursor.execute
7979 statements = sqlparse .parse (sql )
@@ -296,8 +296,15 @@ def _escape(self, value):
296296 https://docs.sqlalchemy.org/en/latest/core/type_api.html#sqlalchemy.types.TypeEngine.literal_processor
297297 """
298298
299+ # Lazily import
300+ import sqlparse
301+
299302 def __escape (value ):
300303
304+ # Lazily import
305+ import datetime
306+ import sqlalchemy
307+
301308 # bool
302309 if type (value ) is bool :
303310 return sqlparse .sql .Token (
@@ -366,6 +373,9 @@ def __escape(value):
366373def _parse_exception (e ):
367374 """Parses an exception, returns its message."""
368375
376+ # Lazily import
377+ import re
378+
369379 # MySQL
370380 matches = re .search (r"^\(_mysql_exceptions\.OperationalError\) \(\d+, \"(.+)\"\)$" , str (e ))
371381 if matches :
@@ -388,6 +398,10 @@ def _parse_exception(e):
388398def _parse_placeholder (token ):
389399 """Infers paramstyle, name from sqlparse.tokens.Name.Placeholder."""
390400
401+ # Lazily load
402+ import re
403+ import sqlparse
404+
391405 # Validate token
392406 if not isinstance (token , sqlparse .sql .Token ) or token .ttype != sqlparse .tokens .Name .Placeholder :
393407 raise TypeError ()
0 commit comments