• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

manahl / PyBloqs / e0ef5b11-a828-46c3-b0cf-c452bea7e733

18 Sep 2024 08:17PM UTC coverage: 87.335% (-0.04%) from 87.377%
e0ef5b11-a828-46c3-b0cf-c452bea7e733

Pull #112

circleci

rspencer01
Add pytest
Pull Request #112: Upgrade code from python 2

99 of 117 new or added lines in 20 files covered. (84.62%)

17 existing lines in 2 files now uncovered.

2117 of 2424 relevant lines covered (87.33%)

2.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/pybloqs/block/convenience.py
1
from collections.abc import Iterable
3✔
2

3
from pybloqs.block.base import BaseBlock
3✔
4
from pybloqs.block.text import Raw
3✔
5

6
_block_types = {}
3✔
7

8

9
def add_block_types(objects, block_cls):
3✔
10
    if not isinstance(objects, Iterable):
3✔
11
        objects = [objects]
3✔
12
    for o in objects:
3✔
13
        _block_types[o] = block_cls
3✔
14

15

16
# noinspection PyPep8Naming
17
def Block(contents=None, title=None, title_level=3, title_wrap=False, inherit_cfg=True, **kwargs):
3✔
18
    """
19
    Constructs a composable layout element that will be rendered automatically by
20
    IPython Notebooks. It can also be saved in HTML, PDF, PNG or JPEG formats.
21

22
    Content is handled as follows:
23

24
    - Lists, tuples and sets are written out into a grid layout, with a single column
25
      by default. Individual elements of the grid are parsed recursively.
26
    - DataFrames are written out as an interactive HTML table.
27
    - Strings are written out in a raw format, preserving any HTML content in them.
28
    - Nested blocks are simply wrapped, unless there is more than one in which case
29
      the same logic applies as for lists, tuples and sets.
30

31
    :param contents: Contents to put in a block.
32
    :param title: Optional title of the block.
33
    :param title_level: Optional title level (adjusts the size and TOC level), 1 being the
34
                        biggest and 9 being the smallest.
35
    :param title_wrap: Toggles whitespace wrapping of the title. (Default: False).
36
    :param inherit_cfg: Optional. Set to False to ensure that the block does not inherit
37
                        any parameters from parents.
38
    :param cascade_cfg: Set to True to enable parmater cascading from this block. A value
39
                        of False means that child blocks do not inherit parameters from
40
                        this block.
41
    :param kwargs: Optional styling arguments. The `style` keyword argument has special
42
                   meaning in that it allows styling to be grouped as one argument.
43
                   It is also useful in case a styling parameter name clashes with a standard
44
                   block parameter.
45
    :return: A block instance.
46
    """
47
    block_cls = None
3✔
48
    # Need to loop to catch inherited classes as well
49
    for key, value in _block_types.items():
3✔
50
        if isinstance(contents, key):
3✔
51
            block_cls = value
3✔
52
            break
3✔
53

54
    # Try some additional transformations if no suitable mapping found
55
    if block_cls is None:
3✔
56
        if isinstance(contents, str):
3✔
57
            block_cls = Raw
3✔
58
        elif isinstance(contents, BaseBlock):
3✔
59
            # If there is no title, there is no point to wrap the existing block
60
            if title is None:
3✔
61
                return contents
3✔
62

63
            class _NestedBlock(BaseBlock):
×
64
                container_tag = None
×
65

66
                def __init__(self):
×
NEW
67
                    super().__init__(
×
68
                        title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs
69
                    )
70

71
                def _to_static(self):
×
72
                    return contents._to_static()
×
73

74
                def _write_contents(self, *sub_args, **sub_kwargs):
×
75
                    contents._write_block(*sub_args, **sub_kwargs)
×
76

77
            return _NestedBlock()
×
78
        elif contents is None:
3✔
79
            block_cls = Raw
3✔
80
            contents = ""
3✔
81
        else:
NEW
82
            raise ValueError(f"Unrecognized argument type: {type(contents)}")
×
83

84
    return block_cls(
3✔
85
        contents, title=title, title_level=title_level, title_wrap=title_wrap, inherit_cfg=inherit_cfg, **kwargs
86
    )
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc