Skip to content

Commit 0cbb228

Browse files
committed
Change record_type to back_as; #20
Decided on this one with @dowski. I wanted something shorter and punchier, he was reading record as a verb. https://botbot.me/freenode/aspen/msg/5326310/
1 parent 34a0fbc commit 0cbb228

File tree

2 files changed

+36
-37
lines changed

2 files changed

+36
-37
lines changed

postgres/__init__.py

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ def __str__(self):
249249

250250
class BadRecordType(Exception):
251251
def __str__(self):
252-
return "Bad record_type: {}. Available record_types are: tuple, " \
252+
return "Bad back_as: {}. Available back_as values are: tuple, " \
253253
"namedtuple, dict, or None (to use the default)." \
254254
.format(self.args[0])
255255

@@ -294,7 +294,7 @@ class Postgres(object):
294294
Use :py:class:`psycopg2.extensions.cursor` for the default
295295
:py:mod:`psycopg2` behavior, which is to return tuples. Whatever default
296296
you set here, you can override that default on a per-call basis by passing
297-
:py:attr:`record_type` or :py:attr:`cursor_factory` to
297+
:py:attr:`back_as` or :py:attr:`cursor_factory` to
298298
:py:meth:`~postgres.Postgres.one`, :py:meth:`~postgres.Postgres.all`, and
299299
:py:meth:`~postgres.Postgres.get_cursor`.
300300
@@ -366,15 +366,14 @@ def run(self, sql, parameters=None, *a, **kw):
366366
cursor.execute(sql, parameters)
367367

368368

369-
def one(self, sql, parameters=None, record_type=None, default=None, \
370-
*a, **kw):
369+
def one(self, sql, parameters=None, back_as=None, default=None, *a, **kw):
371370
"""Execute a query and return a single result or a default value.
372371
373372
:param string sql: the SQL statement to execute
374373
:param parameters: the bind parameters for the SQL statement
375374
:type parameters: dict or tuple
376-
:param record_type: the type of record to return
377-
:type record_type: type or string
375+
:param back_as: the type of record to return
376+
:type back_as: type or string
378377
:param default: the value to return if no results are found
379378
:param a: passed through to
380379
:py:meth:`~postgres.Postgres.get_cursor`
@@ -409,14 +408,14 @@ def one(self, sql, parameters=None, record_type=None, default=None, \
409408
it's easy to just check the return value in the caller and do your
410409
extra logic there.
411410
412-
You can use :py:attr:`record_type` to override the type associated with
413-
the default :py:attr:`cursor_factory` for your
411+
You can use :py:attr:`back_as` to override the type associated with the
412+
default :py:attr:`cursor_factory` for your
414413
:py:class:`~postgres.Postgres` instance:
415414
416415
>>> db.default_cursor_factory
417416
<class 'psycopg2.extras.NamedTupleCursor'>
418417
>>> db.one( "SELECT * FROM foo WHERE bar='buz'"
419-
... , record_type=dict
418+
... , back_as=dict
420419
... )
421420
{'bar': 'buz', 'baz': 42}
422421
@@ -425,11 +424,11 @@ def one(self, sql, parameters=None, record_type=None, default=None, \
425424
importing it in order to get dictionaries back. If you do need more
426425
control (maybe you have a custom cursor class), you can pass
427426
:py:attr:`cursor_factory` explicitly, and that will override any
428-
:py:attr:`record_type`:
427+
:py:attr:`back_as`:
429428
430429
>>> from psycopg2.extensions import cursor
431430
>>> db.one( "SELECT * FROM foo WHERE bar='buz'"
432-
... , record_type=dict
431+
... , back_as=dict
433432
... , cursor_factory=cursor
434433
... )
435434
('buz', 42)
@@ -450,14 +449,14 @@ def one(self, sql, parameters=None, record_type=None, default=None, \
450449
so it should work for both mappings and sequences.
451450
452451
>>> db.one( "SELECT sum(baz) FROM foo WHERE bar='nope'"
453-
... , record_type=dict
452+
... , back_as=dict
454453
... , default=0
455454
... )
456455
0
457456
458457
"""
459458

