Python autopep8.fix_code() Examples

The following are 20 code examples of autopep8.fix_code(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module autopep8 , or try the search function .
Example #1
Source File: vfp2py.py    From vfp2py with MIT License 6 votes vote down vote up
def prg2py_after_preproc(data, parser_start, input_filename):
    input_stream = antlr4.InputStream(data)
    lexer = VisualFoxpro9Lexer(input_stream)
    stream = antlr4.CommonTokenStream(lexer)
    parser = VisualFoxpro9Parser(stream)
    tree = run_parser(stream, parser, parser_start)
    TreeCleanVisitor().visit(tree)
    output_tree = PythonConvertVisitor(input_filename).visit(tree)
    if not isinstance(output_tree, list):
        return output_tree
    output = add_indents(output_tree, 0)
    options = autopep8.parse_args(['--max-line-length', '100000', '-'])
    output = autopep8.fix_code(output, options)
    tokens = list(tokenize.generate_tokens(io.StringIO(output).readline))
    for i, token in enumerate(tokens):
        token = list(token)
        if token[0] == tokenize.STRING and token[1].startswith('u'):
            token[1] = token[1][1:]
        tokens[i] = tuple(token)
    return tokenize.untokenize(tokens) 
Example #2
Source File: generate_code.py    From kale with Apache License 2.0 6 votes vote down vote up
def generate_pipeline(template, nb_graph, step_names, lightweight_components,
                      metadata):
    """Use the pipeline template to generate Python code."""
    # All the Pipeline steps that do not have children
    leaf_steps = graph_utils.get_leaf_nodes(nb_graph)

    # create a dict with step names and their parameters
    all_step_parameters = {step: sorted(nb_graph.nodes(data=True)[step]
                                        .get('parameters', {}).keys())
                           for step in step_names}

    pipeline_code = template.render(
        nb_graph=nb_graph,
        lightweight_components=lightweight_components,
        step_names=step_names,
        step_prevs=pipeline_dependencies_tasks(nb_graph),
        leaf_steps=leaf_steps,
        all_step_parameters=all_step_parameters,
        **metadata
    )
    # fix code style using pep8 guidelines
    return autopep8.fix_code(pipeline_code) 
Example #3
Source File: core.py    From poetry-setup with Apache License 2.0 6 votes vote down vote up
def get_setup(self):
        # render template
        with self.setup_path.open(encoding='utf-8') as f:
            document = f.read()
        template = Environment().from_string(document)
        document = template.render(
            package=self.package,
            format_vcs=self._format_vcs,
        )

        # format by yapf
        style = CreateGoogleStyle()
        document, _changed = FormatCode(document, style_config=style)
        # remove empty strings
        while '\n\n' in document:
            document = document.replace('\n\n', '\n')
        # format by autopep8
        document = fix_code(document)
        return document 
Example #4
Source File: PEP8NotebookBear.py    From coala-bears with GNU Affero General Public License v3.0 6 votes vote down vote up
def autopep8_fix_code_cell(source, options=None, apply_config=None):
    """
    Applies autopep8.fix_code and takes care of newline characters.

    autopep8.fix_code automatically adds a final newline at the end,
    e.g. ``autopep8.fix_code('a=1')`` yields 'a = 1\\n'.
    Note that this is not related to the 'W292' flag, i.e.
    ``autopep8.fix_code('a=1', options=dict(ignore=('W292',)))`` gives
    the same result.
    For notebook code cells, this behaviour does not make sense, hence
    newline is removed if ``source`` does not end with one.
    """
    source_corrected = autopep8.fix_code(source,
                                         apply_config=apply_config,
                                         options=options)

    if not source.endswith('\n'):
        return source_corrected[:-1]

    return source_corrected 
Example #5
Source File: mainwindow.py    From Turing with MIT License 5 votes vote down vote up
def load_python_code():
    import autopep8
    py_code = autopep8.fix_code("\n".join(AppState.algo.python()))
    GuiState.code_editor.setPlainText(py_code.replace("\t", "    "), "", "") 
Example #6
Source File: radius.py    From pep8radius with MIT License 5 votes vote down vote up
def fix_line_range(source_code, start, end, options):
    """Apply autopep8 (and docformatter) between the lines start and end of
    source."""
    # TODO confirm behaviour outside range (indexing starts at 1)
    start = max(start, 1)

    options.line_range = [start, end]
    from autopep8 import fix_code
    fixed = fix_code(source_code, options)

    try:
        if options.docformatter:
            from docformatter import format_code
            fixed = format_code(
                fixed,
                summary_wrap_length=options.max_line_length - 1,
                description_wrap_length=(options.max_line_length
                                         - 2 * options.indent_size),
                pre_summary_newline=options.pre_summary_newline,
                post_description_blank=options.post_description_blank,
                force_wrap=options.force_wrap,
                line_range=[start, end])
    except AttributeError:  # e.g. using autopep8.parse_args, pragma: no cover
        pass

    return fixed 
Example #7
Source File: radius.py    From pep8radius with MIT License 5 votes vote down vote up
def fix_code(source_code, line_ranges, options=None, verbose=0):
    '''Apply autopep8 over the line_ranges, returns the corrected code.

    Note: though this is not checked for line_ranges should not overlap.

    Example
    -------
    >>> code = "def f( x ):\\n  if  True:\\n    return 2*x"
    >>> print(fix_code(code, [(1, 1), (3, 3)]))
    def f(x):
      if  True:
          return 2 * x

    '''
    if options is None:
        from pep8radius.main import parse_args
        options = parse_args()

    if getattr(options, "yapf", False):
        from yapf.yapflib.yapf_api import FormatCode
        result = FormatCode(source_code, style_config=options.style, lines=line_ranges)
        # yapf<0.3 returns diff as str, >=0.3 returns a tuple of (diff, changed)
        return result[0] if isinstance(result, tuple) else result

    line_ranges = reversed(line_ranges)
    # Apply line fixes "up" the file (i.e. in reverse) so that
    # fixes do not affect changes we're yet to make.
    partial = source_code
    for start, end in line_ranges:
        partial = fix_line_range(partial, start, end, options)
        _maybe_print('.', end='', max_=1, verbose=verbose)
    _maybe_print('', max_=1, verbose=verbose)
    fixed = partial
    return fixed 
Example #8
Source File: radius.py    From pep8radius with MIT License 5 votes vote down vote up
def fix_file(file_name, line_ranges, options=None, in_place=False,
             diff=False, verbose=0, cwd=None):
    """Calls fix_code on the source code from the passed in file over the given
    line_ranges.

    - If diff then this returns the udiff for the changes, otherwise
    returns the fixed code.
    - If in_place the changes are written to the file.

    """
    import codecs
    from os import getcwd
    from pep8radius.diff import get_diff
    from pep8radius.shell import from_dir

    if cwd is None:
        cwd = getcwd()

    with from_dir(cwd):
        try:
            with codecs.open(file_name, 'r', encoding='utf-8') as f:
                original = f.read()
        except IOError:
            # Most likely the file has been removed.
            # Note: it would be nice if we could raise here, specifically
            # for the case of passing in a diff when in the wrong directory.
            return ''

    fixed = fix_code(original, line_ranges, options, verbose=verbose)

    if in_place:
        with from_dir(cwd):
            with codecs.open(file_name, 'w', encoding='utf-8') as f:
                f.write(fixed)

    return get_diff(original, fixed, file_name) if diff else fixed 
Example #9
Source File: django_test_client.py    From django-silk with MIT License 5 votes vote down vote up
def gen(path,
        method=None,
        query_params=None,
        data=None,
        content_type=None):
    # generates python code representing a call via django client.
    # useful for use in testing
    method = method.lower()
    t = jinja2.Template(template)
    if method == 'get':
        r = t.render(path=path,
                     data=query_params,
                     lower_case_method=method,
                     content_type=content_type)
    else:
        if query_params:
            query_params = _encode_query_params(query_params)
            path += query_params
        if is_str_typ(data):
            data = "'%s'" % data
        r = t.render(path=path,
                     data=data,
                     lower_case_method=method,
                     query_params=query_params,
                     content_type=content_type)
    return autopep8.fix_code(
        r, options=autopep8.parse_args(['--aggressive', ''])
    ) 
Example #10
Source File: django_test_client.py    From django-silk with MIT License 5 votes vote down vote up
def gen(path,
        method=None,
        query_params=None,
        data=None,
        content_type=None):
    # generates python code representing a call via django client.
    # useful for use in testing
    method = method.lower()
    t = jinja2.Template(template)
    if method == 'get':
        r = t.render(path=path,
                     data=query_params,
                     lower_case_method=method,
                     content_type=content_type)
    else:
        if query_params:
            query_params = _encode_query_params(query_params)
            path += query_params
        if is_str_typ(data):
            data = "'%s'" % data
        r = t.render(path=path,
                     data=data,
                     lower_case_method=method,
                     query_params=query_params,
                     content_type=content_type)
    return autopep8.fix_code(
        r, options=autopep8.parse_args(['--aggressive', ''])
    ) 
Example #11
Source File: formatters.py    From ciocheck with MIT License 5 votes vote down vote up
def format_string(cls, old_contents):
        """Format file for use with task queue."""
        config_options = cls.make_config_dictionary()
        config_options = {}
        new_contents = autopep8.fix_code(old_contents, options=config_options)
        return old_contents, new_contents, 'utf-8' 
Example #12
Source File: workflow_template.py    From girlfriend with MIT License 5 votes vote down vote up
def do_gen(self, line):
        if os.path.exists(self.file_name):
            prompt = u"确定要生成代码?之前的代码文件 '{}' 将被覆盖(y/n)".format(
                self.file_name).encode("utf-8")
            if raw_input(prompt) != 'y':
                return
        with open(self.file_name, "w") as f:
            f.write(autopep8.fix_code("".join(self._generate_workflow_code()))) 
Example #13
Source File: workflow_template.py    From girlfriend with MIT License 5 votes vote down vote up
def do_show(self, line):
        """Show me the code!
        """
        code = autopep8.fix_code("".join(self._generate_workflow_code()))
        if self.options.no_highlight:
            print code
        else:
            print highlight(code, PythonLexer(), TerminalFormatter()) 
Example #14
Source File: generate_code.py    From kale with Apache License 2.0 5 votes vote down vote up
def generate_lightweight_component(template, step_name, step_data, nb_path,
                                   metadata):
    """Use the function template to generate Python code."""
    step_source_raw = step_data.get('source', [])

    def _encode_source(s):
        # Encode line by line a multiline string
        return "\n".join([line.encode("unicode_escape").decode("utf-8")
                          for line in s.splitlines()])

    # Since the code will be wrapped in triple quotes inside the template,
    # we need to escape triple quotes as they will not be escaped by
    # encode("unicode_escape").
    step_source = [re.sub(r"'''", "\\'\\'\\'", _encode_source(s))
                   for s in step_source_raw]

    step_marshal_in = step_data.get('ins', [])
    step_marshal_out = step_data.get('outs', [])
    step_parameters = get_args(step_data.get('parameters', {}))

    fn_code = template.render(
        step_name=step_name,
        function_body=step_source,
        in_variables=step_marshal_in,
        out_variables=step_marshal_out,
        parameters=step_parameters,
        nb_path=nb_path,
        # step_parameters overrides the parameters fields of metadata
        **{**metadata, **step_parameters}
    )
    # fix code style using pep8 guidelines
    return autopep8.fix_code(fn_code) 
Example #15
Source File: io.py    From pandera with MIT License 5 votes vote down vote up
def _format_script(script):
    try:
        import black
        formatter = partial(
            black.format_str, mode=black.FileMode(line_length=80)
        )
    except ImportError:
        # use autopep8 for python3.5
        import autopep8
        formatter = partial(
            autopep8.fix_code, options={"aggressive": 1}
        )

    return formatter(script) 
Example #16
Source File: formatters.py    From jupyterlab_code_formatter with MIT License 5 votes vote down vote up
def format_code(self, code: str, notebook: bool, **options) -> str:
        from autopep8 import fix_code

        return fix_code(code, options=options) 
Example #17
Source File: processor_kfp.py    From elyra with Apache License 2.0 4 votes vote down vote up
def export(self, pipeline, pipeline_export_format, pipeline_export_path, overwrite):
        if pipeline_export_format not in ["yaml", "py"]:
            raise ValueError("Pipeline export format {} not recognized.".format(pipeline_export_format))

        pipeline_name = pipeline.name

        # Since pipeline_export_path may be relative to the notebook directory, ensure
        # we're using its absolute form.
        absolute_pipeline_export_path = self.get_absolute_path(pipeline_export_path)

        runtime_configuration = self._get_runtime_configuration(pipeline.runtime_config)
        api_endpoint = runtime_configuration.metadata['api_endpoint']

        if os.path.exists(absolute_pipeline_export_path) and not overwrite:
            raise ValueError("File " + absolute_pipeline_export_path + " already exists.")

        self.log.info('Creating pipeline definition as a .' + pipeline_export_format + ' file')
        if pipeline_export_format != "py":
            try:
                pipeline_function = lambda: self._cc_pipeline(pipeline, pipeline_name)  # nopep8
                kfp.compiler.Compiler().compile(pipeline_function, absolute_pipeline_export_path)
            except Exception as ex:
                raise RuntimeError('Error compiling pipeline {} for export at {}'.
                                   format(pipeline_name, absolute_pipeline_export_path), str(ex)) from ex
        else:
            # Load template from installed elyra package
            loader = PackageLoader('elyra', 'templates')
            template_env = Environment(loader=loader)

            template = template_env.get_template('kfp_template.jinja2')

            defined_pipeline = self._cc_pipeline(pipeline, pipeline_name)

            for key, operation in defined_pipeline.items():
                self.log.debug("component :\n "
                               "container op name : %s \n "
                               "inputs : %s \n "
                               "outputs : %s \n ",
                               operation.name,
                               operation.inputs,
                               operation.outputs)

            python_output = template.render(operations_list=defined_pipeline,
                                            pipeline_name=pipeline_name,
                                            api_endpoint=api_endpoint,
                                            pipeline_description="Elyra Pipeline")

            # Write to python file and fix formatting
            with open(absolute_pipeline_export_path, "w") as fh:
                fh.write(autopep8.fix_code(python_output))

        return pipeline_export_path  # Return the input value, not its absolute form 
Example #18
Source File: utils_automl.py    From automl-gs with MIT License 4 votes vote down vote up
def render_model(params, model_name, framework, env, problem_type, 
                 target_metric, target_field, train_folder, fields,
                 split, num_epochs, gpu, tpu_address,
                 metrics_path=resource_filename(__name__, "metrics.yml")):
    """Renders and saves the files (model.py, pipeline.py, requirements.txt) for the given hyperparameters.
    """

    files = ['model.py', 'pipeline.py', 'requirements.txt']

    type_map = {
    'numeric': 'float64',
    'categorical': 'str',
    'datetime': 'str',
    'text': 'str'
    }

    load_fields = {field[1]: type_map[field[2]] for field in fields}
    text_fields = [field for field in fields if field[2] == 'text']
    nontarget_fields = [field for field in fields if field[1] != target_field]
    target_field, target_field_raw = [(field[0], field[1]) for field in fields if field[1] == target_field][0]
    has_text_input = 'text' in [field[2] for field in fields]
    text_framework = 'tensorflow' if framework == 'tensorflow' else 'sklearn'

    with open(metrics_path) as f:
        metrics = yaml.safe_load(f)[problem_type]

    for file in files:
        script = env.get_template('scripts/' + file.replace('.py', '')).render(
            params=params,
            model_name=model_name,
            framework=framework,
            problem_type=problem_type,
            target_metric=target_metric,
            target_field=target_field,
            fields=fields,
            split=split,
            num_epochs=num_epochs,
            load_fields=load_fields,
            text_fields=text_fields,
            nontarget_fields=nontarget_fields,
            target_field_raw=target_field_raw,
            has_text_input=has_text_input,
            metrics=metrics,
            text_framework=text_framework,
            gpu=gpu,
            tpu_address=tpu_address)

        script = fix_code(script)

        with open(train_folder + "/" + file, 'w', encoding='utf8') as outfile:
            outfile.write(script) 
Example #19
Source File: harreplay.py    From harreplay with MIT License 4 votes vote down vote up
def fobj_to_pystring(fobj):
    result_lines = [
        'import requests',
    ]

    for entry in json.load(fobj)['log']['entries']:
        request_el = entry['request']
        method = request_el['method']
        url = request_el['url']
        data = None
        if 'postData' in request_el:
            if 'text' in request_el['postData']:
                data = request_el['postData']['text']
            elif 'params' in request_el['postData']:
                raise NotImplementedError(
                    "Haven't seen 'params' in the wild yet.")
        data = data or None
        headers = dict(
            (d['name'], d['value']) for d in request_el['headers'])
        call_code_format = (
            'response = requests.{method}({args}{sep})'
        )
        arg_lines = [
            '{sep}    {url},'.format(
                sep = SEP,
                url = repr(url),
            ),
            '{sep}    headers={headers},'.format(
                sep = SEP,
                headers = repr(headers)
            ) if headers else None,
            '{sep}    data={data},'.format(
                sep = SEP,
                data = repr(data)
            ) if data else None,
        ]
        call_code = call_code_format.format(
            sep = SEP,
            method = method.lower(),
            args = SEP.join(l for l in arg_lines if l),
        )
        result_lines.append(call_code)

    result = SEP.join(result_lines)

    try:
        import autopep8
        result = autopep8.fix_code(
            result, options={'aggressive': 1})
    except ImportError as e:
        pass

    return result 
Example #20
Source File: PEP8Bear.py    From coala-bears with GNU Affero General Public License v3.0 4 votes vote down vote up
def run(self, filename, file,
            max_line_length: int = 79,
            indent_size: int = SpacingHelper.DEFAULT_TAB_WIDTH,
            pep_ignore: typed_list(str) = (),
            pep_select: typed_list(str) = (),
            local_pep8_config: bool = False,
            ):
        """
        Detects and fixes PEP8 incompliant code. This bear will not change
        functionality of the code in any way.

        :param max_line_length:
            Maximum number of characters for a line.
            When set to 0 allows infinite line length.
        :param indent_size:
            Number of spaces per indentation level.
        :param pep_ignore:
            A list of errors/warnings to ignore.
        :param pep_select:
            A list of errors/warnings to exclusively apply.
        :param local_pep8_config:
            Set to true if autopep8 should use a config file as if run normally
            from this directory.
        """
        if not max_line_length:
            max_line_length = sys.maxsize

        options = {'ignore': pep_ignore,
                   'select': pep_select,
                   'max_line_length': max_line_length,
                   'indent_size': indent_size}

        corrected = autopep8.fix_code(''.join(file),
                                      apply_config=local_pep8_config,
                                      options=options).splitlines(True)

        diffs = Diff.from_string_arrays(file, corrected).split_diff()

        for diff in diffs:
            yield Result(self,
                         'The code does not comply to PEP8.',
                         affected_code=(diff.range(filename),),
                         diffs={filename: diff})