def test_self(): """Tests combinations of multiple conditions against the same keyword. """ k0 = ((K.Egap > 6) | (K.Egap < 21)) & (K.PV_cell < 13) # multiple keywords by default do not combine assert sorted(_expr_to_strings(k0).split(",")) == [ "(Egap(!*6):Egap(!21*))", "PV_cell(!13*)", ] # reset() # k1 = ((K.Egap > 6) | (K.Egap < 21)) & ((K.PV_cell < 13) | (K.PV_cell > 2)) # assert str(k1) == "Egap(!*6:!21*),PV_cell(!13*:!*2)" # assert str(K.Egap) == "Egap(!*6:!21*)" # assert str(K.PV_cell) == "PV_cell(!13*:!*2)" # reset() k2 = ((K.Egap > 0) & (K.Egap < 2)) | ((K.Egap > 5) | (K.Egap < 7)) # Bracket for the other OR is omitted assert _expr_to_strings(k2) == "Egap(!*5:!7*:(!*0,!2*))" # reset() k3 = ((K.Egap > 0) & (K.Egap < 2)) | (K.Egap == 5) assert _expr_to_strings(k3) == "Egap(5:(!*0,!2*))" # assert str(k2) == # reset() # k4 = ((K.Egap >= 6) | (K.Egap <= 21)) & (K.PV_cell <= 13) # assert str(k4) == "Egap(6*:*21),PV_cell(*13)" # reset() # k5 = ((K.Egap >= 6) | (K.Egap <= 21)) & ((K.PV_cell <= 13) | (K.PV_cell >= 2)) # assert str(k5) == "Egap(6*:*21),PV_cell(*13:2*)" # assert str(K.Egap) == "Egap(6*:*21)" # assert str(K.PV_cell) == "PV_cell(*13:2*)" # reset() k6 = ((K.Egap >= 0) & (K.Egap <= 2)) | ((K.Egap >= 5) & (K.Egap <= 7)) assert _expr_to_strings(k6) == "Egap((0*,*2):(5*,*7))" # reset() k7 = ((K.Egap >= 0) & (K.Egap <= 2)) | (K.Egap != 5) assert _expr_to_strings(k7) == "Egap(!5:(0*,*2))"
def test_invert(): from sympy import simplify_logic k0 = (K.Egap > 6) & (K.PV_cell < 13) kn0 = ~k0 # The not simplifies version assert _expr_to_strings(kn0) == "!(Egap(!*6),PV_cell(!13*))" kn0 = simplify_logic(kn0) strings = _expr_to_strings(kn0) assert ":" in strings for itm in strings.split(":"): assert "!" not in itm # reset() k1 = (K.Egap >= 6) & (K.PV_cell <= 13) kn1 = simplify_logic(~k1) strings = _expr_to_strings(kn1) assert ":" in strings for itm in strings.split(":"): assert "!" in itm
def test_type(): # in some cased 1 is treated as sympy.One assert _expr_to_strings(K.Egap > 1) # float version assert _expr_to_strings(K.Egap > 1.0) # multiple variables assert _expr_to_strings((K.Egap > 1.0) & (K.Egap < 2.0)) # Following expression will raise TypeError with pytest.raises(TypeError): _expr_to_strings(K.Egap) with pytest.raises(TypeError): _expr_to_strings("Egap(1*)")
def filter(self, keyword): """Adds a search term to the current filter list. Calling :meth:`filter` multiple times will join the final filters together using logical *and*. Args: keyword (aflow.keywords.Keyword): that encapsulates the AFLUX request language logic. New version: allowing keyword as a single string containing filters """ if self._final_check(): self._N = None if isinstance(keyword, str): # normal string if _check_input(keyword): self.filters.append(keyword) elif hasattr(keyword, "func"): # Is a sympy symbol expr = _expr_to_strings(keyword) self.filters.append(expr) else: msg.err(("Query.filter() takes either " "boolean expression or string," f" not {type(keyword)}")) return self
def test_operators(): """Test operators. All tests in this function are 1 variable""" # and k0 = (K.nspecies >= 2) & (K.nspecies <= 4) assert _expr_to_strings(k0) == "nspecies(2*,*4)" k1 = (K.Egap >= 6) & (K.PV_cell <= 13) assert _expr_to_strings(k1) == "Egap(6*),PV_cell(*13)" k2 = (K.Egap == 6) & (K.PV_cell == 13) assert _expr_to_strings(k2) == "Egap(6),PV_cell(13)" k3 = (K.Egap == 6) & (K.PV_cell != 13) assert _expr_to_strings(k3) == "Egap(6),PV_cell(!13)" k4 = (K.data_source == "aflowlib") | (K.species % "Si") assert _expr_to_strings(k4) == "data_source('aflowlib'):species(*'Si'*)" k5 = (K.data_source > "aflow") & (K.species < "Ag") assert _expr_to_strings(k5) == "data_source(!*'aflow'),species(!'Ag'*)"