def test_where_not(self): """test 01: except/select test""" pq = PyLINQ(data) self.assertEqual( pq.where_not(lambda it: it["class"] == "classA") .select(lambda it: {"name": it["name"], "size": it["size"]}).items(), [{"name": "item3", "size": 6}])
def test2(self): """test 02: order_by test""" pq = PyLINQ(data) self.assertEqual( pq.where(lambda it: it["size"] < 10) .order_by(lambda it: it["size"]) .select(lambda it: (it["name"], it["size"])).items(), [("item3", 6), ("item1", 8)])
def test11(self): """test 11: default_if_empty test""" l = [1,2] pq = PyLINQ(iter(l)) self.assertEqual(pq.default_if_empty().items(), [1, 2]) pq = PyLINQ([]) self.assertEqual(pq.default_if_empty(), []) self.assertEqual(pq.default_if_empty([3]), [3])
def test1(self): """test 01: where/select test""" pq = PyLINQ(data) self.assertEqual( pq.where(lambda it: it["class"] == "classA") .select(lambda it: {"name": it["name"], "size": it["size"]}).items(), [{"name": "item1", "size": 8}, {"name": "item2", "size": 10}])
def test12(self): """test 12: element_at_or_default test""" l = [100, 200, 300] pq = PyLINQ(iter(l)) self.assertEqual(pq.element_at_or_default(0), 100) self.assertEqual(pq.element_at_or_default(1), 200) self.assertEqual(pq.element_at_or_default(2), 300) self.assertEqual(pq.element_at_or_default(50, default=0), 0)
def test9(self): """test 09: element_at test""" l = [100, 200, 300] pq = PyLINQ(iter(l)) self.assertEqual(pq.element_at(0), 100) self.assertEqual(pq.element_at(1), 200) self.assertEqual(pq.element_at(2), 300) self.assertRaises(IndexError, pq.element_at, 50) self.assertRaises(IndexError, pq.element_at, -2)
def test3(self): """test 03: count test with generator""" pq = PyLINQ(iter(data)) self.assertEqual( pq.where(lambda it: it["size"] < 10) .count(), 2) self.assertEqual( pq.where(lambda it: it["class"] == "classB") .count(), 1)
def test8(self): """test 08: first & last test""" gen = (x for x in "hello") pq = PyLINQ(iter(gen)) self.assertEqual(pq.first(), "h") self.assertEqual(pq.last(), "o") l = range(10) pq = PyLINQ(l) self.assertEqual(pq.last(lambda x: x < 4), 3) self.assertEqual(pq.first(lambda x: x > 7), 8)
stop_tuples = [get_stop_data(stop_url) for stop_url in stop_urls] return (label, stop_tuples) def get_stop_data(stop_url): stop_doc = get_doc(stop_url) subject_node = stop_doc.xpath( '//swivt:Subject[rdfs:isDefinedBy[@rdf:resource="' + stop_url + '"]]', namespaces=NAMESPACES)[0] display_name = subject_node.xpath('property:Display_name', namespaces=NAMESPACES)[0].text located_at = subject_node.xpath('property:Located_at', namespaces=NAMESPACES) if located_at: located_at = located_at[0].text else: located_at = "No Location Data" return (display_name, located_at) trains_doc = get_doc(TRAIN_CAT_URL) trains = PyLINQ( trains_doc.xpath( '//swivt:Subject[rdf:type[@rdf:resource="http://wiki.busroutes.in/wiki/Special:URIResolver/Category-3ATrain_route"]]/rdfs:isDefinedBy/@rdf:resource', namespaces=NAMESPACES)).distinct(lambda x: x).items() json.dump([get_route_data(train) for train in trains], sys.stdout, indent=4, sort_keys=True)
def test10(self): """test 10: concat test""" l1 = [5, 8] l2 = [10, 12] pq = PyLINQ(iter(l1)) self.assertEqual(pq.concat(l2).items(), l1 + l2)
def test7(self): """test 07: reverse test""" l = [1,3,5,7] pq = PyLINQ(iter(l)) self.assertEqual(pq.reverse().items(), list(reversed(l)))
def test6(self): """test 06: all test""" l = [1,3,5,7] pq = PyLINQ(iter(l)) self.assertEqual(pq.all(lambda x: x % 2 != 0), True) self.assertEqual(pq.all(lambda x: x < 6), False)
def test5(self): """test 05: any test""" l = [1,3,5,7,6] pq = PyLINQ(iter(l)) self.assertEqual(pq.any(lambda x: x % 2 == 0), True) self.assertEqual(pq.any(lambda x: x == 0), False)
def test4(self): """test 04: distinct test""" pq = PyLINQ(data) self.assertEqual( pq.distinct(lambda x: x["class"]).items(), ["classA", "classB"])
def test4_a(self): """test 04: distinct test with default""" pq = PyLINQ([d['class'] for d in data]) self.assertEqual( pq.distinct().items(), ["classA", "classB"])
def test2_a(self): """test 02: order_by test with default clause""" pq = PyLINQ([d['class'] for d in data]) self.assertEqual( pq.order_by().items(), ['classA', 'classA', 'classB'])