Best Python code snippet using Selenium-Requests_python
test_grounding.py
Source:test_grounding.py  
1#from grounding import Grounder2import grounding3from pddl.parser import Parser4from task import Operator5from pddl.pddl import Type, Predicate, Effect, Action, Domain, Problem6def assert_equal(result, expected):7    assert result == expected8def get_action(name, signature, precondition, addlist, dellist):9    effect = Effect()10    effect.addlist = set(addlist)11    effect.dellist = set(dellist)12    return Action(name, signature, precondition, effect)13"""14test domain and problem15"""16# types:17type_object = Type("object", None)18type_vehicle = Type("vehicle", type_object)19type_car = Type("car", type_vehicle)20type_truck = Type("truck", type_vehicle)21type_city = Type("city", type_object)22type_country = Type("country", type_object)23type_my_car = Type("my_car", type_vehicle)24type_color = Type("color", type_object)25types = {"object": type_object, "vehicle": type_vehicle, "car": type_car,26         "truck": type_truck, "city": type_city, "country": type_country,27         "my_car": type_my_car, "color": type_color}28# predicates:29predicate_car_orig = Predicate("at", [("car", types["car"]),30                                      ("orig", types["city"])])31predicate_car_dest = Predicate("at", [("car", types["car"]),32                                      ("dest", types["city"])])33predicate_veh_orig = Predicate("at", [("vehicle", types["vehicle"]),34                                      ("orig", types["city"])])35predicate_veh_dest = Predicate("at", [("vehicle", types["vehicle"]),36                                      ("dest", types["city"])])37predicate_in = Predicate("in", [("car", types["car"]), ("in", types["city"])])38#predicate which does not occur in any operator:39predicate_car_color = Predicate("car_color", [("car", types["car"]),40                                              ("color", types["color"])])41predicate_at = Predicate("at", [("vehicle", types["vehicle"]),42                                ("city", types["city"])])43predicates = {"at": predicate_car_dest, "in": predicate_in,44              "car_color": predicate_car_color}45# actions:46action_drive_car = get_action("DRIVE-CAR", [("car", [types["car"]]),47                         ("orig", [types["city"]]), ("dest", [types["city"]])],48                         [predicate_car_dest], [predicate_car_orig],49                         [predicate_car_dest])50actions = {"drive-car": action_drive_car}51# objects:52objects = {"red_car": types["car"], "green_car": types["car"],53           "blue_truck": types["truck"],  "freiburg": types["city"],54           "basel": types["city"], "green": types["color"],55           "yellow": types["color"]}56# initial and goal state:57initial_state = [Predicate("at", [("red_car", types["car"]),58                                  ("freiburg", types["city"])]),59                 Predicate("at", [("green_car", types["car"]),60                                  ("basel", types["city"])]),61                 Predicate("at", [("blue_truck", types["truck"]),62                                  ("freiburg", types["city"])]),63                 Predicate("at", [("yellow_truck", types["truck"]),64                                  ("basel", types["city"])])]65goal_state = [Predicate("at", [("red_car", types["car"]),66                               ("basel", types["city"])]),67                 Predicate("at", [("green_car", types["car"]),68                                  ("freiburg", types["city"])]),69                 Predicate("at", [("blue_truck", types["truck"]),70                                  ("basel", types["city"])]),71                 Predicate("at", [("yellow_truck", types["truck"]),72                                  ("freiburg", types["city"])])]73# domain and problem74standard_domain = Domain("test_domain_statics", types, predicates, actions)75standard_problem = Problem("test_problem_statics", standard_domain, objects,76                           initial_state, goal_state)77def test_statics1():78    """79    A static predicate is a predicate, which doesn't occur in an effect of an80    action.81    """82    type_object = Type("object", None)83    type_car = Type("car", type_vehicle)84    type_city = Type("city", type_object)85    type_country = Type("country", type_object)86    types = {"object": type_object, "car": type_car, "city": type_city,87             "country": type_country}88    predicate_orig = Predicate("at", [("car", types["car"]),89                                      ("dest", types["city"])])90    predicate_dest = Predicate("at", [("car", types["car"]),91                                      ("orig", types["city"])])92    predicate_in = Predicate("in", [("city", types["city"]),93                                    ("country", types["country"])])94    action_drive_car = get_action("DRIVE-CAR", [("car", [types["car"]]),95                             ("loc-orig", [types["city"]]),96                             ("loc-dest", [types["city"]])],97                             [predicate_orig], [predicate_dest],98                             [predicate_orig])99    expected = [("in", grounding._get_statics([predicate_in],100                                              [action_drive_car]), True),101                ("dest", grounding._get_statics([predicate_dest],102                                                [action_drive_car]), False),103                ("orig", grounding._get_statics([predicate_orig],104                                                [action_drive_car]), False)]105    for pre, statics, element in expected:106        yield in_statics, pre, statics, element107def test_statics2():108    type_object = Type("object", None)109    predicate_a = Predicate("a", [])110    predicate_b = Predicate("b", [])111    the_action = get_action("the-action", [], [predicate_a], [predicate_b], [])112    statics = grounding._get_statics([predicate_a, predicate_b], [the_action])113    assert predicate_a.name in statics and predicate_b.name not in statics114def in_statics(predicate, statics, element):115    if element:116        assert predicate in statics117    else:118        assert not predicate in statics119def test_type_map1():120    """type map: maps each type to a list of objects"""121    type_object = Type("object", None)122    type_vehicle = Type("vehicle", type_object)123    type_car = Type("car", type_vehicle)124    type_truck = Type("truck", type_vehicle)125    type_city = Type("city", type_object)126    objects = {"red_car": type_car, "green_car": type_car,127               "blue_truck": type_truck, "motorbike": type_vehicle,128               "freiburg": type_city, "basel": type_city}129    type_map = grounding._create_type_map(objects)130    expected = [("red_car", type_map[type_car]),131                ("green_car", type_map[type_car]),132                ("blue_truck", type_map[type_truck]),133                ("red_car", type_map[type_vehicle]),134                ("green_car", type_map[type_vehicle]),135                ("blue_truck", type_map[type_vehicle]),136                ("motorbike", type_map[type_vehicle]),137                ("freiburg", type_map[type_city]),138                ("basel", type_map[type_city]),139                ("green_car", type_map[type_object]),140                ("motorbike", type_map[type_object]),141                ("basel", type_map[type_object])]142    for object, object_list in expected:143        yield in_object_set, object, object_list144def test_type_map2():145    type_object = Type("object", None)146    objects = {"object1": type_object}147    type_map = grounding._create_type_map(objects)148    assert "object1" in type_map[type_object]149def in_object_set(object, object_list):150    assert object in object_list151def test_collect_facts():152    op1 = Operator("op1", {"var1"}, {}, {"var3"})153    op2 = Operator("op2", {"var2"}, {"var3"}, {})154    op3 = Operator("op3", {}, {"var1"}, {"var4"})155    assert {"var1", "var2", "var3", "var4"} == grounding._collect_facts(156                                                               [op1, op2, op3])157def test_operators():158    # action with signature with 2 types159    action_drive_vehicle = get_action("DRIVE-VEHICLE",160                                      [("vehicle", [types["car"],161                                                    types["truck"]]),162                                       ("orig", [types["city"]]),163                                       ("dest", [types["city"]])],164                                      [predicate_veh_orig],165                                      [predicate_veh_dest],166                                      [predicate_veh_orig])167    # action with predicate in add & delete list168    action_add_delete = get_action("STAY", [("car", [types["car"]]),169                                            ("in", [types["city"]])],170                                   [predicate_in], [predicate_in],171                                   [predicate_in])172    # action with constant input173    action_constant = get_action("CONSTANT-ACTION",174                                 [("my_car", [types["my_car"]]),175                                  ("city", [types["city"]])],176                                 [],177                                 [Predicate("in", [("basel", [types["city"]]),178                                                   ("switzerland",179                                                    [types["country"]])])], [])180    # action with only delete effects181    action_only_delete = get_action("LEAVE",182                                    [("car", [types["car"]]),183                                     ("in", [types["city"]])],184                                    [predicate_in], [], [predicate_in])185    # action with delete effect which does not occur in precondition186    action_delete = get_action("DELETE", [("car", [types["car"]]),187                                          ("orig", [types["city"]]),188                                          ("dest", [types["city"]])],189                               [], [predicate_car_orig], [predicate_car_dest])190    type_map = grounding._create_type_map(objects)191    grounded_initial_state = grounding._get_partial_state(initial_state)192    grounded_drive_car = list(193        grounding._ground_action(action_drive_car, type_map, [],194                                 grounded_initial_state))195    grounded_drive_vehicle = list(196        grounding._ground_action(action_drive_vehicle, type_map, [],197                                 grounded_initial_state))198    grounded_add_delete = list(199        grounding._ground_action(action_add_delete, type_map, [],200                                 grounded_initial_state))201    grounded_only_delete = list(202        grounding._ground_action(action_only_delete, type_map, [],203                                 grounded_initial_state))204    grounded_delete = list(205        grounding._ground_action(action_delete, type_map, [],206                                 grounded_initial_state))207    domain = Domain("test_domain", types,208                    {"in": Predicate("in", [("city", types["city"]),209                                            ("country", types["country"])])},210                    {"action-constant": action_constant},211                    {"my_car": types["car"]})212    problem = Problem("test_problem", domain, objects, initial_state,213                      goal_state)214    task = grounding.ground(problem)215    grounded_constant = task.operators216    expected = [("(DRIVE-CAR red_car freiburg basel)", grounded_drive_car),217                ("(DRIVE-VEHICLE blue_truck freiburg basel)",218                 grounded_drive_vehicle),219                ("(STAY red_car freiburg)", grounded_add_delete),220                ("(LEAVE red_car freiburg)", grounded_only_delete),221                ("(DELETE red_car freiburg basel)", grounded_delete)]222    for operator, grounded_operators in expected:223        yield operator_grounded, operator, grounded_operators224def operator_grounded(operator, grounded_operators):225    grounded = False226    for op in grounded_operators:227        if(operator == op.name):228            grounded = True229    assert grounded230def test_create_operator():231    statics = grounding._get_statics(standard_domain.predicates.values(),232                                     [action_drive_car])233    initial_state = [Predicate("at", [("ford", types["car"]),234                                      ("freiburg", types["city"])])]235    operator = grounding._create_operator(236        action_drive_car,237        {"car": "ford", "dest": "berlin", "orig": "freiburg"},238        [], initial_state)239    assert operator.name == "(DRIVE-CAR ford freiburg berlin)"240    assert operator.preconditions == {'(at ford berlin)'}241    assert operator.add_effects == {'(at ford freiburg)'}242    assert operator.del_effects == {'(at ford berlin)'}243def test_get_grounded_string():244    grounded_string = "(DRIVE-CAR ford freiburg berlin)"245    assert grounding._get_grounded_string(246        "DRIVE-CAR", ["ford", "freiburg", "berlin"]) == grounded_string247def test_ground():248    """249    predicate which does not occur in any operator: "car_color"250    -> does it occurs in a variable?251    -> does it occur in an operator?252    """253    task = grounding.ground(standard_problem)254    assert not any(var.startswith("car_color") for var in task.facts)255    for operators in task.operators:256        assert not any(pre.startswith("car_color")257                       for pre in operators.preconditions)258        assert not any(add.startswith("car_color")259                       for add in operators.add_effects)260        assert not any(dee.startswith("car_color")261                       for dee in operators.del_effects)262def test_regression():263    parser = Parser('')264    def parse_problem(domain, problem):265        parser.domInput = domain266        parser.probInput = problem267        domain = parser.parse_domain(False)268        return parser.parse_problem(domain, False)269    prob_05 = """270    ;; See domain file for description of this test.271    (define (problem regression-test-05)272      (:domain regression-test)273      (:objects y - object)274      (:init)275      (:goal (the-predicate x y)))276    """277    dom_05 = """278    ;; Expected behaviour: plan of length one found279    ;; Observed behaviour (r265): plan of length zero found280    (define (domain regression-test)281      (:requirements :typing) ;; work around problem in regression test #4.282      (:predicates (the-predicate ?v1 ?v2 - object))283      (:constants x - object)284      (:action theaction285       :parameters (?x - object)286       :precondition (and)287       :effect (the-predicate x ?x)288      )289    )290    """291    prob_06 = """292    ;; See domain file for description of this test.293    (define (problem regression-test-06)294      (:domain regression-test)295      (:objects y - object)296      (:init)297      (:goal (the-predicate y y)))298    """299    dom_06 = """300    ;; Expected behaviour: planner proves that no plan exists301    ;; Observed behaviour (r265): plan of length one found302    (define (domain regression-test)303      (:requirements :typing) ;; work around problem in regression test #4.304      (:predicates (the-predicate ?v1 ?v2 - object))305      (:constants x - object)306      (:action theaction307       :parameters (?x - object)308       :precondition (and)309       :effect (the-predicate x ?x)310      )311    )312    """313    # problem / domain 07 contains a different action compared314    # to the actions of domain 5 & 6315    prob_07 = prob_06316    dom_07 = """317    (define (domain regression-test)318      (:requirements :typing) ;; work around problem in regression test #4.319      (:predicates (the-predicate ?v1 ?v2 - object))320      (:constants y - object)321      (:action theaction322       :parameters (?x - object)323       :precondition (and)324       :effect (the-predicate y ?x)325      )326    )327    """328    # action of problem / domain 8 differs only in the variable name compared329    # to the actions of problem 5 and 6: After grounding there should be no330    # difference between the grounded actions331    prob_08 = prob_05332    dom_08 = """333    (define (domain regression-test)334      (:requirements :typing) ;; work around problem in regression test #4.335      (:predicates (the-predicate ?v1 ?v2 - object))336      (:constants x - object)337      (:action theaction338       :parameters (?z - object)339       :precondition (and)340       :effect (the-predicate x ?z)341      )342    )343    """344    parsed_problem5 = parse_problem(dom_05, prob_05)345    parsed_problem6 = parse_problem(dom_06, prob_06)346    parsed_problem7 = parse_problem(dom_07, prob_07)347    parsed_problem8 = parse_problem(dom_08, prob_08)348    #coded input:349    type_object = Type("object", None)350    types = {"object": type_object}351    predicates = {"the_predicate": Predicate("the-predicate",352                                             [("v1", type_object),353                                              ("v2", type_object)])}354    constants = {"x": type_object}355    actions = {"theaction": get_action("theaction",356                                       [("?x", [type_object])], [],357                                       [Predicate("the-predicate",358                                        [("x", type_object),359                                         ("?x", type_object)])], [])}360    domain = Domain("regression-test", types, predicates, actions, constants)361    problem5 = Problem("regression-test-05", domain, {"y": type_object}, [],362                       [Predicate("the-predicate", [("x", type_object),363                                                    ("y", type_object)])])364    problem6 = Problem("regression-test-06", domain, {"y": type_object}, [],365                       [Predicate("the-predicate", [("y", type_object),366                                                    ("y", type_object)])])367    parsed_task5 = grounding.ground(parsed_problem5)368    coded_task5 = grounding.ground(problem5)369    parsed_task6 = grounding.ground(parsed_problem6)370    coded_task6 = grounding.ground(problem6)371    parsed_task7 = grounding.ground(parsed_problem7)372    parsed_task8 = grounding.ground(parsed_problem8)373    expected = [(parsed_task5.operators, coded_task5.operators, True),374                (parsed_task6.operators, coded_task6.operators, True),375                (parsed_task5.operators, coded_task6.operators, True),376                (parsed_task5.operators, parsed_task7.operators, False),377                (parsed_task5.operators, parsed_task8.operators, True)]378    for operator1, operator2, expected_result in expected:379        yield compare_operators, operator1, operator2, expected_result380def compare_operators(operators1, operators2, expected):381    def compare_operator(operator1, operator2):382        return (operator1.name == operator2.name and383                operator1.preconditions == operator2.preconditions and384                operator1.add_effects == operator2.add_effects and385                operator1.del_effects == operator2.del_effects)386    for operator1 in operators1:387        if(not(any(compare_operator(operator1, operator2)388                   for operator2 in operators2))):389            return False == expected390    return True == expected391def test_add_del_effects():392    parser = Parser('')393    def parse_problem(domain, problem):394        parser.domInput = domain395        parser.probInput = problem396        domain = parser.parse_domain(False)397        return parser.parse_problem(domain, False)398    dom_pddl = """399    (define (domain dom)400      (:requirements :typing)401      (:predicates (ok ?v - object))402      (:action theaction403       :parameters (?x - object)404       :precondition {0}405       :effect {1}406      )407    )408    """409    prob_pddl = """410    ;; See domain file for description of this test.411    (define (problem prob)412      (:domain dom)413      (:objects y - object)414      (:init)415      (:goal (ok y)))416    """417    tests = [418        # Only add effect419        ('(and)', '(ok ?x)', set(), {'(ok y)'}, set()),420        # Only delete effect421        ('(and)', '(and (not (ok ?x)))', set(), set(), {'(ok y)'}),422        # Both add and delete effect423        ('(and)', '(and (ok ?x) (not (ok ?x)))', set(), {'(ok y)'}, set()),424        # Precondition and add effect425        ('(and (ok ?x))', '(ok ?x)', {'(ok y)'}, set(), set()),426        # Precondition and delete effect427        ('(and (ok ?x))', '(and (not (ok ?x)))', {'(ok y)'}, set(),428         {'(ok y)'}),429        # Precondition and both add and delete effect430        ('(and (ok ?x))', '(and (ok ?x) (not (ok ?x)))', {'(ok y)'}, set(),431         set()),432            ]433    for pre_in, eff_in, pre_exp, add_exp, del_exp in tests:434        dom = dom_pddl.format(pre_in, eff_in)435        problem = parse_problem(dom, prob_pddl)436        domain = problem.domain437        actions = domain.actions.values()438        predicates = domain.predicates.values()439        # Objects440        objects = problem.objects441        objects.update(domain.constants)442        # Get the names of the static predicates443        statics = grounding._get_statics(predicates, actions)444        # Create a map from types to objects445        type_map = grounding._create_type_map(objects)446        # Transform initial state into a specific447        init = grounding._get_partial_state(problem.initial_state)448        # Ground actions449        operators = grounding._ground_actions(actions, type_map, statics, init)450        yield assert_equal, len(operators), 1451        op = operators[0]452        yield assert_equal, op.preconditions, pre_exp453        yield assert_equal, op.add_effects, add_exp...entities.py
Source:entities.py  
1"""Backends for Virtuoso (graph database)2The UWKGM project3:copyright: (c) 2020 Ichise Laboratory at NII & AIST4:author: Rungsiman Nararatwong5"""6import time7from socket import timeout8from typing import Any, Dict, List, Union9from uwkgm import db10def find(graph: str, uris: List[str], language: str, query_limit: Union[int, None],11         include_incomings: bool, include_outgoings: bool) -> dict:12    """Find triples of which the given entity is a subject or an object"""13    def fetch(direction: str) -> List[dict]:14        query_main = f'?{target} ?predicate <{uri}>' if direction == 'incoming' else f'<{uri}> ?predicate ?{target}'15        query_lang = f"""16                       FILTER (!isLiteral(?predicate_label) || langMatches(lang(?predicate_label), '') || langMatches(lang(?predicate_label), '{language}'))17                       FILTER (!isLiteral(?{target}) || langMatches(lang(?{target}), '') || langMatches(lang(?{target}), '{language}'))18                       FILTER (!isLiteral(?{target}_label) || langMatches(lang(?{target}_label), '') || langMatches(lang(?{target}_label), '{language}'))19                      """20        limit_filter = f'LIMIT {int(query_limit)}' if query_limit is not None else ''21        query = f"""22                 SELECT DISTINCT ?predicate ?predicate_type ?predicate_label ?{target} ?{target}_type ?{target}_label FROM <{graph}> WHERE {{23                     {query_main}24                     OPTIONAL {{ ?predicate <{'>|<'.join(catalog_labels)}> ?predicate_label }}25                     OPTIONAL {{ ?predicate <{'>|<'.join(catalog_types)}> ?predicate_type }}26                     OPTIONAL {{ ?{target} <{'>|<'.join(catalog_labels)}> ?{target}_label }}27                     OPTIONAL {{ ?{target} <{'>|<'.join(catalog_types)}> ?{target}_type }}28                     {query_lang if language is not None else ''}29                 }}30                 {limit_filter}31                """32        db.graphs.client.setQuery(query)33        return db.graphs.client.query().convert()['results']['bindings']34    def maps() -> None:35        for item in items:36            for el in ['predicate', 'predicate_type', target, f'{target}_type']:37                if el in item and item[el]['type'] == 'uri':38                    if item[el]['value'] not in lookup:39                        lookup[item[el]['value']] = {'id': len(lookup)}40    def assign() -> None:41        for item in items:42            if item[target]['type'] == 'uri':43                if element == 'subject':44                    triples.append((lookup[uri]['id'], lookup[item['predicate']['value']]['id'], lookup[item['object']['value']]['id']))45                    if item['predicate']['value'] in catalog_types:46                        if 'types' not in lookup[uri]:47                            lookup[uri]['types'] = [lookup[item['object']['value']]['id']]48                        else:49                            lookup[uri]['types'].append(lookup[item['object']['value']]['id'])50                else:51                    triples.append((lookup[item['subject']['value']]['id'], lookup[item['predicate']['value']]['id'], lookup[uri]['id']))52            else:53                if 'xml:lang' in item[target]:54                    literal = {'value': item[target]['value'], 'language': item[target]['xml:lang']}55                else:56                    literal = {'value': item[target]['value']}57                if 'literals' not in lookup[uri]:58                    lookup[uri]['literals'] = {lookup[item['predicate']['value']]['id']: literal}59                else:60                    lookup[uri]['literals'][lookup[item['predicate']['value']]['id']] = literal61                if item['predicate']['value'] in catalog_labels:62                    lookup[uri]['label'] = item[target]['value']63            if 'predicate_label' in item:64                lookup[item['predicate']['value']]['label'] = item['predicate_label']['value']65            if f'{target}_label' in item:66                lookup[item[target]['value']]['label'] = item[f'{target}_label']['value']67            if 'predicate_type' in item:68                if 'types' not in lookup[item['predicate']['value']]:69                    lookup[item['predicate']['value']]['types'] = [lookup[item['predicate_type']['value']]['id']]70                elif lookup[item['predicate_type']['value']]['id'] not in lookup[item['predicate']['value']]['types']:71                    lookup[item['predicate']['value']]['types'].append(lookup[item['predicate_type']['value']]['id'])72            if f'{target}_type' in item:73                if 'types' not in lookup[item[target]['value']]:74                    lookup[item[target]['value']]['types'] = [lookup[item[f'{target}_type']['value']]['id']]75                elif lookup[item[f'{target}_type']['value']]['id'] not in lookup[item[target]['value']]['types']:76                    lookup[item[target]['value']]['types'].append(lookup[item[f'{target}_type']['value']]['id'])77    time_start = time.process_time()78    catalog = db.docs.catalog(graph)79    catalog_labels = [lb['uri'] for lb in catalog['predicates']['labels']]80    catalog_types = [ty['uri'] for ty in catalog['predicates']['types']]81    lookup = dict()82    triples = list()83    for i, uri in enumerate(uris):84        lookup[uri] = {'id': i}85    for uri in uris:86        if include_incomings:87            element, target = 'object', 'subject'88            items = fetch('incoming')89            maps()90            assign()91        if include_outgoings:92            element, target = 'subject', 'object'93            items = fetch('outgoing')94            maps()95            assign()96    time_end = time.process_time()97    return {'lookup': lookup, 'triples': triples, 'duration': time_end - time_start}98def candidates(graph: str, search: str, limit: Union[int, None], query_limit: [int, None], label_entities: List[str],99               type_entities: List[str], have_types_only: bool, perfect_match_only: bool) -> Dict[str, List[Dict[str, Union[str, List[str]]]]]:100    """Find candidates of entities given an entity or a partial/full label"""101    def fetch_from_label(perfect_match: bool):102        if perfect_match:103            terms = ['"%s"' % term for term in search.strip().split(' ')]104        else:105            # Each word in the search term needs at least four characters to be included in partial search106            # The asterisk (*) indicates partial search107            terms = ['"%s*"' % term.strip(',') if len(term) > 3 else '"%s"' % term108                     for term in search.strip().split(' ')]109        # Search terms are joined by 'AND' operators110        query = 'SELECT DISTINCT ?entity, ?label, ?type FROM <%s> WHERE { ' % graph111        partial_queries = []112        for label_entity in label_entities:113            labeled_query = '{ ?entity <%s> ?label . ' % label_entity114            labeled_query += '?label bif:contains \'%s\' ' % ' AND '.join(terms)115            if have_types_only:116                for type_entity in type_entities:117                    typed_query = labeled_query + '. ?entity <%s> ?type } ' % type_entity118                    partial_queries.append(typed_query)119            else:120                partial_queries.append('%s }' % labeled_query)121        122        query += ' UNION '.join(partial_queries)123        124        if not have_types_only:125            for type_entity in type_entities:126                query += 'OPTIONAL { ?entity <%s> ?type } ' % type_entity127        if perfect_match:128            query += 'FILTER (lcase(str(?label)) = "%s") ' % search.lower().strip()129        130        query += '} %s' % limit_filter131        db.graphs.client.setQuery(query)132        try:133            return db.graphs.client.query().convert()['results']['bindings']134        except timeout:135            return []136    def fetch_from_uri():137        query = 'SELECT DISTINCT ?label, ?type FROM <%s> WHERE { ' % graph138        partial_queries = []139        for label_entity in label_entities:140            labeled_query = '{ <%s> <%s> ?label ' % (search, label_entity)141            if have_types_only:142                for type_entity in type_entities:143                    typed_query = labeled_query + '. <%s> <%s> ?type } ' % (search, type_entity)144                    partial_queries.append(typed_query)145            else:146                partial_queries.append('%s }' % labeled_query)147        query += ' UNION '.join(partial_queries)148        if not have_types_only:149            for type_entity in type_entities:150                query += 'OPTIONAL { <%s> <%s> ?type } ' % (search, type_entity)151        query += '} %s' % limit_filter152        db.graphs.client.setQuery(query)153        try:154            return db.graphs.client.query().convert()['results']['bindings']155        except timeout:156            return []157    def filter_types(entries: list) -> list:158        filtered = []159        for entry in entries:160            types = [tag.lower() for tag in entry[1]['types'] if isinstance(tag, str)]161            if len(pos_type_tags):162                if all(any(pos.lower() in tag for tag in types) for pos in pos_type_tags):163                    if len(neg_type_tags):164                        if all(all(neg.lower() not in tag for tag in types) for neg in neg_type_tags):165                            filtered.append(entry)166                    else:167                        filtered.append(entry)168            elif len(neg_type_tags):169                if all(all(neg.lower() not in tag for tag in types) for neg in neg_type_tags):170                    filtered.append(entry)171            else:172                filtered.append(entry)173        return filtered174    limit_filter = "LIMIT %d" % query_limit if query_limit is not None else ''175    matches = dict()176    terms = search.split(' ')177    pos_type_tags = []178    neg_type_tags = []179    filtered_terms = []180    for term in terms:181        if term.startswith('+type:'):182            pos_type_tags += term.split(':')[1:]183        elif term.startswith('-type:'):184            neg_type_tags += term.split(':')[1:]185        else:186            filtered_terms.append(term)187    search = ' '.join(filtered_terms)188    if len(search) == 0:189        return {'exact_matches': [], 'first_matches': [], 'partial_matches': []}190    if search.startswith('http://') or search.startswith('https://'):191        response = fetch_from_uri()192    else:193        # Try searching for perfect matches first since perfect-match search is the fastest due to indexing194        response = fetch_from_label(True)195        # If not perfect matches found, try partial matches196        if len(response) == 0 and not perfect_match_only:197            response = fetch_from_label(False)198    # Convert the response into a clean dictionary-based output199    for item in response:200        uri = search if search.startswith('http://') or search.startswith('https://') else item['entity']['value']201        entity_label, entity_types = item['label']['value'], item['type']['value'] if 'type' in item else []202        if uri not in matches:203            matches[uri] = {'label': entity_label, 'types': [entity_types]}204        else:205            matches[uri]['types'].append(entity_types)206    results = {'exact_matches': [], 'first_matches': [], 'partial_matches': []}207    # Categorizes matches in to 'exact', 'first', and 'partial' lists208    for uri, data in matches.items():209        norm_search = search.lower()210        entity_label = data['label'].lower()211        if norm_search == entity_label:212            results['exact_matches'].append((len(entity_label), {'uri': uri, **data}))213        elif entity_label.startswith(norm_search):214            results['first_matches'].append((len(entity_label), {'uri': uri, **data}))215        else:216            results['partial_matches'].append((len(entity_label), {'uri': uri, **data}))217    # Return matches with shorter labels first218    results['exact_matches'].sort(key=lambda x: x[0])219    results['first_matches'].sort(key=lambda x: x[0])220    results['partial_matches'].sort(key=lambda x: x[0])221    results['exact_matches'] = filter_types(results['exact_matches'])222    results['first_matches'] = filter_types(results['first_matches'])223    results['partial_matches'] = filter_types(results['partial_matches'])224    if limit is not None:225        return {'exact_matches': [entity[1] for entity in results['exact_matches'][:limit]],226                'first_matches': [entity[1] for entity in results['first_matches'][:limit - len(results['first_matches'])]],227                'partial_matches': [entity[1] for entity in228                                    results['partial_matches'][:limit - len(results['first_matches']) - len(results['partial_matches'])]]}229    else:230        return {'exact_matches': [entity[1] for entity in results['exact_matches']],231                'first_matches': [entity[1] for entity in results['first_matches']],232                'partial_matches': [entity[1] for entity in results['partial_matches']]}233def entity(graph: str, subject: str, label_entities: List[str], type_entities: str):234    """Find objects or an attribute given subject and predicate"""235    query = """SELECT DISTINCT ?predicate ?predicate_label ?predicate_type ?object ?object_label ?object_type 236            FROM <%s> WHERE { <%s> ?predicate ?object . """ % (graph, subject)237    for label_entity in label_entities:238        query += 'OPTIONAL { ?predicate <%s> ?predicate_label . ?object <%s> ?object_label } . ' \239                 % (label_entity, label_entity)240    for type_entity in type_entities:241        query += 'OPTIONAL { ?predicate <%s> ?predicate_type . ?object <%s> ?object_type } . ' \242                 % (type_entity, type_entity)243    query += '} '244    db.graphs.client.setQuery(query)245    response = db.graphs.client.query().convert()['results']['bindings']246    results = {}247    for item in response:248        predicate_value = item['predicate']['value']249        object_value = item['object']['value']250        if predicate_value not in results:251            results[predicate_value] = {}252        predicate = results[predicate_value]253        if 'predicate_label' in item:254            label = {'value': item['predicate_label']['value']}255            if 'xml:lang' in item['predicate_label']:256                label['language'] = item['predicate_label']['xml:lang']257            if 'labels' not in predicate:258                predicate['labels'] = [label]259            elif label not in predicate['labels']:260                predicate['labels'].append(label)261        if 'predicate_type' in item:262            typ = item['predicate_type']['value']263            if 'types' not in predicate:264                predicate['types'] = [typ]265            elif typ not in predicate['types']:266                predicate['types'].append(typ)267        if item['object']['type'] == 'uri':268            if 'objects' not in predicate:269                predicate['objects'] = {object_value: {}}270            elif item['object']['value'] not in predicate['objects']:271                predicate['objects'][object_value] = {}272            obj = predicate['objects'][object_value]273            if 'object_label' in item:274                label = {'value': item['object_label']['value']}275                if 'xml:lang' in item['object_label']:276                    label['language'] = item['object_label']['xml:lang']277                if 'labels' not in obj:278                    obj['labels'] = [label]279                elif label not in obj['labels']:280                    obj['labels'].append(label)281            if 'object_type' in item:282                typ = item['object_type']['value']283                if 'types' not in obj:284                    obj['types'] = [typ]285                elif typ not in obj['types']:286                    obj['types'].append(typ)287        elif item['object']['type'] in ('literal', 'typed-literal'):288            attribute = {'value': item['object']['value']}289            if 'xml:lang' in item['object']:290                attribute['language'] = item['object']['xml:lang']291            if 'datatype' in item['object']:292                attribute['datatype'] = item['object']['datatype']293            if 'attributes' not in predicate:294                predicate['attributes'] = [attribute]295            elif attribute not in predicate['attributes']:296                predicate['attributes'].append(attribute)...test_filter.py
Source:test_filter.py  
...312        predicate.setLogLevelForNamespace("twext.web2", LogLevel.debug)313        predicate.setLogLevelForNamespace("twext.web2.dav", LogLevel.warn)314        def checkPredicate(namespace, level, expectedResult):315            event = dict(log_namespace=namespace, log_level=level)316            self.assertEqual(expectedResult, predicate(event))317        checkPredicate("", LogLevel.debug, PredicateResult.no)318        checkPredicate("", LogLevel.error, PredicateResult.maybe)319        checkPredicate("twext.web2", LogLevel.debug, PredicateResult.maybe)320        checkPredicate("twext.web2", LogLevel.error, PredicateResult.maybe)321        checkPredicate("twext.web2.dav", LogLevel.debug, PredicateResult.no)322        checkPredicate("twext.web2.dav", LogLevel.error, PredicateResult.maybe)323        checkPredicate(None, LogLevel.critical, PredicateResult.no)...exclusions.py
Source:exclusions.py  
...11import contextlib12import inspect13class skip_if(object):14    def __init__(self, predicate, reason=None):15        self.predicate = _as_predicate(predicate)16        self.reason = reason17    _fails_on = None18    def __add__(self, other):19        def decorate(fn):20            return other(self(fn))21        return decorate22    @property23    def enabled(self):24        return self.enabled_for_config(config._current)25    def enabled_for_config(self, config):26        return not self.predicate(config)27    @contextlib.contextmanager28    def fail_if(self, name='block'):29        try:30            yield31        except Exception as ex:32            if self.predicate(config._current):33                print(("%s failed as expected (%s): %s " % (34                    name, self.predicate, str(ex))))35            else:36                raise37        else:38            if self.predicate(config._current):39                raise AssertionError(40                    "Unexpected success for '%s' (%s)" %41                    (name, self.predicate))42    def __call__(self, fn):43        @decorator44        def decorate(fn, *args, **kw):45            if self.predicate(config._current):46                if self.reason:47                    msg = "'%s' : %s" % (48                            fn.__name__,49                            self.reason50                        )51                else:52                    msg = "'%s': %s" % (53                            fn.__name__, self.predicate54                        )55                raise SkipTest(msg)56            else:57                if self._fails_on:58                    with self._fails_on.fail_if(name=fn.__name__):59                        return fn(*args, **kw)60                else:61                    return fn(*args, **kw)62        return decorate(fn)63    def fails_on(self, other, reason=None):64        self._fails_on = skip_if(other, reason)65        return self66    def fails_on_everything_except(self, *dbs):67        self._fails_on = skip_if(fails_on_everything_except(*dbs))68        return self69class fails_if(skip_if):70    def __call__(self, fn):71        @decorator72        def decorate(fn, *args, **kw):73            with self.fail_if(name=fn.__name__):74                return fn(*args, **kw)75        return decorate(fn)76def only_if(predicate, reason=None):77    predicate = _as_predicate(predicate)78    return skip_if(NotPredicate(predicate), reason)79def succeeds_if(predicate, reason=None):80    predicate = _as_predicate(predicate)81    return fails_if(NotPredicate(predicate), reason)82class Predicate(object):83    @classmethod84    def as_predicate(cls, predicate):85        if isinstance(predicate, skip_if):86            return NotPredicate(predicate.predicate)87        elif isinstance(predicate, Predicate):88            return predicate89        elif isinstance(predicate, list):90            return OrPredicate([cls.as_predicate(pred) for pred in predicate])91        elif isinstance(predicate, tuple):92            return SpecPredicate(*predicate)93        elif isinstance(predicate, util.string_types):94            tokens = predicate.split(" ", 2)95            op = spec = None96            db = tokens.pop(0)97            if tokens:98                op = tokens.pop(0)99            if tokens:100                spec = tuple(int(d) for d in tokens.pop(0).split("."))101            return SpecPredicate(db, op, spec)102        elif util.callable(predicate):103            return LambdaPredicate(predicate)104        else:105            assert False, "unknown predicate type: %s" % predicate106class BooleanPredicate(Predicate):107    def __init__(self, value, description=None):108        self.value = value109        self.description = description or "boolean %s" % value110    def __call__(self, config):111        return self.value112    def _as_string(self, negate=False):113        if negate:114            return "not " + self.description115        else:116            return self.description117    def __str__(self):118        return self._as_string()119class SpecPredicate(Predicate):120    def __init__(self, db, op=None, spec=None, description=None):121        self.db = db122        self.op = op123        self.spec = spec124        self.description = description125    _ops = {126            '<': operator.lt,127             '>': operator.gt,128             '==': operator.eq,129             '!=': operator.ne,130             '<=': operator.le,131             '>=': operator.ge,132             'in': operator.contains,133             'between': lambda val, pair: val >= pair[0] and val <= pair[1],134             }135    def __call__(self, config):136        engine = config.db137        if "+" in self.db:138            dialect, driver = self.db.split('+')139        else:140            dialect, driver = self.db, None141        if dialect and engine.name != dialect:142            return False143        if driver is not None and engine.driver != driver:144            return False145        if self.op is not None:146            assert driver is None, "DBAPI version specs not supported yet"147            version = _server_version(engine)148            oper = hasattr(self.op, '__call__') and self.op \149                        or self._ops[self.op]150            return oper(version, self.spec)151        else:152            return True153    def _as_string(self, negate=False):154        if self.description is not None:155            return self.description156        elif self.op is None:157            if negate:158                return "not %s" % self.db159            else:160                return "%s" % self.db161        else:162            if negate:163                return "not %s %s %s" % (164                        self.db,165                        self.op,166                        self.spec167                    )168            else:169                return "%s %s %s" % (170                        self.db,171                        self.op,172                        self.spec173                    )174    def __str__(self):175        return self._as_string()176class LambdaPredicate(Predicate):177    def __init__(self, lambda_, description=None, args=None, kw=None):178        spec = inspect.getargspec(lambda_)179        if not spec[0]:180            self.lambda_ = lambda db: lambda_()181        else:182            self.lambda_ = lambda_183        self.args = args or ()184        self.kw = kw or {}185        if description:186            self.description = description187        elif lambda_.__doc__:188            self.description = lambda_.__doc__189        else:190            self.description = "custom function"191    def __call__(self, config):192        return self.lambda_(config)193    def _as_string(self, negate=False):194        if negate:195            return "not " + self.description196        else:197            return self.description198    def __str__(self):199        return self._as_string()200class NotPredicate(Predicate):201    def __init__(self, predicate):202        self.predicate = predicate203    def __call__(self, config):204        return not self.predicate(config)205    def __str__(self):206        return self.predicate._as_string(True)207class OrPredicate(Predicate):208    def __init__(self, predicates, description=None):209        self.predicates = predicates210        self.description = description211    def __call__(self, config):212        for pred in self.predicates:213            if pred(config):214                self._str = pred215                return True216        return False217    _str = None218    def _eval_str(self, negate=False):219        if self._str is None:220            if negate:221                conjunction = " and "222            else:223                conjunction = " or "224            return conjunction.join(p._as_string(negate=negate)225                            for p in self.predicates)226        else:227            return self._str._as_string(negate=negate)228    def _negation_str(self):229        if self.description is not None:230            return "Not " + (self.description % {"spec": self._str})231        else:232            return self._eval_str(negate=True)233    def _as_string(self, negate=False):234        if negate:235            return self._negation_str()236        else:237            if self.description is not None:238                return self.description % {"spec": self._str}239            else:240                return self._eval_str()241    def __str__(self):242        return self._as_string()243_as_predicate = Predicate.as_predicate244def _is_excluded(db, op, spec):245    return SpecPredicate(db, op, spec)(config._current)246def _server_version(engine):247    """Return a server_version_info tuple."""248    # force metadata to be retrieved249    conn = engine.connect()250    version = getattr(engine.dialect, 'server_version_info', ())251    conn.close()252    return version253def db_spec(*dbs):254    return OrPredicate(255            [Predicate.as_predicate(db) for db in dbs]256        )257def open():258    return skip_if(BooleanPredicate(False, "mark as execute"))259def closed():260    return skip_if(BooleanPredicate(True, "marked as skip"))261def fails():262    return fails_if(BooleanPredicate(True, "expected to fail"))263@decorator264def future(fn, *arg):265    return fails_if(LambdaPredicate(fn), "Future feature")266def fails_on(db, reason=None):267    return fails_if(SpecPredicate(db), reason)268def fails_on_everything_except(*dbs):269    return succeeds_if(270                OrPredicate([271                    SpecPredicate(db) for db in dbs272                    ])273            )274def skip(db, reason=None):275    return skip_if(SpecPredicate(db), reason)276def only_on(dbs, reason=None):277    return only_if(278            OrPredicate([SpecPredicate(db) for db in util.to_list(dbs)])279    )280def exclude(db, op, spec, reason=None):281    return skip_if(SpecPredicate(db, op, spec), reason)282def against(config, *queries):283    assert queries, "no queries sent!"284    return OrPredicate([285                Predicate.as_predicate(query)286                for query in queries...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
