Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/nbformat/v1/nbbase.py: 22%

32 statements  

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

1"""The basic dict based notebook format. 

2 

3Authors: 

4 

5* Brian Granger 

6""" 

7 

8# ----------------------------------------------------------------------------- 

9# Copyright (C) 2008-2011 The IPython Development Team 

10# 

11# Distributed under the terms of the BSD License. The full license is in 

12# the file LICENSE, distributed as part of this software. 

13# ----------------------------------------------------------------------------- 

14 

15# ----------------------------------------------------------------------------- 

16# Imports 

17# ----------------------------------------------------------------------------- 

18 

19from nbformat._struct import Struct 

20 

21# ----------------------------------------------------------------------------- 

22# Code 

23# ----------------------------------------------------------------------------- 

24 

25 

26class NotebookNode(Struct): 

27 """A notebook node object.""" 

28 

29 pass 

30 

31 

32def from_dict(d): 

33 """Create notebook node(s) from an object.""" 

34 if isinstance(d, dict): 

35 newd = NotebookNode() 

36 for k, v in d.items(): 

37 newd[k] = from_dict(v) 

38 return newd 

39 elif isinstance(d, (tuple, list)): 

40 return [from_dict(i) for i in d] 

41 else: 

42 return d 

43 

44 

45def new_code_cell(code=None, prompt_number=None): 

46 """Create a new code cell with input and output""" 

47 cell = NotebookNode() 

48 cell.cell_type = "code" 

49 if code is not None: 

50 cell.code = str(code) 

51 if prompt_number is not None: 

52 cell.prompt_number = int(prompt_number) 

53 return cell 

54 

55 

56def new_text_cell(text=None): 

57 """Create a new text cell.""" 

58 cell = NotebookNode() 

59 if text is not None: 

60 cell.text = str(text) 

61 cell.cell_type = "text" 

62 return cell 

63 

64 

65def new_notebook(cells=None): 

66 """Create a notebook by name, id and a list of worksheets.""" 

67 nb = NotebookNode() 

68 if cells is not None: 

69 nb.cells = cells 

70 else: 

71 nb.cells = [] 

72 return nb