0

I need to apply some regex substitutions on every element on a list. I wrote a function to repeat myself less. anyway there's still too much repeating. How could I optimize this?

def cleanlist(mylist, regex, substitution):
    tmp_list = mylist
    cleaned_list = [re.sub(regex, substitution, line) for line in tmp_list]
    return cleaned_list


create_table_parts = cleanlist(create_table_parts, "(SET).+?(\n)", "\n") 
create_table_parts = cleanlist(create_table_parts, "(__|\(__).*?\n|(^\)|(?<=\n)(\n))", "")
create_table_parts = cleanlist(create_table_parts, "\"", "")
create_table_parts = cleanlist(create_table_parts, "(?<=CREATE\sTABLE\s).+?(\.)", "") 
create_table_parts = cleanlist(create_table_parts, "(PRIMARY\sKEY\s).+?(\n)|(FOREIGN\sKEY\s).+?(\n)|", "")
create_table_parts = cleanlist(create_table_parts, "(CREATE_INDEX\s).+?(\n)", "") 
tshepang
  • 12,111
  • 21
  • 91
  • 136
royskatt
  • 1,190
  • 2
  • 15
  • 35

1 Answers1

4

Put your patterns in a list and loop:

patterns = [
    ("(SET).+?(\n)", "\n"),
    ("(__|\(__).*?\n|(^\)|(?<=\n)(\n))", ""),
    ("\"", ""), ("(?<=CREATE\sTABLE\s).+?(\.)", ""),
    ("(PRIMARY\sKEY\s).+?(\n)|(FOREIGN\sKEY\s).+?(\n)|", ""),
    ("(CREATE_INDEX\s).+?(\n)", "")
]

for patt, sub in patterns:
    create_table_parts = cleanlist(create_table_parts, patt, sub)

You could even use reduce() for this, instead of the for loop:

create_table_parts = reduce(lambda ctp, patt: cleanlist(ctp, *patt),
                            patterns, create_table_parts)

but it's a personal call wether or not that's any more readable.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks, honestly said, I find that reduce solution very unintuitive and not easy to read, compared to the for-loop. – royskatt May 28 '14 at 11:27
  • 1
    @royskatt: agreed, it's there for completeness sake. Any `for` loop that repeatedly replaces the input with the output of a function call is a target for a `reduce()` call, but that's then also a personal call to actually use it or not. – Martijn Pieters May 28 '14 at 11:30