Beispiel #1
0
class GroupSplitSpec(object):
    '''
    A single group-split specification.

    These specifications are oridinarily provided in a list to indicate new groups that one other group is being split
    into.

    TODO: At this point, attributes and relations to be removed are assumed to be identified by their names only and
          not their values (i.e., we use a set to hold the keys that should be removed from the dictionaries for
          attributes and relations).  Perhaps this is not the way to go and we should instead be using both names and
          values.
    '''

    p: float = attrib(
        default=0.0,
        converter=float)  # validator=attr.validators.instance_of(float))
    attr_set: dict = attrib(factory=dict,
                            converter=converters.default_if_none(factory=dict))
    attr_del: set = attrib(factory=set,
                           converter=converters.default_if_none(factory=set))
    rel_set: dict = attrib(factory=dict,
                           converter=converters.default_if_none(factory=dict))
    rel_del: set = attrib(factory=set,
                          converter=converters.default_if_none(factory=set))

    @p.validator
    def is_prob(self, attribute, value):
        if not isinstance(value, float):
            raise TypeError(Err.type('p', 'float'))
        if not (0 <= value <= 1):
            raise ValueError("The probability 'p' must be in [0,1] range.")
Beispiel #2
0
    def test_none_factory(self):
        """
        Factories are used if None is passed.
        """
        c = default_if_none(factory=list)

        assert [] == c(None)

        c = default_if_none(default=Factory(list))

        assert [] == c(None)
Beispiel #3
0
    def test_not_none(self, val):
        """
        If a non-None value is passed, it's handed down.
        """
        c = default_if_none("nope")

        assert val == c(val)

        c = default_if_none(factory=list)

        assert val == c(val)
Beispiel #4
0
    def test_none_value(self):
        """
        Default values are returned when a None is passed.
        """
        c = default_if_none(42)

        assert 42 == c(None)
Beispiel #5
0
class GroupQry(object):
    '''
    Group query.

    Objects of this simple class are used to select groups from a group population using attribute- and relation-based
    search criteria.

    It would make sense to declare this class frozen (i.e., 'frozen=True'), but as is revealsed by the following two
    measurements, performance suffers slightly when slotted classes get frozen.

    python -m timeit -s "import attr; C = attr.make_class('C', ['x', 'y', 'z'], slots=True)"             "C(1,2,3)"
    python -m timeit -s "import attr; C = attr.make_class('C', ['x', 'y', 'z'], slots=True,frozen=True)" "C(1,2,3)"
    '''

    attr: dict = attrib(factory=dict,
                        converter=converters.default_if_none(factory=dict))
    rel: dict = attrib(factory=dict,
                       converter=converters.default_if_none(factory=dict))
Beispiel #6
0
class ProbePersistenceDBItem(object):
    """A description of how a probe will interact with a database for the purpose of data persistence.

    Args:
        name (str): The item's name.
        ins_qry (str): The SQL INSERT query that is used for persisting data in the database.  This query should be
            parameterized to expect the value to be persisted.
        sel_qry (str): The SQL SELECT query that is used for retrieving data from the database (e.g., to generate
            plots).
        ins_val (list): The values to be inserted into the database using the INSERT query.
    """

    probe: Probe = attrib()  # the original Probe object
    name: str = attrib()
    ins_qry: str = attrib(
    )  # insert query (used for persisting data in the DB)
    sel_qry: str = attrib(
    )  # select query (used for retrieving data from the DB, e.g., to generate plots)
    ins_val: list = attrib(factory=list,
                           converter=converters.default_if_none(factory=list))
Beispiel #7
0
 def test_factory_takes_self(self):
     """
     Raises ValueError if passed Factory has takes_self=True.
     """
     with pytest.raises(ValueError, match="takes_self"):
         default_if_none(Factory(list, takes_self=True))
Beispiel #8
0
 def test_too_many_defaults(self):
     """
     Raises TypeError if both default and factory are passed.
     """
     with pytest.raises(TypeError, match="but not both"):
         default_if_none(True, lambda: 42)
Beispiel #9
0
 def test_missing_default(self):
     """
     Raises TypeError if neither default nor factory have been passed.
     """
     with pytest.raises(TypeError, match="Must pass either"):
         default_if_none()