@@ -16,14 +16,14 @@ class TokenTypes(object):
1616 EndStatement = 9
1717
1818class Delimiter (object ):
19- def __init__ (self , terminal_printer , delimiter = ',' , group_chars = None ):
19+ def __init__ (self , terminal_printer , delimiter = ',' , add_parens = False ):
2020 """
2121 Delimited group printer
2222
2323 A group of items that should be delimited by a delimiter character.
2424 Each call to new_item() will insert the delimiter character if necessary.
2525
26- When used as a context manager, the group will be enclosed by the start and end characters.
26+ When used as a context manager, the group will be enclosed by the start and end characters if the group has any items .
2727
2828 >>> d = Delimiter(terminal_printer)
2929 ... d.new_item()
@@ -36,24 +36,22 @@ def __init__(self, terminal_printer, delimiter=',', group_chars=None):
3636 ... print(terminal_printer.code)
3737 a,b
3838
39- >>> with Delimiter(terminal_printer, group_chars=('(', ')') ) as d:
39+ >>> with Delimiter(terminal_printer, add_parens=True ) as d:
4040 ... d.new_item()
4141 ... terminal_printer.identifier('a')
4242 ... print(terminal_printer.code)
4343 (a)
4444
4545 :param terminal_printer: The terminal printer to use.
4646 :param delimiter: The delimiter to use.
47- :param group_chars: The characters to use to open and close the delimited group .
47+ :param add_parens: If the group should be enclosed by parentheses. Only used when used as a context manager .
4848 """
4949
5050 self ._terminal_printer = terminal_printer
5151 self ._delimiter = delimiter
52+ self ._add_parens = add_parens
5253
53- if group_chars :
54- self ._start_char , self ._end_char = group_chars
55-
56- self .first = True
54+ self ._first = True
5755
5856 self ._context_manager = False
5957
@@ -64,25 +62,23 @@ def __enter__(self):
6462
6563 def __exit__ (self , exc_type , exc_val , exc_tb ):
6664 """Close the delimited group."""
67- if not self .first and self ._end_char :
68- self ._terminal_printer .delimiter (self . _end_char )
65+ if not self ._first and self ._add_parens :
66+ self ._terminal_printer .delimiter (')' )
6967
7068 def new_item (self ):
7169 """Add a new item to the delimited group."""
72- if self .first :
73- self .first = False
74- if self ._context_manager and self ._start_char :
75- self ._terminal_printer .delimiter (self . _start_char )
70+ if self ._first :
71+ self ._first = False
72+ if self ._context_manager and self ._add_parens :
73+ self ._terminal_printer .delimiter ('(' )
7674 else :
7775 self ._terminal_printer .delimiter (self ._delimiter )
7876
79- class TerminalPrinter (object ):
77+ class TokenPrinter (object ):
8078 """
8179 Concatenates terminal symbols of the python grammar
8280 """
8381
84- #__slots__ = ['__code', 'indent', 'unicode_literals', 'previous_token', '_prefer_single_line', '_allow_invalid_num_warnings']
85-
8682 def __init__ (self , prefer_single_line = False , allow_invalid_num_warnings = False ):
8783 """
8884 :param prefer_single_line: If True, chooses to put as much code as possible on a single line.
0 commit comments