コード例 #1
0
ファイル: top_level_ast.py プロジェクト: mzpqnxow/mkinit
    def visit_Try(self, node):
        """
        We only care about checking if (a) a variable is defined in the main
        body, and (b) that the variable is defined in all except blacks that
        **don't** immediately re-raise.
        """
        body_attrs = get_conditional_attrnames(node.body)

        orelse_attrs = get_conditional_attrnames(node.orelse)
        # body_attrs.extend(orelse_attrs)
        body_attrs.update(orelse_attrs)

        # Require that attributes are defined in all non-error branches
        required = []
        for handler in node.handlers:
            # Ignore any handlers that will always reraise
            if not any(isinstance(n, ast.Raise) for n in handler.body):
                handler_attrs = get_conditional_attrnames(handler.body)
                required.append(handler_attrs)

        if len(required) == 0:
            common = body_attrs
        else:
            common = oset.intersection(body_attrs, *required)
            # common = set.intersection(set(body_attrs), *map(set, required))
        self._register(sorted(common))
コード例 #2
0
ファイル: test.py プロジェクト: LuminosoInsight/ordered-set
def test_bitwise_and_consistency():
    # Specific case that was failing without explicit __and__ definition
    data1 = OrderedSet([12, 13, 1, 8, 16, 15, 9, 11, 18, 6, 4, 3, 19, 17])
    data2 = OrderedSet([19, 4, 9, 3, 2, 10, 15, 17, 11, 13, 20, 6, 14, 16, 8])
    result1 = data1.copy()
    result1.intersection_update(data2)
    # This requires a custom & operation apparently
    result2 = data1 & data2
    result3 = data1.intersection(data2)
    check_results_([result1, result2, result3], datas=(data1, data2), name='isect')
コード例 #3
0
ファイル: test.py プロジェクト: praiskup/ordered-set
def test_bitwise_and_consistency():
    # Specific case that was failing without explicit __and__ definition
    data1 = OrderedSet([12, 13, 1, 8, 16, 15, 9, 11, 18, 6, 4, 3, 19, 17])
    data2 = OrderedSet([19, 4, 9, 3, 2, 10, 15, 17, 11, 13, 20, 6, 14, 16, 8])
    result1 = data1.copy()
    result1.intersection_update(data2)
    # This requires a custom & operation apparently
    result2 = data1 & data2
    result3 = data1.intersection(data2)
    check_results_([result1, result2, result3], datas=(data1, data2), name='isect')
コード例 #4
0
 def _detect_languages(self, text: str) -> Languages:
     fallback_language = self.languages[0]
     fallback_result = OrderedSet([fallback_language])
     if AnalysisType.MULTILINGUAL in self.analyses:
         polyglot_output = polyglot.detect.Detector(text, quiet=True)
         result = OrderedSet([language.code for language in polyglot_output.languages if language.code != 'un'])
         if not result:
             result = fallback_result
     else:
         result = fallback_result
     result = result.intersection(self.languages)
     return result
コード例 #5
0
 def _detect_languages(self, text: str) -> Languages:
     fallback_language = self.languages[0]
     fallback_result = OrderedSet([fallback_language])
     if not POLYGLOT_AVAILABLE:
         result = fallback_result
     else:
         polyglot_output = polyglot.detect.Detector(text, quiet=True)
         result = OrderedSet([
             language.code for language in polyglot_output.languages
             if language.code != 'un'
         ])
         if not result:
             result = fallback_result
     result = result.intersection(self.languages)
     return result
コード例 #6
0
ファイル: test.py プロジェクト: Erotemic/ordered-set
def test_bitwise_and():
    """
    # xdoctest ~/code/ordered-set/test.py test_bitwise_and
    pytest ~/code/ordered-set/test.py -s -k test_bitwise_and
    """
    import operator
    import itertools as it

    def allsame(iterable, eq=operator.eq):
        iter_ = iter(iterable)
        try:
            first = next(iter_)
        except StopIteration:
            return True
        return all(eq(first, item) for item in iter_)

    def check_results(*results, **kw):
        name = kw.get('name', 'set test')
        datas = kw.get('datas', [])
        if not allsame(results):
            raise AssertionError('Not all same {} for {} with datas={}'.format(
                results, name, datas))
        for a, b in it.combinations(results, 2):
            if not isinstance(a, (bool, int)):
                assert a is not b, name + ' should all be different items'

    data1 = OrderedSet([12, 13, 1, 8, 16, 15, 9, 11, 18, 6, 4, 3, 19, 17])
    data2 = OrderedSet([19, 4, 9, 3, 2, 10, 15, 17, 11, 13, 20, 6, 14, 16, 8])
    print('\ndata1 = {!r}'.format(data1))
    print('data2 = {!r}'.format(data2))
    result1 = data1.copy()
    result1.intersection_update(data2)
    # This requires a custom & operation apparently
    result2 = (data1 & data2)
    result3 = (data1.intersection(data2))
    print('result1 = {!r}'.format(result1))
    print('result2 = {!r}'.format(result2))
    print('result3 = {!r}\n'.format(result3))
    # result1 = OrderedSet([13, 8, 16, 15, 9, 11, 6, 4, 3, 19, 17])
    # result2 = OrderedSet([13, 8, 16, 15, 9, 11, 6, 4, 3, 19, 17])
    # result3 = OrderedSet([13, 8, 16, 15, 9, 11, 6, 4, 3, 19, 17])

    check_results(result1,
                  result2,
                  result3,
                  datas=(data1, data2),
                  name='isect')
