本文整理汇总了Python中searcher.Searcher.fromClass方法的典型用法代码示例。如果您正苦于以下问题:Python Searcher.fromClass方法的具体用法?Python Searcher.fromClass怎么用?Python Searcher.fromClass使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类searcher.Searcher
的用法示例。
在下文中一共展示了Searcher.fromClass方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __str__
# 需要导入模块: from searcher import Searcher [as 别名]
# 或者: from searcher.Searcher import fromClass [as 别名]
def __str__(self):
return self.color + " " + self.brand + " built in " + str(self.year)
if __name__ == '__main__':
c1 = Car("Red", "Ford", 1974)
c2 = Car("Yellow", "Dodge", 2000)
c3 = Car("Black", "Toyota", 2005)
c4 = Car("Pink", "Chevy", 1985)
c5 = Car("Red", "Ferrari", 1990)
c6 = Car("Black", "Ford", 1990)
searcher = Searcher()
fordCars = searcher.fromClass("Car").where("brand = Ford")
newCars = searcher.fromClass("Car").where("year >= 2000")
blackFords = searcher.fromClass("Car").where("brand = Ford AND color = Black ")
blackOrRedCars = searcher.fromClass("Car").where("color = Red OR color = Black")
redOrFerrariCars = searcher.fromClass("Car").where("color = Red OR brand = Ferrari")
print "====Ford Cars==="
for car in fordCars:
print car
print "====Cars made in 2000 or later==="
for car in newCars:
print car
print "====Black Fords==="
for car in blackFords:
示例2: TestSearcher
# 需要导入模块: from searcher import Searcher [as 别名]
# 或者: from searcher.Searcher import fromClass [as 别名]
class TestSearcher(unittest.TestCase):
def setUp(self):
self.searcher = Searcher()
self.mikeObj = Person("Mike", 15)
self.mikeObj2 = Person("Mike", 99)
self.mikeObj3 = Person("Mike", 15, "Michael")
self.jouhan = Person("Jouhan", 22)
self.jouhan1 = Person("Jouhan", 74)
self.alex = Person("Alex", 15)
self.tim = Person("Tim", 100)
self.cat = Animal("Cat", "Red", True)
self.fish1 = Animal("Fish", "Blue", False)
self.fish2 = Animal("Fish", "Red", False)
self.dog = Animal("Dog", "Black", True)
self.human = Animal("Human", "White", True)
def tearDown(self):
#to destroy the database that was created for testing AKA ISOLATION!
self.searcher.index.clear()
def test_searcher_expecting_multiple_values(self):
self.assertEquals(len(self.searcher.fromClass("Person").where("age = 15")), 3)
def test_searcher_excepting_no_values(self):
self.assertEquals(len(self.searcher.fromClass('Person').where("age = 20")), 0)
def test_searcher_excepting_correct_result(self):
person = self.searcher.fromClass("Person").where("age = 99")[0]
self.assertTrue(self.mikeObj2.__hash__() == person.__hash__())
def test_searcher_equality_search(self):
person = self.searcher.fromClass("Person").where("name = Alex")[0]
self.assertTrue(person.__hash__(), self.alex.__hash__())
def test_searcher_greater_or_equal_to_search_correct_results(self):
people = self.searcher.fromClass("Person").where("age >= 74")
hashes = [person.__hash__() for person in people]
self.assertTrue(self.mikeObj2.__hash__() in hashes and self.jouhan1.__hash__() in hashes and self.tim.__hash__() in hashes)
self.assertEquals(len(people), 3)
def test_searcher_greater_to_search_correct_results(self):
people = self.searcher.fromClass("Person").where("age > 74")
hashes = [person.__hash__() for person in people]
self.assertTrue(self.mikeObj2.__hash__() in hashes and self.tim.__hash__() in hashes)
self.assertEquals(len(people), 2)
def test_searcher_between_search_correct_results(self):
people = self.searcher.fromClass("Person").where("age BETWEEN 15 AND 74")
hashes = [person.__hash__() for person in people]
self.assertTrue(self.mikeObj.__hash__() in hashes and self.mikeObj3.__hash__() in hashes and self.jouhan.__hash__() in hashes and self.alex.__hash__() in hashes and self.jouhan1.__hash__() in hashes)
self.assertEquals(len(people), 5)
def test_searcher_with_select_two_dicts(self):
people = self.searcher.fromClass("Person").select("name, age").where("name = Mike")
self.assertTrue({"name" : "Mike", "age" : 15} in people and {"name" : "Mike", "age" : 99} in people)
def test_searcher_with_select_and_queries(self):
people = self.searcher.fromClass("Person").select("name, age").where("name = Mike AND age = 99")
self.assertTrue({"name" : "Mike", "age" : 99} in people)
def test_searcher_with_select_two_dicts_no_space(self):
people = self.searcher.fromClass("Person").select("name,age").where("name = Mike")
self.assertTrue({"name" : "Mike", "age" : 15} in people and {"name" : "Mike", "age" : 99} in people)
def test_searcher_with_select_and_queries_no_space(self):
people = self.searcher.fromClass("Person").select("name,age").where("name = Mike AND age = 99")
self.assertTrue({"name" : "Mike", "age" : 99} in people)
self.assertEquals(len(people), 1)
def test_searcher_with_select_or_queries(self):
people = self.searcher.fromClass("Person").select("name, age").where("name = Alex OR name = Tim")
self.assertTrue({"name" : "Alex", "age" : 15} in people and {"name" : "Tim", "age" : 100} in people)
self.assertEquals(len(people), 2)
def test_searcher_with_select_between_queries(self):
people = self.searcher.fromClass("Person").select("name").where("age BETWEEN 70 AND 100")
self.assertTrue({"name" : "Jouhan"} in people and {"name" : "Mike"} in people and {"name" : "Tim"} in people)
self.assertEquals(len(people), 3)
def test_searcher_with_true_value_query(self):
animals = self.searcher.fromClass("Animal").where("mammal == True")
hashes = [animal.__hash__() for animal in animals]
self.assertTrue(self.human.__hash__() in hashes and self.cat.__hash__() in hashes and self.dog.__hash__() in hashes)
self.assertEquals(len(animals), 3)
def test_searcher_with_false_value_query(self):
animals = self.searcher.fromClass("Animal").where("mammal == False")
hashes = [animal.__hash__() for animal in animals]
self.assertTrue(self.fish1.__hash__() in hashes and self.fish2.__hash__() in hashes)
self.assertEquals(len(animals), 2)
def test_searcher_change_attributes(self):
people = self.searcher.fromClass("Person").where("age = 15")
self.assertEquals(len(people), 3)
self.alex.age = 16
self.assertTrue(len(self.searcher.fromClass("Person").where("age = 15")) == 2)
def test_searcher_or_query(self):
people = self.searcher.fromClass("Person").where("name = Jouhan OR name = Mike")
#.........这里部分代码省略.........