Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Coding dojo: SQL code generator for an SQL comment to describe items to exclude.
"""
Coding dojo: SQL code generator for an SQL comment to describe items to
exclude.
Write a function that generates an SQL comment describing a list of items to
exclude. Long lists of items should be broken into multiple lines. To make the
test case more managable, the maximum line length is 30.
For example, ['one', 'two'] should result in
-- Excluding: one, two
while ['one', 'two', 'some_more', 'even_more'] should result in
-- Excluding: one, two,
-- some_more, even_more
You can make the following assumptions for the list of items to exclude:
* The list is neither empty nor ``None``.
* All items are unicode strings.
* All items fite in one line (have less than 27 characters).
Below you can find an example implementation and test cases.
Things you can learn form this dojo:
1. The textwrap module can be handy.
2. str.join() makes it easy to build a text of comma separated parts.
"""
import unittest
import textwrap
WIDTH = 30 # Maximum length of each line in the generated SQL comment.
def sql_exclude_comment(items_to_exclude):
assert items_to_exclude
for item in items_to_exclude:
assert 1 <= len(item) <= WIDTH - 4
item_text = ', '.join(items_to_exclude)
wrapper = textwrap.TextWrapper(
initial_indent='-- Excluding: ', subsequent_indent='-- ', width=WIDTH)
return wrapper.fill(item_text) + '\n'
class SqlExcludeCommentTest(unittest.TestCase):
def test_can_generate_one_item(self):
self.assertEqual(
'-- Excluding: one\n',
sql_exclude_comment(['one']))
def test_can_generate_two_items(self):
self.assertEqual(
'-- Excluding: one, two\n',
sql_exclude_comment(['one', 'two']))
def test_can_break_line(self):
self.assertEqual(
'-- Excluding: one, two,\n-- some_more, even_more\n',
sql_exclude_comment(['one', 'two', 'some_more', 'even_more']))
def test_can_break_multiple_lines(self):
self.assertEqual(
'-- Excluding: one, two,\n'
'-- some_more, even_more,\n'
'-- almost_done, the_end\n',
sql_exclude_comment([
'one', 'two', 'some_more', 'even_more', 'almost_done',
'the_end'
]))
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.