What is YAPF?

A formatter for Python files, developed by Google team
30 October 2017   4089

What is YAPF?

Most of the current formatters for Python --- e.g., autopep8, and pep8ify --- are made to remove lint errors from code. This has some obvious limitations. For instance, code that conforms to the PEP 8 guidelines may not be reformatted. But it doesn't mean that the code looks good.

YAPF takes a different approach. It's based off of 'clang-format', developed by Daniel Jasper. In essence, the algorithm takes the code and reformats it to the best formatting that conforms to the style guide, even if the original code didn't violate the style guide. The idea is also similar to the 'gofmt' tool for the Go programming language: end all holy wars about formatting - if the whole codebase of a project is simply piped through YAPF whenever modifications are made, the style remains consistent throughout the project and there's no point arguing about style in every code review.

The ultimate goal is that the code YAPF produces is as good as the code that a programmer would write if they were following the style guide. It takes away some of the drudgery of maintaining your code.

Code examples

YAPF takes this code:

x = {  'a':37,'b':42,

'c':927}

y = 'hello ''world'
z = 'hello '+'world'
a = 'hello {}'.format('world')
class foo  (     object  ):
  def f    (self   ):
    return       37*-+2
  def g(self, x,y=42):
      return y
def f  (   a ) :
  return      37+-+a[42-x :  y**3]

and reformat it into:

x = {'a': 37, 'b': 42, 'c': 927}

y = 'hello ' 'world'
z = 'hello ' + 'world'
a = 'hello {}'.format('world')


class foo(object):
    def f(self):
        return 37 * -+2

    def g(self, x, y=42):
        return y


def f(a):
    return 37 + -+a[42 - x:y**3]

See GitHub for more information. 

Python News Digest 8 - 14.02

Learn about the best Python tools, why sys.getsizeof is not what you need, how to call await on multiple functions and more
14 February 2020   172

Greetings! I hope your week went great! Here's new Python news digest.

Learn how parallelism can slow down your Python code, implementing interface in Python, how to check if a file is a valid image with Python and other intersting things awaits for you in this digest.

Articles

  • Understanding Best Practice Python Tooling by Comparing Popular Project Templates

Author checked and compare the most popular Python tools in this big article

  • The Parallelism Blues: when faster code is slower

Learn when, why and how parallelism can slow down your Python app

  • sys.getsizeof is not what you want

Learn why sys.getsizeof counts not all the bytes, and even wrong bytes

Guides

  • Implementing an Interface in Python

Tutorial for beginners on how to use a Python interface; understand why interfaces are so useful and learn how to implement formal and informal interfaces in Python

  • Python asyncio and await'ing multiple functions

In this tiny tutorial, you'll learn how to call await on multiple functions in Python using the asyncio package

  • How to Check if a File is a Valid Image with Python

Really small, but a useful tutorial that we will show you how to check if a certain file is a valied image using Python

  • Understand Group by in Django with SQL

Learn and understand what GROUP BY in Django ORM is by comparing QuerySets and SQL

Updates

  • virtualenv

A virtual environment builder for Python