Skip to content

vietjtnguyen/py-misc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Miscellaneous Python Tools

pycalc

Inspired by Crunch I wrote up a Python script to do something similar. It allows for inline, line-by-line (i.e. no function definitions and for loops) code execution with appended results. Really the only difference between passing the code to Python directly is that it will append the results of expressions to the end of the line as a comment.

It is written with the intention of execution in vim like Crunch, but will happily accept anything from standard input. The intended use is via vim's filter operation (also see this post), which is really really awesome.

Advantages:

  • Code execution in-session
  • Results appended, leaving original code intact for quick feedback cycle
  • Access to anything Python like NumPy, SciPy, SymPy, etc.

Disadvantages:

  • Does not execute multi-line code
  • Does not remember variables between executions

Example

Say we have the following block of code 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 in that second paragraph you can evaluate it with {V}!pycalc in vim (assuming pycalc is in your path and your Python is located at /usr/bin/python). The result 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

pymap

This script started as some ideas that got into my head while writing pycalc. Basically it lets you write small Python functions (really just lambdas) and apply them (i.e. map) to each input line.

This documentation is incomplete

It works by reading each line as it comes in from standard input. If a line does not start with a function marker then it is simply kept as an input line. If a line starts with a function marker then it is treated as code to be evaluated. Functions are evaluated as they're encountered with the results remaining persistent across functions. So you can apply one map and then apply a map on that result.

There are five operations you can perform. Each operation is determined by the first character.

  • !: applied per item
  • *: applied to list of items and then assigned to items
  • #: applied per enumerated item
  • -: used to filter items
  • =: applied to list of items and then appends result to end of function

After the function marker you simply define your lambda function. If you don't include the lambda then it'll prepend it for you.

At this point the best way to explain it is to look at examples.

Example

Example 1

Input:

[1, 2, 3]
[4, 5, 6, 7]
[8, 9, 10, 11, 12]
!x: sum(eval(x))

After evaluating all lines (such as ggVG!pymap in vim):

6
22
50
!x: sum(eval(x))

If you prefer keeping the input, you can do the following:

[1, 2, 3] # 6
[4, 5, 6, 7] # 22
[8, 9, 10, 11, 12] # 50
!x: '%s # %d' % (x, sum(eval(x)))

Example 2

Input:

John, [1, 2, 3]
Sarah, [4, 5, 6, 7]
Bob, [8, 9, 10, 11, 12]
!x: x.split(',')
!x: (x[0], eval(','.join(x[1:])))
!*name, items: 'Sum of %s\'s items: %d' % (name, sum(items))

After evaluating all lines (such as ggVG!pymap in vim):

Sum of John's items: 6
Sum of Sarah's items: 22
Sum of Bob's items: 50
!x: x.split(',')
!x: (x[0], eval(','.join(x[1:])))
!*name, items: 'Sum of %s\'s items: %d' % (name, sum(items))

Example 3

This example uses NumPy which you can enable by uncommenting the import lines at the top of the script (I should probably change those to try-import blocks).

[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
!x: ast.literal_eval(x)
*x: np.array(x)
=x: np.mean(x)
!x: 10*x
=x: np.mean(x)

After evaluating all lines (such as ggVG!pymap in vim):

[10 20 30]
[40 50 60]
[70 80 90]
!x: ast.literal_eval(x)
*x: np.array(x)
=x: np.mean(x)# = 5.0
!x: 10*x
=x: np.mean(x)# = 50.0

About

Python tools intended for use with vim filter.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages