Matlab/Octave syntax for 1D/2D arrays is more packed and doesn't require putting extra ',' and extra '[', ']' between dimensions. For this I wrote a parser that intercepts python interpreter and using numpy functionality parses Matlab's style arrays 1D and 2D into numpy.arrays.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import code, re
import numpy
class MatLikeListConsole(code.InteractiveConsole):
def push(self, line):
self.buffer.append(line)
source = "\n".join(self.buffer)
sqbr_re = re.compile(r'\[.*\]', re.S)
list_found = re.findall(sqbr_re, source)
if list_found:
try:
# [0] - assumes only one list at a time - limitation
arr = numpy.array(numpy.mat(list_found[0].strip('[]')))
except ValueError as e:
print(e)
more = False
else:
if arr.shape[0] == 1: # change single row matrix to 1D array
arr = arr[0]
str_np_arr = 'numpy.' + repr(arr) # back to string representation
more = self.runsource(re.sub(sqbr_re, str_np_arr, source), self.filename)
else:
# temporary change to string if only opening bracket present '['
more = self.runsource(re.sub(r'\[', r"'''", source, re.S), self.filename)
if not more:
self.resetbuffer()
return more
console = MatLikeListConsole(locals=locals())
console.interact()
# and now...
>>> import matlist # assuming above code in this module or copy-paste to console window
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(MatLikeListConsole)
>>> A = [1 2 3] # 1D array
>>> A
array([1, 2, 3])
>>>
>>> B = [1 2 3; # 2D matrix
... 4 5 6]
>>> B
array([[1, 2, 3],
[4, 5, 6]])
>>> Error = [1 2; 3 4;] # wrong format
Rows not the same size.
>>> # Wiiiiii!!!
|
Of course it's a hack for adult and responsible people. :) But is useful for quick checking and parameters tweaking in some cases.
I've created more robust version for IPython i Jupyter Notebook, because this is where I actually use it. https://github.com/Quinzel/pymat