Refactor input_many to remove duplicate code, fix some pep8 problems
This commit is contained in:
parent
76b6165181
commit
7fd9241f25
@ -135,17 +135,15 @@ class Table(object):
|
|||||||
columns = sync_row.keys()
|
columns = sync_row.keys()
|
||||||
|
|
||||||
chunk = []
|
chunk = []
|
||||||
for row in rows:
|
for index, row in enumerate(rows):
|
||||||
chunk.append(row)
|
chunk.append(row)
|
||||||
if len(chunk) == chunk_size:
|
|
||||||
|
# Insert when chunk_size is fulfilled or this is the last row
|
||||||
|
if len(chunk) == chunk_size or index == len(row) - 1:
|
||||||
chunk = pad_chunk_columns(chunk, columns)
|
chunk = pad_chunk_columns(chunk, columns)
|
||||||
self.table.insert().execute(chunk)
|
self.table.insert().execute(chunk)
|
||||||
chunk = []
|
chunk = []
|
||||||
|
|
||||||
if len(chunk):
|
|
||||||
chunk = pad_chunk_columns(chunk, columns)
|
|
||||||
self.table.insert().execute(chunk)
|
|
||||||
|
|
||||||
def update(self, row, keys, ensure=None, types=None, return_count=False):
|
def update(self, row, keys, ensure=None, types=None, return_count=False):
|
||||||
"""Update a row in the table.
|
"""Update a row in the table.
|
||||||
|
|
||||||
@ -189,7 +187,7 @@ class Table(object):
|
|||||||
|
|
||||||
chunk = []
|
chunk = []
|
||||||
columns = set()
|
columns = set()
|
||||||
for row in rows:
|
for index, row in enumerate(rows):
|
||||||
chunk.append(row)
|
chunk.append(row)
|
||||||
columns = columns.union(set(row.keys()))
|
columns = columns.union(set(row.keys()))
|
||||||
|
|
||||||
@ -197,14 +195,14 @@ class Table(object):
|
|||||||
for key in keys:
|
for key in keys:
|
||||||
row[f'_{key}'] = row[f'{key}']
|
row[f'_{key}'] = row[f'{key}']
|
||||||
|
|
||||||
# Update when chunksize is fulfilled or this is the last row
|
# Update when chunk_size is fulfilled or this is the last row
|
||||||
if len(chunk) == chunk_size or rows.index(row) == len(rows)-1:
|
if len(chunk) == chunk_size or index == len(rows) - 1:
|
||||||
stmt = self.table.update(
|
stmt = self.table.update(
|
||||||
whereclause=and_(
|
whereclause=and_(
|
||||||
*[self.table.c[key] == bindparam(f'_{key}') for key in keys]
|
*[self.table.c[k] == bindparam(f'_{k}') for k in keys]
|
||||||
),
|
),
|
||||||
values={
|
values={
|
||||||
column: bindparam(column, required=False) for column in columns
|
col: bindparam(col, required=False) for col in columns
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
self.db.executable.execute(stmt, chunk)
|
self.db.executable.execute(stmt, chunk)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user