我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用psycopg2.extras.NumericRange()。
def test_cast_numbers(self): from psycopg2.extras import NumericRange cur = self.conn.cursor() for type in ('int4range', 'int8range'): cur.execute("select '(10,20)'::%s" % type) r = cur.fetchone()[0] self.assertTrue(isinstance(r, NumericRange)) self.assertTrue(not r.isempty) self.assertEqual(r.lower, 11) self.assertEqual(r.upper, 20) self.assertTrue(not r.lower_inf) self.assertTrue(not r.upper_inf) self.assertTrue(r.lower_inc) self.assertTrue(not r.upper_inc) cur.execute("select '(10.2,20.6)'::numrange") r = cur.fetchone()[0] self.assertTrue(isinstance(r, NumericRange)) self.assertTrue(not r.isempty) self.assertEqual(r.lower, Decimal('10.2')) self.assertEqual(r.upper, Decimal('20.6')) self.assertTrue(not r.lower_inf) self.assertTrue(not r.upper_inf) self.assertTrue(not r.lower_inc) self.assertTrue(not r.upper_inc)
def test_cast_numbers(self): from psycopg2.extras import NumericRange cur = self.conn.cursor() for type in ('int4range', 'int8range'): cur.execute("select '(10,20)'::%s" % type) r = cur.fetchone()[0] self.assert_(isinstance(r, NumericRange)) self.assert_(not r.isempty) self.assertEqual(r.lower, 11) self.assertEqual(r.upper, 20) self.assert_(not r.lower_inf) self.assert_(not r.upper_inf) self.assert_(r.lower_inc) self.assert_(not r.upper_inc) cur.execute("select '(10.2,20.6)'::numrange") r = cur.fetchone()[0] self.assert_(isinstance(r, NumericRange)) self.assert_(not r.isempty) self.assertEqual(r.lower, Decimal('10.2')) self.assertEqual(r.upper, Decimal('20.6')) self.assert_(not r.lower_inf) self.assert_(not r.upper_inf) self.assert_(not r.lower_inc) self.assert_(not r.upper_inc)
def test_eq_subclass(self): from psycopg2.extras import Range, NumericRange class IntRange(NumericRange): pass class PositiveIntRange(IntRange): pass self.assertEqual(Range(10, 20), IntRange(10, 20)) self.assertEqual(PositiveIntRange(10, 20), IntRange(10, 20)) # as the postgres docs describe for the server-side stuff, # ordering is rather arbitrary, but will remain stable # and consistent.
def test_adapt_number_range(self): from psycopg2.extras import NumericRange cur = self.conn.cursor() r = NumericRange(empty=True) cur.execute("select %s::int4range", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, NumericRange)) self.assertTrue(r1.isempty) r = NumericRange(10, 20) cur.execute("select %s::int8range", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, NumericRange)) self.assertEqual(r1.lower, 10) self.assertEqual(r1.upper, 20) self.assertTrue(r1.lower_inc) self.assertTrue(not r1.upper_inc) r = NumericRange(Decimal('10.2'), Decimal('20.5'), '(]') cur.execute("select %s::numrange", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, NumericRange)) self.assertEqual(r1.lower, Decimal('10.2')) self.assertEqual(r1.upper, Decimal('20.5')) self.assertTrue(not r1.lower_inc) self.assertTrue(r1.upper_inc)
def test_adapt_numeric_range(self): from psycopg2.extras import NumericRange cur = self.conn.cursor() r = NumericRange(empty=True) cur.execute("select %s::int4range", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, NumericRange), r1) self.assertTrue(r1.isempty) r = NumericRange(10, 20) cur.execute("select %s::int8range", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, NumericRange)) self.assertEqual(r1.lower, 10) self.assertEqual(r1.upper, 20) self.assertTrue(r1.lower_inc) self.assertTrue(not r1.upper_inc) r = NumericRange(Decimal('10.2'), Decimal('20.5'), '(]') cur.execute("select %s::numrange", (r,)) r1 = cur.fetchone()[0] self.assertTrue(isinstance(r1, NumericRange)) self.assertEqual(r1.lower, Decimal('10.2')) self.assertEqual(r1.upper, Decimal('20.5')) self.assertTrue(not r1.lower_inc) self.assertTrue(r1.upper_inc)
def test_adapt_number_range(self): from psycopg2.extras import NumericRange cur = self.conn.cursor() r = NumericRange(empty=True) cur.execute("select %s::int4range", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, NumericRange)) self.assert_(r1.isempty) r = NumericRange(10, 20) cur.execute("select %s::int8range", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, NumericRange)) self.assertEqual(r1.lower, 10) self.assertEqual(r1.upper, 20) self.assert_(r1.lower_inc) self.assert_(not r1.upper_inc) r = NumericRange(Decimal('10.2'), Decimal('20.5'), '(]') cur.execute("select %s::numrange", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, NumericRange)) self.assertEqual(r1.lower, Decimal('10.2')) self.assertEqual(r1.upper, Decimal('20.5')) self.assert_(not r1.lower_inc) self.assert_(r1.upper_inc)
def test_adapt_numeric_range(self): from psycopg2.extras import NumericRange cur = self.conn.cursor() r = NumericRange(empty=True) cur.execute("select %s::int4range", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, NumericRange), r1) self.assert_(r1.isempty) r = NumericRange(10, 20) cur.execute("select %s::int8range", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, NumericRange)) self.assertEqual(r1.lower, 10) self.assertEqual(r1.upper, 20) self.assert_(r1.lower_inc) self.assert_(not r1.upper_inc) r = NumericRange(Decimal('10.2'), Decimal('20.5'), '(]') cur.execute("select %s::numrange", (r,)) r1 = cur.fetchone()[0] self.assert_(isinstance(r1, NumericRange)) self.assertEqual(r1.lower, Decimal('10.2')) self.assertEqual(r1.upper, Decimal('20.5')) self.assert_(not r1.lower_inc) self.assert_(r1.upper_inc)
def generate_numeric_range(items, lower_bound, upper_bound): """Generate postgresql numeric range and label for insertion. Parameters ---------- items: iterable labels for ranges. lower_bound: numeric lower bound upper_bound: numeric upper bound """ quantile_grid = create_quantiles(items, lower_bound, upper_bound) labels, bounds = (zip(*quantile_grid)) ranges = ((label, NumericRange(*bound)) for label, bound in zip(labels, bounds)) return ranges