Simplifying many-indents-code I have the following code, written in Python:

if id:
    fields = r.table(cls._tablename)
              .get(id)
              .run(cls.connection())
else:
    try:
        fields = list(r.table(cls._tablename)
                       .filter(**kwargs)
                       .limit(1)
                       .run(cls.connection()))[0]
    except IndexError:
        fields = None


The code acts as a wrapper for getting a document from a RethinkDB datastore. I find all those indents unaesthetic, and I cannot seem to find a way to wrap the lines to look nicer.

So, is there a way I can rewrite (or maybe just rewrap) this code to look better and provide the same functionality?
Your indentations strike me as reasonable.  In any case, in functions you can often use return statements to help avoid indentation ...

def get_fields(id):
    if id:
        fields = ...
        return fields

    try:
        fields = ...
    except:
        fields = ...

    return fields