Skip to content

Commit

Permalink
Python source cleanup using flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
dvarrazzo committed Oct 10, 2016
1 parent 4458c9b commit 91d2158
Show file tree
Hide file tree
Showing 35 changed files with 644 additions and 432 deletions.
21 changes: 11 additions & 10 deletions lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,20 @@

# Import the DBAPI-2.0 stuff into top-level module.

from psycopg2._psycopg import BINARY, NUMBER, STRING, DATETIME, ROWID
from psycopg2._psycopg import ( # noqa
BINARY, NUMBER, STRING, DATETIME, ROWID,

from psycopg2._psycopg import Binary, Date, Time, Timestamp
from psycopg2._psycopg import DateFromTicks, TimeFromTicks, TimestampFromTicks
Binary, Date, Time, Timestamp,
DateFromTicks, TimeFromTicks, TimestampFromTicks,

from psycopg2._psycopg import Error, Warning, DataError, DatabaseError, ProgrammingError
from psycopg2._psycopg import IntegrityError, InterfaceError, InternalError
from psycopg2._psycopg import NotSupportedError, OperationalError
Error, Warning, DataError, DatabaseError, ProgrammingError, IntegrityError,
InterfaceError, InternalError, NotSupportedError, OperationalError,

from psycopg2._psycopg import _connect, apilevel, threadsafety, paramstyle
from psycopg2._psycopg import __version__, __libpq_version__
_connect, apilevel, threadsafety, paramstyle,
__version__, __libpq_version__,
)

from psycopg2 import tz
from psycopg2 import tz # noqa


# Register default adapters.
Expand All @@ -82,7 +83,7 @@


def connect(dsn=None, connection_factory=None, cursor_factory=None,
async=False, **kwargs):
async=False, **kwargs):
"""
Create a new database connection.
Expand Down
14 changes: 8 additions & 6 deletions lib/_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


# import the best json implementation available
if sys.version_info[:2] >= (2,6):
if sys.version_info[:2] >= (2, 6):
import json
else:
try:
Expand All @@ -51,6 +51,7 @@
JSONB_OID = 3802
JSONBARRAY_OID = 3807


class Json(object):
"""
An `~psycopg2.extensions.ISQLQuote` wrapper to adapt a Python object to
Expand Down Expand Up @@ -106,7 +107,7 @@ def __str__(self):


def register_json(conn_or_curs=None, globally=False, loads=None,
oid=None, array_oid=None, name='json'):
oid=None, array_oid=None, name='json'):
"""Create and register typecasters converting :sql:`json` type to Python objects.
:param conn_or_curs: a connection or cursor used to find the :sql:`json`
Expand Down Expand Up @@ -143,6 +144,7 @@ def register_json(conn_or_curs=None, globally=False, loads=None,

return JSON, JSONARRAY


def register_default_json(conn_or_curs=None, globally=False, loads=None):
"""
Create and register :sql:`json` typecasters for PostgreSQL 9.2 and following.
Expand All @@ -155,6 +157,7 @@ def register_default_json(conn_or_curs=None, globally=False, loads=None):
return register_json(conn_or_curs=conn_or_curs, globally=globally,
loads=loads, oid=JSON_OID, array_oid=JSONARRAY_OID)


def register_default_jsonb(conn_or_curs=None, globally=False, loads=None):
"""
Create and register :sql:`jsonb` typecasters for PostgreSQL 9.4 and following.
Expand All @@ -167,6 +170,7 @@ def register_default_jsonb(conn_or_curs=None, globally=False, loads=None):
return register_json(conn_or_curs=conn_or_curs, globally=globally,
loads=loads, oid=JSONB_OID, array_oid=JSONBARRAY_OID, name='jsonb')


def _create_json_typecasters(oid, array_oid, loads=None, name='JSON'):
"""Create typecasters for json data type."""
if loads is None:
Expand All @@ -188,6 +192,7 @@ def typecast_json(s, cur):

return JSON, JSONARRAY


def _get_json_oids(conn_or_curs, name='json'):
# lazy imports
from psycopg2.extensions import STATUS_IN_TRANSACTION
Expand All @@ -204,7 +209,7 @@ def _get_json_oids(conn_or_curs, name='json'):
# get the oid for the hstore
curs.execute(
"SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;"
% typarray, (name,))
% typarray, (name,))
r = curs.fetchone()

# revert the status of the connection as before the command
Expand All @@ -215,6 +220,3 @@ def _get_json_oids(conn_or_curs, name='json'):
raise conn.ProgrammingError("%s data type not found" % name)

return r



39 changes: 25 additions & 14 deletions lib/_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from psycopg2.extensions import ISQLQuote, adapt, register_adapter
from psycopg2.extensions import new_type, new_array_type, register_type


class Range(object):
"""Python representation for a PostgreSQL |range|_ type.
Expand Down Expand Up @@ -78,42 +79,50 @@ def isempty(self):
@property
def lower_inf(self):
"""`!True` if the range doesn't have a lower bound."""
if self._bounds is None: return False
if self._bounds is None:
return False
return self._lower is None

@property
def upper_inf(self):
"""`!True` if the range doesn't have an upper bound."""
if self._bounds is None: return False
if self._bounds is None:
return False
return self._upper is None

@property
def lower_inc(self):
"""`!True` if the lower bound is included in the range."""
if self._bounds is None: return False
if self._lower is None: return False
if self._bounds is None or self._lower is None:
return False
return self._bounds[0] == '['

@property
def upper_inc(self):
"""`!True` if the upper bound is included in the range."""
if self._bounds is None: return False
if self._upper is None: return False
if self._bounds is None or self._upper is None:
return False
return self._bounds[1] == ']'

def __contains__(self, x):
if self._bounds is None: return False
if self._bounds is None:
return False

if self._lower is not None:
if self._bounds[0] == '[':
if x < self._lower: return False
if x < self._lower:
return False
else:
if x <= self._lower: return False
if x <= self._lower:
return False

if self._upper is not None:
if self._bounds[1] == ']':
if x > self._upper: return False
if x > self._upper:
return False
else:
if x >= self._upper: return False
if x >= self._upper:
return False

return True

Expand Down Expand Up @@ -295,7 +304,8 @@ def _create_ranges(self, pgrange, pyrange):
self.adapter.name = pgrange
else:
try:
if issubclass(pgrange, RangeAdapter) and pgrange is not RangeAdapter:
if issubclass(pgrange, RangeAdapter) \
and pgrange is not RangeAdapter:
self.adapter = pgrange
except TypeError:
pass
Expand Down Expand Up @@ -436,14 +446,17 @@ class NumericRange(Range):
"""
pass


class DateRange(Range):
"""Represents :sql:`daterange` values."""
pass


class DateTimeRange(Range):
"""Represents :sql:`tsrange` values."""
pass


class DateTimeTZRange(Range):
"""Represents :sql:`tstzrange` values."""
pass
Expand Down Expand Up @@ -508,5 +521,3 @@ def getquoted(self):
tstzrange_caster = RangeCaster('tstzrange', DateTimeTZRange,
oid=3910, subtype_oid=1184, array_oid=3911)
tstzrange_caster._register()


1 change: 1 addition & 0 deletions lib/errorcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
# http://www.postgresql.org/docs/current/static/errcodes-appendix.html
#


def lookup(code, _cache={}):
"""Lookup an error code or class code and return its symbolic name.
Expand Down
82 changes: 40 additions & 42 deletions lib/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,71 +33,69 @@
# License for more details.

import re as _re
import sys as _sys

from psycopg2._psycopg import UNICODE, INTEGER, LONGINTEGER, BOOLEAN, FLOAT
from psycopg2._psycopg import TIME, DATE, INTERVAL, DECIMAL
from psycopg2._psycopg import BINARYARRAY, BOOLEANARRAY, DATEARRAY, DATETIMEARRAY
from psycopg2._psycopg import DECIMALARRAY, FLOATARRAY, INTEGERARRAY, INTERVALARRAY
from psycopg2._psycopg import LONGINTEGERARRAY, ROWIDARRAY, STRINGARRAY, TIMEARRAY
from psycopg2._psycopg import UNICODEARRAY
from psycopg2._psycopg import ( # noqa
BINARYARRAY, BOOLEAN, BOOLEANARRAY, DATE, DATEARRAY, DATETIMEARRAY,
DECIMAL, DECIMALARRAY, FLOAT, FLOATARRAY, INTEGER, INTEGERARRAY,
INTERVAL, INTERVALARRAY, LONGINTEGER, LONGINTEGERARRAY, ROWIDARRAY,
STRINGARRAY, TIME, TIMEARRAY, UNICODE, UNICODEARRAY,
AsIs, Binary, Boolean, Float, Int, QuotedString, )

from psycopg2._psycopg import Binary, Boolean, Int, Float, QuotedString, AsIs
try:
from psycopg2._psycopg import MXDATE, MXDATETIME, MXINTERVAL, MXTIME
from psycopg2._psycopg import MXDATEARRAY, MXDATETIMEARRAY, MXINTERVALARRAY, MXTIMEARRAY
from psycopg2._psycopg import DateFromMx, TimeFromMx, TimestampFromMx
from psycopg2._psycopg import IntervalFromMx
from psycopg2._psycopg import ( # noqa
MXDATE, MXDATETIME, MXINTERVAL, MXTIME,
MXDATEARRAY, MXDATETIMEARRAY, MXINTERVALARRAY, MXTIMEARRAY,
DateFromMx, TimeFromMx, TimestampFromMx, IntervalFromMx, )
except ImportError:
pass

try:
from psycopg2._psycopg import PYDATE, PYDATETIME, PYINTERVAL, PYTIME
from psycopg2._psycopg import PYDATEARRAY, PYDATETIMEARRAY, PYINTERVALARRAY, PYTIMEARRAY
from psycopg2._psycopg import DateFromPy, TimeFromPy, TimestampFromPy
from psycopg2._psycopg import IntervalFromPy
from psycopg2._psycopg import ( # noqa
PYDATE, PYDATETIME, PYINTERVAL, PYTIME,
PYDATEARRAY, PYDATETIMEARRAY, PYINTERVALARRAY, PYTIMEARRAY,
DateFromPy, TimeFromPy, TimestampFromPy, IntervalFromPy, )
except ImportError:
pass

from psycopg2._psycopg import adapt, adapters, encodings, connection, cursor
from psycopg2._psycopg import lobject, Xid, libpq_version, parse_dsn, quote_ident
from psycopg2._psycopg import string_types, binary_types, new_type, new_array_type, register_type
from psycopg2._psycopg import ISQLQuote, Notify, Diagnostics, Column
from psycopg2._psycopg import ( # noqa
adapt, adapters, encodings, connection, cursor,
lobject, Xid, libpq_version, parse_dsn, quote_ident,
string_types, binary_types, new_type, new_array_type, register_type,
ISQLQuote, Notify, Diagnostics, Column,
QueryCanceledError, TransactionRollbackError,
set_wait_callback, get_wait_callback, )

from psycopg2._psycopg import QueryCanceledError, TransactionRollbackError

try:
from psycopg2._psycopg import set_wait_callback, get_wait_callback
except ImportError:
pass

"""Isolation level values."""
ISOLATION_LEVEL_AUTOCOMMIT = 0
ISOLATION_LEVEL_READ_UNCOMMITTED = 4
ISOLATION_LEVEL_READ_COMMITTED = 1
ISOLATION_LEVEL_REPEATABLE_READ = 2
ISOLATION_LEVEL_SERIALIZABLE = 3
ISOLATION_LEVEL_AUTOCOMMIT = 0
ISOLATION_LEVEL_READ_UNCOMMITTED = 4
ISOLATION_LEVEL_READ_COMMITTED = 1
ISOLATION_LEVEL_REPEATABLE_READ = 2
ISOLATION_LEVEL_SERIALIZABLE = 3


"""psycopg connection status values."""
STATUS_SETUP = 0
STATUS_READY = 1
STATUS_BEGIN = 2
STATUS_SYNC = 3 # currently unused
STATUS_ASYNC = 4 # currently unused
STATUS_SETUP = 0
STATUS_READY = 1
STATUS_BEGIN = 2
STATUS_SYNC = 3 # currently unused
STATUS_ASYNC = 4 # currently unused
STATUS_PREPARED = 5

# This is a useful mnemonic to check if the connection is in a transaction
STATUS_IN_TRANSACTION = STATUS_BEGIN


"""psycopg asynchronous connection polling values"""
POLL_OK = 0
POLL_READ = 1
POLL_OK = 0
POLL_READ = 1
POLL_WRITE = 2
POLL_ERROR = 3


"""Backend transaction status values."""
TRANSACTION_STATUS_IDLE = 0
TRANSACTION_STATUS_ACTIVE = 1
TRANSACTION_STATUS_IDLE = 0
TRANSACTION_STATUS_ACTIVE = 1
TRANSACTION_STATUS_INTRANS = 2
TRANSACTION_STATUS_INERROR = 3
TRANSACTION_STATUS_UNKNOWN = 4
Expand Down Expand Up @@ -194,7 +192,7 @@ def _param_escape(s,


# Create default json typecasters for PostgreSQL 9.2 oids
from psycopg2._json import register_default_json, register_default_jsonb
from psycopg2._json import register_default_json, register_default_jsonb # noqa

try:
JSON, JSONARRAY = register_default_json()
Expand All @@ -206,7 +204,7 @@ def _param_escape(s,


# Create default Range typecasters
from psycopg2. _range import Range
from psycopg2. _range import Range # noqa
del Range


Expand Down
Loading

0 comments on commit 91d2158

Please sign in to comment.