More documentation, getting ready to put this on

PyPi.
This commit is contained in:
Friedrich Lindenberg
2013-03-30 22:28:32 +01:00
parent 867008c016
commit c27f8155bd
9 changed files with 98 additions and 71 deletions
+3 -2
View File
@@ -1,4 +1,3 @@
from sqlaload.schema import connect
from sqlaload.schema import create_table, load_table, get_table, drop_table
from sqlaload.schema import create_column
@@ -6,4 +5,6 @@ from sqlaload.write import add_row, update_row
from sqlaload.write import upsert, update, delete
from sqlaload.query import distinct, resultiter, all, find_one, find, query
from sqlaload.util import dump_csv
# shut up useless SA warning:
import warnings
warnings.filterwarnings('ignore', 'Unicode type received non-unicode bind param value.')
+6 -8
View File
@@ -2,7 +2,7 @@ import logging
from itertools import count
from sqlalchemy.sql import expression, and_
from sqlaload.schema import _ensure_columns
from sqlaload.schema import _ensure_columns, get_table
log = logging.getLogger(__name__)
@@ -17,19 +17,17 @@ def resultiter(rp):
yield dict(zip(keys, row))
def find_one(engine, table, **kw):
table = get_table(engine, table)
res = list(find(engine, table, _limit=1, **kw))
if not len(res):
return None
return res[0]
def find(engine, table, _limit=None, _step=5000, _offset=0,
order_by=None, **kw):
order_by='id', **kw):
table = get_table(engine, table)
_ensure_columns(engine, table, kw)
if order_by is None:
order_by = [table.c.id.asc()]
else:
order_by = [table.c[order_by].asc()]
order_by = [table.c[order_by].asc()]
qargs = []
try:
@@ -59,7 +57,7 @@ def query(engine, query):
yield res
def distinct(engine, table, *columns, **kw):
table = get_table(engine, table)
qargs = []
try:
columns = [table.c[c] for c in columns]
+5
View File
@@ -46,6 +46,9 @@ def load_table(engine, table_name):
return table
def get_table(engine, table_name):
if isinstance(table_name, Table):
return table_name
# Accept Connection objects here
if hasattr(engine, 'engine'):
engine = engine.engine
@@ -102,12 +105,14 @@ def _args_to_clause(table, args):
return and_(*clauses)
def create_column(engine, table, name, type):
table = get_table(engine, table)
with lock:
if name not in table.columns.keys():
col = Column(name, type)
col.create(table, connection=engine)
def create_index(engine, table, columns, name=None):
table = get_table(engine, table)
with lock:
if not name:
sig = abs(hash('||'.join(columns)))
-24
View File
@@ -1,24 +0,0 @@
import csv
from datetime import datetime
def _convert_cell(v):
if isinstance(v, unicode):
return v.encode('utf-8')
elif isinstance(v, datetime):
return v.isoformat()
return v
def dump_csv(query_iter, fh):
writer, columns = None, None
for row in query_iter:
if writer is None:
writer = csv.writer(fh)
columns = row.keys()
writer.writerow(columns)
writer.writerow([_convert_cell(row.get(c)) \
for c in columns])
fh.close()
+6 -1
View File
@@ -1,7 +1,7 @@
import logging
from sqlaload.schema import _ensure_columns, _args_to_clause
from sqlaload.schema import create_index
from sqlaload.schema import create_index, get_table
log = logging.getLogger(__name__)
@@ -9,6 +9,7 @@ def add_row(engine, table, row, ensure=True, types={}):
""" Add a row (type: dict). If ``ensure`` is set, any of
the keys of the row are not table columns, they will be type
guessed and created. """
table = get_table(engine, table)
if ensure:
_ensure_columns(engine, table, row, types=types)
engine.execute(table.insert(row))
@@ -16,6 +17,7 @@ def add_row(engine, table, row, ensure=True, types={}):
def update_row(engine, table, row, unique, ensure=True, types={}):
if not len(unique):
return False
table = get_table(engine, table)
clause = dict([(u, row.get(u)) for u in unique])
if ensure:
_ensure_columns(engine, table, row, types=types)
@@ -28,6 +30,7 @@ def update_row(engine, table, row, unique, ensure=True, types={}):
return False
def upsert(engine, table, row, unique, ensure=True, types={}):
table = get_table(engine, table)
if ensure:
create_index(engine, table, unique)
@@ -35,6 +38,7 @@ def upsert(engine, table, row, unique, ensure=True, types={}):
add_row(engine, table, row, ensure=ensure, types=types)
def update(engine, table, criteria, values, ensure=True, types={}):
table = get_table(engine, table)
if ensure:
_ensure_columns(engine, table, values, types=types)
q = table.update().values(values)
@@ -43,6 +47,7 @@ def update(engine, table, criteria, values, ensure=True, types={}):
engine.execute(q)
def delete(engine, table, **kw):
table = get_table(engine, table)
_ensure_columns(engine, table, kw)
qargs = []