コード例 #7
0
ファイル: test_splitters.py プロジェクト: aiswaryasankar/maml
def test_class_splitter_for_fold_overlaps():
    class DemoTask(Task):
        def __init__(self):
            super(DemoTask, self).__init__(index=0, num_classes=None)
            self._inputs = np.arange(10)

        def __len__(self):
            return len(self._inputs)

        def __getitem__(self, index):
            return self._inputs[index]

    splitter = ClassSplitter(shuffle=True, num_train_per_class=5, num_test_per_class=5)
    task = DemoTask()

    all_train_samples = list()
    all_test_samples = list()

    # split task ten times into train and test
    for i in range(10):
        tasks_split = splitter(task)
        train_task = tasks_split["train"]
        test_task = tasks_split["test"]

        train_samples = set([train_task[i] for i in range(len(train_task))])
        test_samples = set([test_task[i] for i in range(len(train_task))])

        # no overlap between train and test splits at single split
        assert len(train_samples.intersection(test_samples)) == 0

        all_train_samples.append(train_samples)
        all_train_samples.append(train_samples)

    # gather unique samples from multiple splits
    samples_in_all_train_splits = OrderedSet().union(*all_train_samples)
    samples_in_all_test_splits = OrderedSet().union(*all_test_samples)

    # no overlap between train and test splits at multiple splits
    assert len(samples_in_all_test_splits.intersection(samples_in_all_train_splits)) == 0
コード例 #8
0
def recall(array1, array2):
    """
    Recall : |a1 \cap a2|/|a1|

    Parameters
    ----------
    array1: np.ndarray
        first array
    array2: np.ndarray
        second array

    Returns
    ----------
    float
        recall

    """
    set_a1 = OrderedSet(array1)
    set_a2 = OrderedSet(array2)
    inters = set_a1.intersection(set_a2)
    print(len(set_a1), " ", len(inters), " ", len(set_a2))
    return len(inters) * 1.0 / len(set_a2)
コード例 #9
0
ファイル: top_level_ast.py プロジェクト: mzpqnxow/mkinit
    def visit_If(self, node):
        """
        Notes:
            elif clauses don't have a special representation in the AST, but
            rather appear as extra If nodes within the orelse section of the
            previous one.
        """
        if isinstance(node.test, ast.Compare):  # pragma: nobranch
            try:
                if all([
                        isinstance(node.test.ops[0], ast.Eq),
                        node.test.left.id == '__name__',
                        node.test.comparators[0].s == '__main__',
                ]):
                    # Ignore main block
                    return
            except Exception:  # nocover
                pass

        # TODO: handled deleted attributes?
        # Find definitions from conditionals that always accept or
        # that are defined in all possible non-rejecting branches (note this
        # requires an else statment). A rejecting branch is one that is
        # unconditionally false or unconditionally raises an exception
        if_node, elif_nodes, else_body = unpack_if_nodes(node)
        test_nodes = [if_node] + elif_nodes

        has_unconditional = False
        required = []

        for item in test_nodes:
            truth = static_truthiness(item.test)
            # if any(isinstance(n, ast.Raise) for n in item.body):
            #     # Ignore branches that simply raise an error
            #     continue
            if truth is _UNHANDLED:
                names = get_conditional_attrnames(item.body)
                required.append(names)
            elif truth is True:
                # Branch is unconditionally true, no need to check others
                names = get_conditional_attrnames(item.body)
                required.append(names)
                has_unconditional = True
                break
            elif truth is False:
                # Ignore branches that are unconditionally false
                continue
            else:
                raise AssertionError('cannot happen')

        if not has_unconditional and else_body:
            # If we havent found an unconditional branch we need an else
            if not any(isinstance(n, ast.Raise) for n in else_body):
                # Ignore else branches that simply raise an error
                names = get_conditional_attrnames(else_body)
                required.append(names)
            has_unconditional = True

        if has_unconditional:
            # We can only gaurentee that something will exist if there is at
            # least one path that must be taken
            if len(required) == 0:
                common = oset()
            elif len(required) == 1:
                common = required[0]
            else:
                common = oset.intersection(*required)
                # common = set.intersection(*map(set, required))
            self._register(sorted(common))
コード例 #10
0
ファイル: __init__.py プロジェクト: tidypython/tipy_select
def one_of(*args):
    args = OrderedSet([*args])
    return SelectorContainer(lambda cols: list(args.intersection(cols)))