Any way to program custom syntax to python prompt? >> I want to editmatrices.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mosi

    Any way to program custom syntax to python prompt? >> I want to editmatrices.

    Python matrices are usually defined with numpy scipy array or similar.
    e.g.
    >>matrix1 = [[1, 2], [3, 4], [5, 6]]
    I would like to have easier way of defining matrices,
    for example:
    >>matrix = [1, 2; 3, 4; 5, 6]
    >>matrix =
    [ 1, 2;
    3, 4;
    5, 6;]

    Any ideas how could this be done? The ";" sign is reserved, the "[ ]"
    is used for lists.

    Also, how to program custom operations for this new "class?"
    matrix ???

    For example:
    >>matrix + 2
    [ 3, 4;
    5, 6;
    7, 8;]

    Possibly with operator overloading?
    I appreciate all your comments, directions, pointers.
    mosi
  • sturlamolden

    #2
    Re: Any way to program custom syntax to python prompt? >> I want toedit matrices.

    On 11 Des, 01:54, mosi <skawan...@gmai l.comwrote:
    Python matrices are usually defined with numpy scipy array or similar.
    e.g.>>matrix1 = [[1, 2], [3, 4], [5, 6]]
    That is a list of Python lists, not a NumPy array or matrix.

    For example:>>matri x + 2
    >
    [ 3, 4;
    5, 6;
    7, 8;]
    >
    Possibly with operator overloading?
    Go and get NumPy: www.scipy.org

    Then buy Travis Oliphant's book.




    Comment

    • Stargaming

      #3
      Re: Any way to program custom syntax to python prompt? &gt;&gt; I want toeditmatrices.

      On Mon, 10 Dec 2007 16:54:08 -0800, mosi wrote:
      Python matrices are usually defined with numpy scipy array or similar.
      e.g.
      >>>matrix1 = [[1, 2], [3, 4], [5, 6]]
      I would like to have easier way of defining matrices, for example:
      >>>matrix = [1, 2; 3, 4; 5, 6]
      >
      >>>matrix =
      [ 1, 2;
      3, 4;
      5, 6;]
      >
      Any ideas how could this be done? The ";" sign is reserved, the "[ ]" is
      used for lists.
      You could have some class `Matrix` with a constructor taking a string, as
      in ``Matrix("[1, 2; 3, 4; 5, 6]")``. A very naive parser could just strip
      trailing/leading brackets and all spaces, split on ';', split again on
      ',', done.
      Also, how to program custom operations for this new "class?" matrix ???
      >
      For example:
      >>>matrix + 2
      [ 3, 4;
      5, 6;
      7, 8;]
      >
      Possibly with operator overloading?
      Yes, by simply overloading __add__ et al. See http://docs.python.org/ref/
      specialnames.ht ml for details (certainly, "Emulating numeric types"
      should be interesting to you).
      I appreciate all your comments, directions, pointers. mosi
      Cheers,

      Comment

      • Ramsey Nasser

        #4
        Re: Any way to program custom syntax to python prompt? &gt;&gt; I want toedit matrices.

        On 11 Dec 2007 04:14:09 GMT, Stargaming <stargaming@gma il.comwrote:
        On Mon, 10 Dec 2007 16:54:08 -0800, mosi wrote:
        >
        Python matrices are usually defined with numpy scipy array or similar.
        e.g.
        >>matrix1 = [[1, 2], [3, 4], [5, 6]]
        I would like to have easier way of defining matrices, for example:
        >>matrix = [1, 2; 3, 4; 5, 6]
        >>matrix =
        [ 1, 2;
        3, 4;
        5, 6;]

        Any ideas how could this be done? The ";" sign is reserved, the "[ ]" is
        used for lists.
        I think mosi was looking to dynamically modify the syntax of Python
        itself, as opposed to working around the existing syntax. This
        includes things like messing with reserved symbols and keywords.

        The interpreter is compiled with these definitions built in, so to
        change them would conceivably require a recompile. If this is the
        case, then mosi may be out of luck. Then again, people have done some
        interesting dynamic modifications to the interpreter during runtime
        without recompiling. Psyco is the best example of this that comes to
        mind. Maybe someone more versed in Psyco-esque interpreter voodoo
        would be able to help.

        --
        nasser

        Comment

        Working...