Пример #1
0
    def inject_assignment(self,
                          name,
                          value,
                          extra_trailing_newline=False,
                          convert_value=True,
                          line_index=0):
        '''
        Injects a variable assignment, name = value, at line_index.

        Generated variables get one newline before. If you want to pass in raw
        source (instead of a value to be converted into source by a formatter),
        use convert_value=False.

        If extra_trailing_newline==True, one additional newline will be added
        after the variable declaration, so that there's a full blank line
        between it and the next line of source code.

        line_index is relative to the node body. If line_index is not provided,
        the variable will be inserted at node.body_start_line.
        '''

        indentation = self.node.inner_indentation
        if convert_value:
            value = format_value(value, starting_indentation=indentation)

        generated_source = '\n' + indentation + name + ' = ' + value + '\n'

        if extra_trailing_newline:
            generated_source += '\n'

        return self.inject_at_body_line(line_index, generated_source)
Пример #2
0
    def inject_assignment(self,
            name,
            value,
            extra_trailing_newline=False,
            convert_value=True,
            line_index=0):
        '''
        Injects a variable assignment, name = value, at line_index.

        Generated variables get one newline before. If you want to pass in raw
        source (instead of a value to be converted into source by a formatter),
        use convert_value=False.

        If extra_trailing_newline==True, one additional newline will be added
        after the variable declaration, so that there's a full blank line
        between it and the next line of source code.

        line_index is relative to the node body. If line_index is not provided,
        the variable will be inserted at node.body_start_line.
        '''

        indentation = self.node.inner_indentation
        if convert_value:
            value = format_value(
                value,
                starting_indentation=indentation)

        generated_source = '\n' + indentation + name + ' = ' + value + '\n'

        if extra_trailing_newline:
            generated_source += '\n'

        return self.inject_at_body_line(
            line_index,
            generated_source)
Пример #3
0
    def value(self, value):
        '''Generate a change that changes the value of the variable to value.
        Value must be an int, string, bool, list, tuple, or dict. Lists, tuples,
        and dicts, must ALSO only contain ints, strings, bools, lists, tuples,
        or dicts.

        To put it another way, .value() takes in a value, converts it to Python
        source, and then overwrites the variable body with that source.'''

        return self.overwrite_body(
            format_value(value,
                         starting_indentation=self.node.outer_indentation,
                         indent_first_line=False))
Пример #4
0
    def value(self, value):
        '''Generate a change that changes the value of the variable to value.
        Value must be an int, string, bool, list, tuple, or dict. Lists, tuples,
        and dicts, must ALSO only contain ints, strings, bools, lists, tuples,
        or dicts.

        To put it another way, .value() takes in a value, converts it to Python
        source, and then overwrites the variable body with that source.'''

        return self.overwrite_body(
            format_value(
                value,
                starting_indentation=self.node.outer_indentation,
                indent_first_line=False))
Пример #5
0
def test_nested():
    assert_equal(format_value((True, (3, 4))), EXPECTED_NESTED)
Пример #6
0
def test_tuple():
    assert_equal(format_value(('foo/bar/baz.quux', True)), EXPECTED_TUPLE)
Пример #7
0
def test_dict():
    assert_equal(format_value({'a':42, 'b':False}), EXPECTED_DICT)
Пример #8
0
def test_literal():
    assert_equal(format_value(3), '3')
    assert_equal(format_value(True), 'True')
    assert_equal(format_value('foo'), "'foo'")
Пример #9
0
def test_list():
    assert_equal(format_value([3, 4, 5]), EXPECTED_LIST)
Пример #10
0
def test_nested():
    assert_equal(format_value((True, (3, 4))), EXPECTED_NESTED)
Пример #11
0
def test_dict():
    assert_equal(format_value({'a': 42, 'b': False}), EXPECTED_DICT)
Пример #12
0
def test_tuple():
    assert_equal(format_value(('foo/bar/baz.quux', True)), EXPECTED_TUPLE)
Пример #13
0
def test_list():
    assert_equal(format_value([3, 4, 5]), EXPECTED_LIST)
Пример #14
0
def test_literal():
    assert_equal(format_value(3), '3')
    assert_equal(format_value(True), 'True')
    assert_equal(format_value('foo'), "'foo'")