Skip to content

Commit 71b92c6

Browse files
committedDec 22, 2017
tweaks per Chad
1 parent 675c047 commit 71b92c6

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed
 

‎src/cs50/__init__.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
import os
22
import sys
33

4+
5+
class CustomImporter(object):
6+
"""
7+
Import cs50.SQL lazily so that rest of library can be used without SQLAlchemy installed.
8+
9+
https://docs.python.org/3/library/imp.html
10+
http://xion.org.pl/2012/05/06/hacking-python-imports/
11+
http://dangerontheranger.blogspot.com/2012/07/how-to-use-sysmetapath-with-python.html
12+
"""
13+
14+
def find_module(self, fullname, path=None):
15+
if fullname == "cs50.SQL":
16+
return self
17+
return None
18+
19+
def load_module(self, name):
20+
if name in sys.modules:
21+
return sys.modules[name]
22+
from .sql import SQL
23+
sys.modules[name] = SQL
24+
return SQL
25+
26+
427
try:
528

629
# Save student's sys.path
@@ -13,33 +36,13 @@
1336
from .cs50 import eprint, get_char, get_float, get_int, get_string
1437
try:
1538
from .cs50 import get_long
16-
except:
39+
except Exception:
1740
pass
1841

19-
# Replace flask's logger
42+
# Replace Flask's logger
2043
from . import flask
2144

22-
class CustomImporter(object):
23-
"""
24-
Import cs50.SQL lazily so that rest of library can be used without SQLAlchemy installed.
25-
26-
https://docs.python.org/3/library/imp.html
27-
http://xion.org.pl/2012/05/06/hacking-python-imports/
28-
http://dangerontheranger.blogspot.com/2012/07/how-to-use-sysmetapath-with-python.html
29-
"""
30-
31-
def find_module(self, fullname, path=None):
32-
if fullname == "cs50.SQL":
33-
return self
34-
return None
35-
36-
def load_module(self, name):
37-
if name in sys.modules:
38-
return sys.modules[name]
39-
from .sql import SQL
40-
sys.modules[name] = SQL
41-
return SQL
42-
45+
# Lazily load CS50.SQL
4346
sys.meta_path.append(CustomImporter())
4447

4548
finally:

0 commit comments

Comments
 (0)
Please sign in to comment.