Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/_imports.py: 9%

11 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-03 06:10 +0000

1""" 

2A simple utility to import something by its string name. 

3 

4Vendored form ipython_genutils 

5""" 

6 

7# Copyright (c) IPython Development Team. 

8# Distributed under the terms of the Modified BSD License. 

9 

10 

11def import_item(name): 

12 """Import and return ``bar`` given the string ``foo.bar``. 

13 

14 Calling ``bar = import_item("foo.bar")`` is the functional equivalent of 

15 executing the code ``from foo import bar``. 

16 

17 Parameters 

18 ---------- 

19 name : string 

20 The fully qualified name of the module/package being imported. 

21 

22 Returns 

23 ------- 

24 mod : module object 

25 The module that was imported. 

26 """ 

27 

28 parts = name.rsplit(".", 1) 

29 if len(parts) == 2: # noqa 

30 # called with 'foo.bar....' 

31 package, obj = parts 

32 module = __import__(package, fromlist=[obj]) 

33 try: 

34 pak = getattr(module, obj) 

35 except AttributeError: 

36 raise ImportError("No module named %s" % obj) from None 

37 return pak 

38 else: 

39 # called with un-dotted string 

40 return __import__(parts[0])