Skip to content

Commit f9990c8

Browse files
committed
Make some minor tweaks to mysqldump_to_csv.py
* 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]>
1 parent 24301df commit f9990c8

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

mysqldump_to_csv.py

100644100755
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def values_sanity_check(values):
3030
Ensures that values from the INSERT statement meet basic checks.
3131
"""
3232
assert values
33-
assert values[0] == '('
33+
assert values[0].startswith("(")
3434
# Assertions have not been raised
3535
return True
3636

@@ -53,16 +53,16 @@ def parse_values(values, outfile):
5353
for reader_row in reader:
5454
for column in reader_row:
5555
# If our current string is empty...
56-
if len(column) == 0 or column == 'NULL':
56+
if not column or column == "NULL":
5757
latest_row.append(chr(0))
5858
continue
5959
# If our string starts with an open paren
60-
if column[0] == "(":
60+
if column.startswith("("):
6161
# Assume that this column does not begin
6262
# a new row.
6363
new_row = False
6464
# If we've been filling out a row
65-
if len(latest_row) > 0:
65+
if latest_row:
6666
# Check if the previous entry ended in
6767
# a close paren. If so, the row we've
6868
# been filling out has been COMPLETED
@@ -80,15 +80,15 @@ def parse_values(values, outfile):
8080
latest_row = []
8181
# If we're beginning a new row, eliminate the
8282
# opening parentheses.
83-
if len(latest_row) == 0:
83+
if not latest_row:
8484
column = column[1:]
8585
# Add our column to the row we're working on.
8686
latest_row.append(column)
8787
# At the end of an INSERT statement, we'll
8888
# have the semicolon.
8989
# Make sure to remove the semicolon and
9090
# the close paren.
91-
if latest_row[-1][-2:] == ");":
91+
if latest_row[-1].endswith(");"):
9292
latest_row[-1] = latest_row[-1][:-2]
9393
writer.writerow(latest_row)
9494

0 commit comments

Comments
 (0)