Search code examples
pythonautopep8

How can I convert multi-line Python code to a single line?


I have some code like:

imgs, original_img, original_size = load_img(INPUT_IMG_FILE,
                                             input_size=NETWORK_INPUT_SIZE,
                                             preprocess_fn=resnet.preprocess_input)

It has the extra spaces and indentation. I want to somehow convert it to one line.

I tried autopep8.fix_code(code), but that keeps it as is.

I'd also like this code to be on one line:

      install_requires=['numpy>=1.9.1',
                        'scipy>=0.14',
                        'six>=1.9.0',
                        'pyyaml',
                        'h5py',
                        'keras_applications>=1.0.6',
                        'keras_preprocessing>=1.0.5'],

Solution

  • You can use the max_line_length option:

    autopep8.fix_code(code, options={'max_line_length': 150})
    

    Another option is to ignore warnings about line length:

    autopep8.fix_code(code, options={'ignore': ['E501']})