Control Python wrapping for conditionals and list comprehensions?

Is there any way to set up Code Style to control how Reformat Code handles wrapping for conditionals and list comprehensions?  Consider the following two snippets:

        if option.attributes["name"].value == "additionalOptions" and "-includes" in option.attributes["value"].value] and 37 < 1:

        include_options_elems = [option for option in compiler_options_elem.getElementsByTagName("option") if option.attributes["name"].value == "additionalOptions" and "-includes" in option.attributes["value"].value]


When I reformat code, I'd like to get something like this:

        if option.attributes["name"].value == "additionalOptions"
              and "-includes" in option.attributes["value"].value]
              and 37 < 1:

        include_options_elems = [option for option in compiler_options_elem.getElementsByTagName("option")
                                                  if option.attributes["name"].value == "additionalOptions"
                                                       and "-includes" in option.attributes["value"].value]

or maybe even this, if the iteration or transform in the list comprehension is complex enough:

        include_options_elems = [option
                                                  for option in compiler_options_elem.getElementsByTagName("option")
                                                  if option.attributes["name"].value == "additionalOptions"
                                                       and "-includes" in option.attributes["value"].value]


But what I get is this:

        include_options_elems = [option for option in compiler_options_elem.getElementsByTagName("option") if
                                 option.attributes["name"].value == "additionalOptions" and "-includes" in
                                 option.attributes["value"].value]

        if option.attributes["name"].value == "additionalOptions" and "-includes" in option.attributes[
            "value"].value] and 37 < 1:


There's no rhyme or reason to the wrapping at all - it just wrapped blindly at my right margin, without any knowledge of the syntax. Other languages such as ActionScript have syntax-aware wrapping.  Is there a way to do this in Python?

Message was edited by: Rob Freundlich to add tags

1 comment
Comment actions Permalink

Hi Rob,

This blog post explains perfectly how code formatting can be archieved: http://harshcreationz.blogspot.nl/2014/11/how-to-make-you-own-code-formatting.html. Slightly less relevant, but also interesting: a blog post on Python list comprehensions.

1

Please sign in to leave a comment.