Skip to content

Commit caa2ccc

Browse files
author
Kareem Zidane
authored
Merge pull request #94 from cs50/customimporter
reorganized imports
2 parents ddbc69d + bd3a1af commit caa2ccc

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/cs50/flask.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import logging
22

33
from distutils.version import StrictVersion
4-
from os import getenv
54
from pkg_resources import get_distribution
65

76
from .cs50 import _formatException
@@ -44,6 +43,7 @@ def _execute_after(*args, **kwargs):
4443

4544
# When behind CS50 IDE's proxy, ensure that flask.redirect doesn't redirect from HTTPS to HTTP
4645
# https://werkzeug.palletsprojects.com/en/0.15.x/middleware/proxy_fix/#module-werkzeug.middleware.proxy_fix
46+
from os import getenv
4747
if getenv("CS50_IDE_TYPE") == "online":
4848
try:
4949
import flask

src/cs50/sql.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
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-
151
class 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):
366373
def _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):
388398
def _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()

tests/python.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
import cs50
66

7-
i = cs50.get_int()
8-
print(i)
7+
i = cs50.get_int("Input: ")
8+
print(f"Output: {i}")

0 commit comments

Comments
 (0)