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

neurospin-deepinsight / brainprep / 19471672015

18 Nov 2025 03:32PM UTC coverage: 79.472% (+0.02%) from 79.449%
19471672015

push

github

AGrigis
doc: fix documentation.

1444 of 1817 relevant lines covered (79.47%)

0.79 hits per line

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

84.44
/brainprep/utils/bunch.py
1
##########################################################################
2
# NSAp - Copyright (C) CEA, 2025
3
# Distributed under the terms of the CeCILL-B license, as published by
4
# the CEA-CNRS-INRIA. Refer to the LICENSE file or to
5
# http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
6
# for details.
7
# Authors: The scikit-learn developers
8
# SPDX-License-Identifier: BSD-3-Clause
9
##########################################################################
10

11
"""
12
An extension of dictionaries by enabling values to be accessed by a key or by
13
an attribute.
14
"""
15

16
import warnings
1✔
17
from typing import Any
1✔
18

19

20
class Bunch(dict):
1✔
21
    """Container object exposing keys as attributes.
22

23
    Bunch objects are sometimes used as an output for functions and methods.
24
    They extend dictionaries by enabling values to be accessed by key,
25
    `bunch["value_key"]`, or by an attribute, `bunch.value_key`.
26

27
    Parameters
28
    ----------
29
    **kwargs : Any
30
        Passed to dictionary contructor.
31

32
    Examples
33
    --------
34
    >>> from brainprep.utils import Bunch
35
    >>> b = Bunch(a=1, b=2)
36
    >>> b['b']
37
    2
38
    >>> b.b
39
    2
40
    >>> b.a = 3
41
    >>> b['a']
42
    3
43
    >>> b.c = 6
44
    >>> b['c']
45
    6
46
    """
47

48
    def __init__(self, **kwargs: Any):
1✔
49
        super().__init__(kwargs)
1✔
50

51
        # Map from deprecated key to warning message
52
        self.__dict__["_deprecated_key_to_warnings"] = {}
1✔
53

54
    def __getitem__(self, key):
1✔
55
        if key in self.__dict__.get("_deprecated_key_to_warnings", {}):
1✔
56
            warnings.warn(
×
57
                self._deprecated_key_to_warnings[key],
58
                FutureWarning, stacklevel=2
59
            )
60
        return super().__getitem__(key)
1✔
61

62
    def _set_deprecated(self, value, *, new_key, deprecated_key,
1✔
63
                        warning_message):
64
        """ Set key in dictionary to be deprecated with its warning message.
65
        """
66
        self.__dict__["_deprecated_key_to_warnings"][
×
67
            deprecated_key] = warning_message
68
        self[new_key] = self[deprecated_key] = value
×
69

70
    def __setattr__(self, key, value):
1✔
71
        self[key] = value
1✔
72

73
    def __dir__(self):
1✔
74
        return self.keys()
×
75

76
    def __getattr__(self, key):
1✔
77
        try:
1✔
78
            return self[key]
1✔
79
        except KeyError as exc:
×
80
            raise AttributeError(key) from exc
×
81

82
    def __setstate__(self, state):
1✔
83
        # Bunch pickles generated with scikit-learn 0.16.* have an non
84
        # empty __dict__. This causes a surprising behavior when
85
        # loading these pickles scikit-learn 0.17: reading bunch.key
86
        # uses __dict__ but assigning to bunch.key use __setattr__ and
87
        # only changes bunch['key']. More details can be found at:
88
        # https://github.com/scikit-learn/scikit-learn/issues/6196.
89
        # Overriding __setstate__ to be a noop has the effect of
90
        # ignoring the pickled __dict__
91
        pass
×
92

93
    def __repr__(self):
1✔
94
        lines = []
1✔
95
        for key, item in self.items():
1✔
96
            item_str = repr(item)
1✔
97
            item_str = self._addindent(item_str, 2)
1✔
98
            lines.append(f"{key}: {item_str}")
1✔
99
        main_str = f"{self.__class__.__name__}("
1✔
100
        if lines:
1✔
101
            main_str += "\n  " + "\n  ".join(lines) + "\n"
1✔
102
        main_str += ")"
1✔
103
        return main_str
1✔
104

105
    @classmethod
1✔
106
    def _addindent(cls, s_, num_spaces):
1✔
107
        s = s_.split("\n")
1✔
108
        # don't do anything for single-line stuff
109
        if len(s) == 1:
1✔
110
            return s_
1✔
111
        first = s.pop(0)
1✔
112
        s = [(num_spaces * " ") + line for line in s]
1✔
113
        s = "\n".join(s)
1✔
114
        s = first + "\n" + s
1✔
115
        return s
1✔
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

© 2026 Coveralls, Inc