FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
databases/databases/importer.py at drop-python3.6 · pythonran/databases · GitHub
pythonran
/
databases
Public
forked from
encode/databases
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
databases
/
databases
/
importer.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
35 lines (28 loc) · 1.08 KB
Breadcrumbs
databases
/
databases
/
importer.py
Copy path
File metadata and controls
35 lines (28 loc) · 1.08 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import
importlib
import
typing
class
ImportFromStringError
(
Exception
):
pass
def
import_from_string
(
import_str
:
str
)
->
typing
.
Any
:
module_str
,
_
,
attrs_str
=
import_str
.
partition
(
":"
)
if
not
module_str
or
not
attrs_str
:
message
=
(
'Import string "{import_str}" must be in format "<module>:<attribute>".'
)
raise
ImportFromStringError
(
message
.
format
(
import_str
=
import_str
))
try
:
module
=
importlib
.
import_module
(
module_str
)
except
ImportError
as
exc
:
if
exc
.
name
!=
module_str
:
raise
exc
from
None
message
=
'Could not import module "{module_str}".'
raise
ImportFromStringError
(
message
.
format
(
module_str
=
module_str
))
instance
=
module
try
:
for
attr_str
in
attrs_str
.
split
(
"."
):
instance
=
getattr
(
instance
,
attr_str
)
except
AttributeError
as
exc
:
message
=
'Attribute "{attrs_str}" not found in module "{module_str}".'
raise
ImportFromStringError
(
message
.
format
(
attrs_str
=
attrs_str
,
module_str
=
module_str
)
)
return
instance
Back
|
FazBrowse Home
|
New Git URL