Showing posts with label substitution. Show all posts
Showing posts with label substitution. Show all posts

Thursday, June 27, 2013

Using Vim to replace line-terminal ReST :: with line-separate ReST :: indicators.

You can transform this ...

def foo(x, y):
   r'''Explanation of function::

      >>> foo(7, 8)
      ...

   Return something.
   '''
   ...

... to this ...

def foo(x, y):
   r'''Explanation of function:

   ::

      >>> foo(7, 8)
      ...

   Return something.
   '''
   ...

... using Vim substitution.

The command ...

   :%s/\(\s*\)\(.*\w\)\(::\)$/\1\2:\r\r\1::/g

... will do the trick.

Changing Python list comprehensions to generator comprehensions

The list comprehension ...

    assert all([x % 2 == 0 for x in sequence])

... can be rewritten as the generator comprehension ...

    assert all(x % 2 == 0 for x in sequence)

... instead.

You can use the Vim substitution :%s/all(\[\(.*\)\])/all(\1)/g to change all([...]) to all(...) everywhere in a file.