Skip to content

Instantly share code, notes, and snippets.

@vietjtnguyen
Created November 12, 2013 09:07
Show Gist options
  • Save vietjtnguyen/7427816 to your computer and use it in GitHub Desktop.
Save vietjtnguyen/7427816 to your computer and use it in GitHub Desktop.
Python script to do in place calculations while in vim. Inspired by https://github.com/arecarn/crunch.
#!/usr/bin/python
#import numpy as np
#import pandas as pd
#import scipy as sp
#import scipy.ndimage as nd
#import scipy.signal as sg
#import itertools, pickle, random, os
from math import *
import sys
for line in sys.stdin.readlines():
line = line[:line.rfind('#')]
try:
code_object = compile(line, '', 'eval')
output = '# = {:}'.format(str(eval(code_object)))
except SyntaxError as e:
code_object = compile(line, '', 'exec')
exec code_object
output = ''
print('{:}{:}'.format(str(line).strip(), output))
@vietjtnguyen
Copy link
Author

Python script to do in place calculations while in vim. Inspired by https://github.com/arecarn/crunch.

For example, say you have this block of text in vim:

paragraph one

x = 23.21
y = 53.32
norm = lambda x, y: sqrt(x*x+y*y)
norm(x, y)
atan2(y, x)

paragraph three

If you have your cursor somewhere in the second paragraph you can evaluate it with {V}:!./pycalc.py (assuming pycalc.py is in the working directory). What you get after is the following:

paragraph one

x = 23.21
y = 53.32
norm = lambda x, y: sqrt(x*x+y*y)
norm(x, y)# = 58.1526138707
atan2(y, x)# = 1.1602370238

paragraph three

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment