Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
just tidying function docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
smnorris committed Jan 19, 2018
1 parent 871cca0 commit 2d31f18
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 55 deletions.
7 changes: 5 additions & 2 deletions pgdata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@


def connect(url=None, schema=None, sql_path='sql', multiprocessing=False):
""" Open a new connection to postgres via psycopg2/sqlalchemy
db = pgdata.connect('')
"""Open a new connection to postgres via psycopg2/sqlalchemy
"""
if url is None:
url = os.environ.get('DATABASE_URL')
return Database(url, schema, multiprocessing=multiprocessing)


def create_db(url=None):
"""Create a new database
"""
if url is None:
url = os.environ.get('DATABASE_URL')
parsed_url = urlparse(url)
Expand All @@ -41,6 +42,8 @@ def create_db(url=None):


def drop_db(url=None):
"""Drop specified database
"""
if url is None:
url = os.environ.get('DATABASE_URL')
parsed_url = urlparse(url)
Expand Down
28 changes: 2 additions & 26 deletions pgdata/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ def tables(self):
[schema+"."+t for t in self.tables_in_schema(schema)]
return tables

"""
def _get_connection(self):
c = psycopg2.connect(database=self.database,
user=self.user,
password=self.password,
host=self.host,
port=self.port,
cursor_factory=extras.DictCursor)
c.autocommit = True
return c
"""

def print_notices(self):
for notice in self.psycopg2_conn.notices:
print(notice)
Expand All @@ -93,9 +81,6 @@ def __getitem__(self, table):
else:
return Table(self, "public", None)

#def _get_cursor(self):
# return self.conn.cursor()

def _valid_table_name(self, table):
"""Check if the table name is obviously invalid.
"""
Expand Down Expand Up @@ -143,7 +128,7 @@ def tables_in_schema(self, schema):
return [t[0] for t in self.query(sql, (schema,)).fetchall()]

def parse_table_name(self, table):
"""parse schema qualified table name
"""Parse schema qualified table name
"""
if "." in table:
schema, table = table.split('.')
Expand All @@ -167,10 +152,8 @@ def load_table(self, table):
return None

def execute(self, sql, params=None):
"""Just a pointer to engine.execute
"""
Just a pointer to engine.execute
"""
#return self._get_cursor().execute(sql, params)
# wrap in a transaction to ensure things are committed
# https://github.com/smnorris/pgdata/issues/3
with self.engine.begin() as conn:
Expand All @@ -180,23 +163,16 @@ def execute(self, sql, params=None):
def execute_many(self, sql, params):
"""Wrapper for executemany.
"""
#self._get_cursor().executemany(sql, params)
self.engine.executemany(sql, params)

def query(self, sql, params=None):
"""Another word for execute
"""
#cur = self._get_cursor()
#cur.execute(sql, params)
#return cur.fetchall()
return self.engine.execute(sql, params)

def query_one(self, sql, params=None):
"""Grab just one record
"""
#cur = self._get_cursor()
#cur.execute(sql, params)
#return cur.fetchone()
r = self.engine.execute(sql, params)
return r.fetchone()

Expand Down
38 changes: 15 additions & 23 deletions pgdata/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,19 @@ def _normalized_columns(self):

@property
def columns(self):
"""
Return list of all columns in table
"""Return list of all columns in table
"""
return list(self.table.columns.keys())

@property
def sqla_columns(self):
"""
Return all columns in table as sqlalchemy column types
"""Return all columns in table as sqlalchemy column types
"""
return self.table.columns

@property
def column_types(self):
"""
Return a dict mapping column name to type for all columns in table
"""Return a dict mapping column name to type for all columns in table
"""
column_types = {}
for c in self.sqla_columns:
Expand All @@ -83,8 +80,7 @@ def column_types(self):

@property
def primary_key(self):
"""
return a list of columns making up the primary key constraint
"""Return a list of columns making up the primary key constraint
"""
return [c.name for c in self.table.primary_key]

Expand All @@ -94,7 +90,8 @@ def op(self):
return Operations(ctx)

def _valid_table_name(self, table_name):
""" Check if the table name is obviously invalid. """
"""Check if the table name is obviously invalid.
"""
if table_name is None or not len(table_name.strip()):
raise ValueError("Invalid table name: %r" % table_name)
return table_name.strip()
Expand All @@ -105,8 +102,7 @@ def _update_table(self, table_name):
return SQLATable(table_name, self.metadata, schema=self.schema)

def add_primary_key(self, column="id"):
"""
add primary key constraint to specified column
"""Add primary key constraint to specified column
"""
if not self.primary_key:
sql = """ALTER TABLE {s}.{t}
Expand All @@ -117,8 +113,7 @@ def add_primary_key(self, column="id"):
self.db.execute(sql)

def drop(self):
"""
Drop the table from the database
"""Drop the table from the database
"""
if self._is_dropped is False:
self.table.drop(self.engine)
Expand Down Expand Up @@ -200,8 +195,7 @@ def create_index(self, columns, name=None, index_type="btree"):
return idx

def create_index_geom(self, column="geom"):
"""
shortcut to create index on geometry
"""Shortcut to create index on geometry
"""
self.create_index([column], index_type="gist")

Expand Down Expand Up @@ -277,6 +271,8 @@ def _process_chunk(chunk):
_process_chunk(chunk)

def rename(self, name):
"""Rename the table
"""
sql = """ALTER TABLE {s}.{t} RENAME TO {name}
""".format(s=self.schema, t=self.name, name=name)
self.engine.execute(sql)
Expand Down Expand Up @@ -363,18 +359,14 @@ def find(self, _limit=None, _offset=0, _step=5000,

def count(self, **_filter):
"""
Return the count of results for the given filter set (same filter options as with ``find()``).
Return the count of results for the given filter set
(same filter options as with ``find()``).
"""
return self.find(return_count=True, **_filter)

#def __len__(self):
# """
# Returns the number of rows in the table.
# """
# return self.count()

def __getitem__(self, item):
""" This is an alias for distinct which allows the table to be queried as using
"""
This is an alias for distinct which allows the table to be queried as using
square bracket syntax.
::
# Same as distinct:
Expand Down
8 changes: 4 additions & 4 deletions pgdata/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ def convert_row(row_type, row):


class ResultIter(object):
""" SQLAlchemy ResultProxies are not iterable to get a
list of dictionaries. This is to wrap them. """

"""
SQLAlchemy ResultProxies are not iterable to get a list of dictionaries.
This is to wrap them.
"""
def __init__(self, result_proxies, row_type=row_type):
self.row_type = row_type
if not isgenerator(result_proxies):
Expand Down Expand Up @@ -65,4 +66,3 @@ def __next__(self):

def __iter__(self):
return self

0 comments on commit 2d31f18

Please sign in to comment.