460-
out = self._some(sql, parameters, 0, 1, record_type, *a, **kw)
459+
out = self._some(sql, parameters, 0, 1, back_as, *a, **kw)
461460

462461
# dereference
463462
if out is not None and len(out) == 1:
@@ -471,14 +470,14 @@ def one(self, sql, parameters=None, record_type=None, default=None, \
471470
return out
472471

473472

474-
def all(self, sql, parameters=None, record_type=None, *a, **kw):
473+
def all(self, sql, parameters=None, back_as=None, *a, **kw):
475474
"""Execute a query and return all results.
476475
477476
:param string sql: the SQL statement to execute
478477
:param parameters: the bind parameters for the SQL statement
479478
:type parameters: dict or tuple
480-
:param record_type: the type of record to return
481-
:type record_type: type or string
479+
:param back_as: the type of record to return
480+
:type back_as: type or string
482481
:param a: passed through to
483482
:py:meth:`~postgres.Postgres.get_cursor`
484483
:param kw: passed through to
@@ -489,25 +488,25 @@ def all(self, sql, parameters=None, record_type=None, *a, **kw):
489488
>>> db.all("SELECT * FROM foo ORDER BY bar")
490489
[Record(bar='bit', baz=537), Record(bar='buz', baz=42)]
491490
492-
You can use :py:attr:`record_type` to override the type associated with
493-
the default :py:attr:`cursor_factory` for your
491+
You can use :py:attr:`back_as` to override the type associated with the
492+
default :py:attr:`cursor_factory` for your
494493
:py:class:`~postgres.Postgres` instance:
495494
496495
>>> db.default_cursor_factory
497496
<class 'psycopg2.extras.NamedTupleCursor'>
498-
>>> db.all("SELECT * FROM foo ORDER BY bar", record_type=dict)
497+
>>> db.all("SELECT * FROM foo ORDER BY bar", back_as=dict)
499498
[{'bar': 'bit', 'baz': 537}, {'bar': 'buz', 'baz': 42}]
500499
501500
That's a convenience so you don't have to go to the trouble of
502501
remembering where :py:class:`~psycopg2.extras.RealDictCursor` lives and
503502
importing it in order to get dictionaries back. If you do need more
504503
control (maybe you have a custom cursor class), you can pass
505504
:py:attr:`cursor_factory` explicitly, and that will override any
506-
:py:attr:`record_type`:
505+
:py:attr:`back_as`:
507506
508507
>>> from psycopg2.extensions import cursor
509508
>>> db.all( "SELECT * FROM foo ORDER BY bar"
510-
... , record_type=dict
509+
... , back_as=dict
511510
... , cursor_factory=cursor
512511
... )
513512
[('bit', 537), ('buz', 42)]
@@ -522,11 +521,11 @@ def all(self, sql, parameters=None, record_type=None, *a, **kw):
522521
:py:meth:`__len__` and a :py:meth:`values` method) as well those that
523522
are sequences:
524523
525-
>>> db.all("SELECT baz FROM foo ORDER BY bar", record_type=dict)
524+
>>> db.all("SELECT baz FROM foo ORDER BY bar", back_as=dict)
526525
[537, 42]
527526
528527
"""
529-
with self.get_cursor(record_type=record_type, *a, **kw) as cursor:
528+
with self.get_cursor(back_as=back_as, *a, **kw) as cursor:
530529
cursor.execute(sql, parameters)
531530
recs = cursor.fetchall()
532531
if recs and len(recs[0]) == 1: # dereference
@@ -537,14 +536,14 @@ def all(self, sql, parameters=None, record_type=None, *a, **kw):
537536
return recs
538537

539538

540-
def _some(self, sql, parameters, lo, hi, record_type, *a, **kw):
539+
def _some(self, sql, parameters, lo, hi, back_as, *a, **kw):
541540

542541
# This is undocumented because I think it's a rare case where this is
543542
# wanted directly. It was added to make one and one_or_zero DRY when we
544543
# had those two methods. Help yourself to _some now that you've found
545544
# it. :^)
546545

547-
with self.get_cursor(record_type=record_type, *a, **kw) as cursor:
546+
with self.get_cursor(back_as=back_as, *a, **kw) as cursor:
548547
cursor.execute(sql, parameters)
549548

550549
if cursor.rowcount < lo:
@@ -678,7 +677,7 @@ class CursorContextManager(object):
678677
The return value of :py:func:`CursorContextManager.__enter__` is a
679678
:py:mod:`psycopg2` cursor. Any positional and keyword arguments to our
680679
constructor are passed through to the cursor constructor. If you pass
681-
:py:attr:`record_type` as a keyword argument then we'll infer a
680+
:py:attr:`back_as` as a keyword argument then we'll infer a
682681
:py:attr:`cursor_factory` from that, though any explicit
683682
:py:attr:`cursor_factory` keyword argument will take precedence.
684683
@@ -722,13 +721,13 @@ def __exit__(self, *exc_info):
722721

723722

724723
def compute_cursor_factory(self, **kw):
725-
"""Pull :py:attr:`record_type` out of :py:attr:`kw` and maybe add
724+
"""Pull :py:attr:`back_as` out of :py:attr:`kw` and maybe add
726725
:py:attr:`cursor_factory`.
727726
728-
Valid values for :py:attr:`record_type` are :py:class:`tuple`,
727+
Valid values for :py:attr:`back_as` are :py:class:`tuple`,
729728
:py:class:`namedtuple`, :py:class:`dict` (or the strings ``tuple``,
730729
``namedtuple``, and ``dict``), and :py:class:`None`. If the value of
731-
:py:attr:`record_type` is :py:class:`None`, then we won't insert any
730+
:py:attr:`back_as` is :py:class:`None`, then we won't insert any
732731
:py:attr:`cursor_factory` keyword argument. Otherwise we'll specify a
733732
:py:attr:`cursor_factory` that will result in records of the specific
734733
type: :py:class:`psycopg2.extensions.cursor` for :py:class:`tuple`,
@@ -738,17 +737,17 @@ def compute_cursor_factory(self, **kw):
738737
739738
"""
740739

741-
# Pull record_type out of kw.
740+
# Pull back_as out of kw.
742741
# ===========================
743742
# If we leave it in psycopg2 will complain. Our internal calls to
744743
# get_cursor always have it but external use might not.
745744

746-
record_type = kw.pop('record_type', None)
745+
back_as = kw.pop('back_as', None)
747746

748747

749748
if 'cursor_factory' not in kw:
750749

751-
# Compute cursor_factory from record_type.
750+
# Compute cursor_factory from back_as.
752751
# ====================================
753752

754753
cursor_factory_registry = { tuple: RegularCursor
@@ -760,10 +759,10 @@ def compute_cursor_factory(self, **kw):
760759
, None: None
761760
}
762761

763-
if record_type not in cursor_factory_registry:
764-
raise BadRecordType(record_type)
762+
if back_as not in cursor_factory_registry:
763+
raise BadRecordType(back_as)
765764

766-
cursor_factory = cursor_factory_registry[record_type]
765+
cursor_factory = cursor_factory_registry[back_as]
767766
if cursor_factory is not None:
768767
kw['cursor_factory'] = cursor_factory
769768

tests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def test_TooMany_message_is_helpful_for_two_options(self):
9999
, parameters=None
100100
, lo=1
101101
, hi=1
102-
, record_type=None
102+
, back_as=None
103103
)
104104
except TooMany as exc:
105105
actual = str(exc)
@@ -113,7 +113,7 @@ def test_TooMany_message_is_helpful_for_a_range(self):
113113
, parameters=None
114114
, lo=1
115115
, hi=3
116-
, record_type=None
116+
, back_as=None
117117
)
118118
except TooMany as exc:
119119
actual = str(exc)

0 commit comments

Comments
 (0)