コード例 #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
ファイル: change.py プロジェクト: ForSpareParts/code_monkey
    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
ファイル: change.py プロジェクト: ForSpareParts/code_monkey
    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'")