File: base.py

package info (click to toggle)
nbclient 0.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 776 kB
  • sloc: python: 2,946; makefile: 18
file content (51 lines) | stat: -rw-r--r-- 2,011 bytes parent folder | download
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import unittest

from nbformat import v4 as nbformat

# mypy: disable-error-code="no-untyped-call,no-untyped-def"


class NBClientTestsBase(unittest.TestCase):
    def build_notebook(self, with_json_outputs=False):
        """Build a notebook in memory for use with NotebookClient tests"""

        outputs = [
            nbformat.new_output("stream", name="stdout", text="a"),
            nbformat.new_output("display_data", data={"text/plain": "b"}),
            nbformat.new_output("stream", name="stdout", text="c"),
            nbformat.new_output("stream", name="stdout", text="d"),
            nbformat.new_output("stream", name="stderr", text="e"),
            nbformat.new_output("stream", name="stderr", text="f"),
            nbformat.new_output("display_data", data={"image/png": "Zw=="}),  # g
            nbformat.new_output("display_data", data={"application/pdf": "aA=="}),  # h
        ]
        if with_json_outputs:
            outputs.extend(
                [
                    nbformat.new_output("display_data", data={"application/json": [1, 2, 3]}),  # j
                    nbformat.new_output(
                        "display_data", data={"application/json": {"a": 1, "c": {"b": 2}}}
                    ),  # k
                    nbformat.new_output("display_data", data={"application/json": "abc"}),  # l
                    nbformat.new_output("display_data", data={"application/json": 15.03}),  # m
                ]
            )

        cells = [
            nbformat.new_code_cell(source="$ e $", execution_count=1, outputs=outputs),
            nbformat.new_markdown_cell(source="$ e $"),
        ]

        return nbformat.new_notebook(cells=cells)

    def build_resources(self):
        """Build an empty resources dictionary."""
        return {"metadata": {}}

    @classmethod
    def merge_dicts(cls, *dict_args):
        # Because this is annoying to do inline
        outcome = {}
        for d in dict_args:
            outcome.update(d)
        return outcome