Skip to content

Commit

Permalink
Make some minor tweaks to mysqldump_to_csv.py
Browse files Browse the repository at this point in the history
* Use `.startswith(..)` in place of the unrolled versions of the
  equivalent functionality.
* Use `if foo:` and `if not foo:` instead of `if len(foo) != 0:`
  and `if len(foo) == 0:`. The former set of patterns is more
  performant and it protects against `foo` potentially not being
  an iterable object, or an object that doesn't implement
  `.__len__(..)`.
* Change the mode of the script to 0755 so it's executable out of
  the box.

Signed-of-by:	Enji Cooper <[email protected]>
  • Loading branch information
ngie-eign committed Nov 14, 2019
1 parent 24301df commit e27261f
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions mysqldump_to_csv.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ def parse_values(values, outfile):
for reader_row in reader:
for column in reader_row:
# If our current string is empty...
if len(column) == 0 or column == 'NULL':
if not column or column == "NULL":
latest_row.append(chr(0))
continue
# If our string starts with an open paren
if column[0] == "(":
if column.startswith("("):
# Assume that this column does not begin
# a new row.
new_row = False
# If we've been filling out a row
if len(latest_row) > 0:
if latest_row:
# Check if the previous entry ended in
# a close paren. If so, the row we've
# been filling out has been COMPLETED
Expand All @@ -80,15 +80,15 @@ def parse_values(values, outfile):
latest_row = []
# If we're beginning a new row, eliminate the
# opening parentheses.
if len(latest_row) == 0:
if not latest_row:
column = column[1:]
# Add our column to the row we're working on.
latest_row.append(column)
# At the end of an INSERT statement, we'll
# have the semicolon.
# Make sure to remove the semicolon and
# the close paren.
if latest_row[-1][-2:] == ");":
if latest_row[-1].endswith(");"):
latest_row[-1] = latest_row[-1][:-2]
writer.writerow(latest_row)

Expand Down

0 comments on commit e27261f

Please sign in to comment.