Skip to content

Instantly share code, notes, and snippets.

@gcr
Created September 23, 2016 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcr/eeea119a7bdb0a4975df5804262508a3 to your computer and use it in GitHub Desktop.
Save gcr/eeea119a7bdb0a4975df5804262508a3 to your computer and use it in GitHub Desktop.
Right-aligned python
import re
python_regex = re.compile(r"^(.*?)(\s*)$")
from IPython.core.magic import register_cell_magic, cell_magic, magics_class, Magics
@magics_class
class RightAlignMagics(Magics):
@cell_magic
def right_align(self, line, cell):
cell = "\n".join([
python_regex.sub(r"\2\1", l)
for l in cell.split("\n")
])
self.shell.run_cell(cell)
get_ipython().register_magics(RightAlignMagics)
%%right_align
for x in xrange(50):
out = []
if x % 7 == 0:
out.append("Fizz")
if x % 5 == 0:
out.append("Buzz")
if not out:
out = str(x)
print out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment