More like a shell command.

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

    More like a shell command.

    Is there anyway I can extend python to accept a command
    which looks more like shell syntax than a function call.

    I want to be able to do this:

    if blah :
    MyCommand Arg1 Arg2

    as opposed to this:

    if blah :
    MyCommand(Arg1, Arg2)

    or this:

    if blah :
    x("MyCommand Arg1 Arg2")

    Of source, I would like to do this by writing a module (or through
    some other run-time hook) as opposed to editing the Python source
    code.

    Thanks in advance
    (unless you are just a Python nut who is flaming to tell me that I
    should not want this. :-) )

    Bill
  • Mike Driscoll

    #2
    Re: More like a shell command.

    On Aug 6, 9:38 am, Bill <galaxyblu...@g mail.comwrote:
    Is there anyway I can extend python to accept a command
    which looks more like shell syntax than a function call.
    >
    I want to be able to do this:
    >
        if blah :
            MyCommand  Arg1  Arg2
    >
    as opposed to this:
    >
        if blah :
            MyCommand(Arg1, Arg2)
    >
    or this:
    >
        if blah :
            x("MyCommand  Arg1  Arg2")
    >
    Of source, I would like to do this by writing a module (or through
    some other run-time hook) as opposed to editing the Python source
    code.
    >
    Thanks in advance
    (unless you are just a Python nut who is flaming to tell me that I
    should not want this.  :-)  )
    >
    Bill
    I'm not aware of any way to do this without modifying the original
    source in some fundamental way. You may be able to use some
    metaprogramming techniques, like decorators, to achieve this, although
    I'm not thinking of anything clever at the moment.

    -------------------
    Mike Driscoll

    Blog: http://blog.pythonlibrary.org
    Python Extension Building Network: http://www.pythonlibrary.org

    Comment

    • Miki

      #3
      Re: More like a shell command.

      Hello,
      Is there anyway I can extend python to accept a command
      which looks more like shell syntax than a function call.
      >
      I want to be able to do this:
      >
          if blah :
              MyCommand  Arg1  Arg2
      >
      as opposed to this:
      >
          if blah :
              MyCommand(Arg1, Arg2)
      >
      or this:
      >
          if blah :
              x("MyCommand  Arg1  Arg2")
      >
      Of source, I would like to do this by writing a module (or through
      some other run-time hook) as opposed to editing the Python source
      code.
      You might want to have a look at ipython, they do something like that
      but for the command interpreter and not the compiler.

      The other option will be to write a compiler from your syntax to valid
      Python syntax. However this my not be simple since you need to know
      the context. e.g. is f("a b c") calling f in the string "a b c" or
      f(a, b, c)?

      HTH,
      --
      Miki <miki.tebeka@gm ail.com>
      If it won't be simple, it simply won't be. [Hire me, source code]

      Comment

      • Thor

        #4
        Re: More like a shell command.

        Maybe this module would work fine:

        Source code: Lib/cmd.py The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes tha...


        --
        Angel

        Comment

        • castironpi

          #5
          Re: More like a shell command.

          On Aug 6, 9:38 am, Bill <galaxyblu...@g mail.comwrote:
          Is there anyway I can extend python to accept a command
          which looks more like shell syntax than a function call.
          >
          I want to be able to do this:
          >
              if blah :
                  MyCommand  Arg1  Arg2
          >
          as opposed to this:
          >
              if blah :
                  MyCommand(Arg1, Arg2)
          >
          or this:
          >
              if blah :
                  x("MyCommand  Arg1  Arg2")
          >
          Of source, I would like to do this by writing a module (or through
          some other run-time hook) as opposed to editing the Python source
          code.
          >
          Thanks in advance
          (unless you are just a Python nut who is flaming to tell me that I
          should not want this.  :-)  )
          >
          Bill
          Bill,

          You'll need to decide on a grammar you want to use beforehand. What
          do you do with:
          >>if f 0 and g
          ?

          Does it translate to:

          if f( 0 ) and g( )

          or

          if f( 0 and g )

          ? If every line starts with its one and only function, and the
          parameters exhaust the rest of the line, and you don't allow nested
          expressions, you can run a preprocessor, then call Python. (Those
          three conditions are limiting.) Here's the steps for each line.

          1. strip leading tabs
          2. compile( line, '<none>', 'exec' ) to check for SyntaxError
          3. if found:
          3a. replace first space with '('
          3b. replace remaining space with ','
          3c. add trailing ')'
          3d. replace leading tabs

          Then just run:

          python preprocessor.py -filename-
          python -filename-.pppy

          Have a look at code.py and codeop.py as well and the
          InteractiveInte rpreter class.

          Comment

          Working...