cmd all commands method?

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

    cmd all commands method?

    Hi all,

    if i want to treat every cmdloop prompt entry as a potential command
    then i need to overwrite the default() method ?

    What i want to achieve is to be able to support global variable
    creation for example;

    res = sum 1 2

    this would create a variable res with the result of the method
    do_sum() ?

    then would i be able to run;

    sum a 5

    this would return 8 or an error saying that res is not defined


    Cheers

  • placid

    #2
    Re: cmd all commands method?


    placid wrote:
    Hi all,
    >
    if i want to treat every cmdloop prompt entry as a potential command
    then i need to overwrite the default() method ?
    >
    What i want to achieve is to be able to support global variable
    creation for example;
    >
    res = sum 1 2
    >
    this would create a variable res with the result of the method
    do_sum() ?
    >
    then would i be able to run;
    >
    sum a 5
    this should have been,

    sum res 5
    >
    this would return 8 or an error saying that res is not defined
    >
    >
    Cheers

    Comment

    • Bjoern Schliessmann

      #3
      Re: cmd all commands method?

      placid wrote:
      if i want to treat every cmdloop prompt entry as a potential
      command then i need to overwrite the default() method ?
      Excuse me, what's a cmdloop prompt? What's the "default() method"?
      What i want to achieve is to be able to support global variable
      creation for example;
      >
      res = sum 1 2
      >
      this would create a variable res with the result of the method
      do_sum() ?
      >
      then would i be able to run;
      >
      sum a 5
      >
      this would return 8 or an error saying that res is not defined
      Are you sure you're talking about Python here?

      Regards,


      Björn

      --
      BOFH excuse #7:

      poor power conditioning

      Comment

      • Michele Simionato

        #4
        Re: cmd all commands method?

        On Feb 17, 11:44 pm, Bjoern Schliessmann <usenet-
        mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
        placid wrote:
        if i want to treat every cmdloop prompt entry as a potential
        command then i need to overwrite the default() method ?
        >
        Excuse me, what's a cmdloop prompt? What's the "default() method"?
        >
        What i want to achieve is to be able to support global variable
        creation for example;
        >
        res = sum 1 2
        >
        this would create a variable res with the result of the method
        do_sum() ?
        >
        then would i be able to run;
        >
        sum a 5
        >
        this would return 8 or an error saying that res is not defined
        >
        Are you sure you're talking about Python here?

        Yes, he is talking about the cmd module: http://docs.python.org/dev/lib/Cmd-objects.html.
        However that module was never intended as a real interpreter, so
        defining variables
        as the OP wants would require some work.

        Michele Simionato

        Comment

        • placid

          #5
          Re: cmd all commands method?

          On Feb 18, 7:17 pm, "Michele Simionato" <michele.simion ...@gmail.com>
          wrote:
          On Feb 17, 11:44 pm, Bjoern Schliessmann <usenet-
          >
          >
          >
          mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
          placid wrote:
          if i want to treat every cmdloop prompt entry as a potential
          command then i need to overwrite the default() method ?
          >
          Excuse me, what's a cmdloop prompt? What's the "default() method"?
          >
          What i want to achieve is to be able to support global variable
          creation for example;
          >
          res = sum 1 2
          >
          this would create a variable res with the result of the method
          do_sum() ?
          >
          then would i be able to run;
          >
          sum a 5
          >
          this would return 8 or an error saying that res is not defined
          >
          Are you sure you're talking about Python here?
          >
          Yes, he is talking about the cmd module:http://docs.python.org/dev/lib/Cmd-objects.html.
          However that module was never intended as a real interpreter, so
          defining variables
          as the OP wants would require some work.
          >
          Michele Simionato
          How much work does it require ?

          Comment

          • Peter Otten

            #6
            Re: cmd all commands method?

            placid wrote:
            On Feb 18, 7:17 pm, "Michele Simionato" <michele.simion ...@gmail.com>
            wrote:
            >On Feb 17, 11:44 pm, Bjoern Schliessmann <usenet-
            >>
            >>
            >>
            >mail-0306.20.chr0n.. .@spamgourmet.c omwrote:
            placid wrote:
            if i want to treat every cmdloop prompt entry as a potential
            command then i need to overwrite the default() method ?
            >>
            Excuse me, what's a cmdloop prompt? What's the "default() method"?
            >>
            What i want to achieve is to be able to support global variable
            creation for example;
            >>
            res = sum 1 2
            >>
            this would create a variable res with the result of the method
            do_sum() ?
            >>
            then would i be able to run;
            >>
            sum a 5
            >>
            this would return 8 or an error saying that res is not defined
            >>
            Are you sure you're talking about Python here?
            >>
            >Yes, he is talking about the cmd
            >module:http://docs.python.org/dev/lib/Cmd-objects.html. However that
            >module was never intended as a real interpreter, so defining variables
            >as the OP wants would require some work.
            >>
            > Michele Simionato
            >
            How much work does it require ?
            Too much. However, here's how far I got:

            import cmd
            import shlex

            DEFAULT_TARGET = "_"

            def number(arg):
            for convert in int, float:
            try:
            return convert(arg)
            except ValueError:
            pass
            return arg

            class MyCmd(cmd.Cmd):
            def __init__(self, *args, **kw):
            cmd.Cmd.__init_ _(self, *args, **kw)
            self.namespace = {}
            self.target = DEFAULT_TARGET
            def precmd(self, line):
            parts = line.split(None , 2)
            if len(parts) == 3 and parts[1] == "=":
            self.target = parts[0]
            return parts[2]
            self.target = DEFAULT_TARGET
            return line
            def resolve(self, arg):
            args = shlex.split(arg )
            result = []
            for arg in args:
            try:
            value = self.namespace[arg]
            except KeyError:
            value = number(arg)
            result.append(v alue)
            return result
            def calc(self, func, arg):
            try:
            result = self.namespace[self.target] = func(self.resol ve(arg))
            except Exception, e:
            print e
            else:
            print result

            def do_sum(self, arg):
            self.calc(sum, arg)
            def do_max(self, arg):
            self.calc(max, arg)
            def do_print(self, arg):
            print " ".join(str( arg) for arg in self.resolve(ar g))
            def do_values(self, arg):
            pairs = sorted(self.nam espace.iteritem s())
            print "\n".join(" %s = %s" % nv for nv in pairs)
            def do_EOF(self, arg):
            return True

            if __name__ == "__main__":
            c = MyCmd()
            c.cmdloop()

            Peter

            Comment

            • Michele Simionato

              #7
              Re: cmd all commands method?

              On Feb 18, 10:49 am, "placid" <Bul...@gmail.c omwrote:
              On Feb 18, 7:17 pm, "Michele Simionato" <michele.simion ...@gmail.com>
              >
              Yes, he is talking about the cmd module:http://docs.python.org/dev/lib/Cmd-objects.html.
              However that module was never intended as a real interpreter, so
              defining variables
              as the OP wants would require some work.
              >
              Michele Simionato
              >
              How much work does it require ?
              Have you ever written an interpreter? It is a nontrivial job.

              Michele Simionato

              Comment

              • placid

                #8
                Re: cmd all commands method?

                On Feb 18, 8:59 pm, "Michele Simionato" <michele.simion ...@gmail.com>
                wrote:
                On Feb 18, 10:49 am, "placid" <Bul...@gmail.c omwrote:
                >
                On Feb 18, 7:17 pm, "Michele Simionato" <michele.simion ...@gmail.com>
                >
                Yes, he is talking about the cmd module:http://docs.python.org/dev/lib/Cmd-objects.html.
                However that module was never intended as a real interpreter, so
                defining variables
                as the OP wants would require some work.
                >
                Michele Simionato
                >
                How much work does it require ?
                >
                Have you ever written an interpreter? It is a nontrivial job.
                >
                Michele Simionato
                No i have never written an interpreter and i can just imagine how much
                work/effort is needed to write something like that.

                If anyone can provide a suggestion to replicate the following Tcl
                command in Python, i would greatly appreciate it.

                namespace eval foo {
                variable bar 12345
                }

                what this does is create a namespace foo with the variable bar set to
                12345.



                The code provided by Peter Otten is a good start for me. Cheers for
                that!


                Cheers

                Comment

                • Gabriel Genellina

                  #9
                  Re: cmd all commands method?

                  En Mon, 19 Feb 2007 00:08:45 -0300, placid <Bulkan@gmail.c omescribió:
                  If anyone can provide a suggestion to replicate the following Tcl
                  command in Python, i would greatly appreciate it.
                  >
                  namespace eval foo {
                  variable bar 12345
                  }
                  >
                  what this does is create a namespace foo with the variable bar set to
                  12345.
                  Python namespaces are simple dictionaries. See the eval function.

                  pys = "3+x**2"
                  pyfreevars = {"x": 2}
                  pyeval(s, {}, freevars)
                  7

                  --
                  Gabriel Genellina

                  Comment

                  Working...