0
\$\begingroup\$

I have a file of SQL (actually HiveQL) queries terminated by semicolon (;). I need to obtain a list of formatted queries from this file. Formatting includes replacing multiple spaces with single one and removing extra spaces in cases like '( ', ' )', ' ,'. So, the following is my approach and it looks monstrous:

fdHql = open(args.hql, 'r')
hql_file = fdHql.read()
fdHql.close()
queries = [re.sub('\s+', ' ', each.replace('\n', ' ').strip()).replace('( ', '(').replace(' )', ')').replace(' ,', ',') for each in hql_file.split(';')]

Is there a better way to implement same behavior?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You can eliminate the .close() using a with ... as ...: statement.

Since newlines are white-space characters, re.sub('\s+', ' ', ...) will already convert any '\n' characters to a space, so the .replace('\n', ' ') is unnecessary.

And you don't need to write everything on one line, even when you want a "one-liner".

with open(args.hql, 'r') as fdHql:
    queries = [
               re.sub('\s+', ' ', each.strip())
                   .replace('( ', '(')
                   .replace(' )', ')')
                   .replace(' ,', ',')
               for each in fdHql.read().split(';')
              ]

Of course, the clearest is to move the work to a helper function.

def _format_sql(sql):
    return re.sub('\s+', ' ', sql.strip())      \
               .replace('( ', '(')              \
               .replace(' )', ')')              \
               .replace(' ,', ',')

with open(args.hql, 'r') as fdHql:
    queries = [ _format_sql(each) for each in fdHql.read().split(';') ]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.