Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #59826

Re: Fire Method by predefined string!

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Fire Method by predefined string!
Date 2013-11-18 02:29 +0000
References <mailman.2807.1384725251.18130.python-list@python.org> <roy-53F741.17205217112013@news.panix.com> <5289704c$0$29975$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.2816.1384741808.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 18/11/2013 01:41, Steven D'Aprano wrote:
> On Sun, 17 Nov 2013 17:20:52 -0500, Roy Smith wrote:
>
>> In article <mailman.2807.1384725251.18130.python-list@python.org>,
>>   Tamer Higazi <th982a@googlemail.com> wrote:
>
>>> I want the param which is a string to be converted, that I can fire
>>> directly a method. Is it somehow possible in python, instead of writing
>>> if else statements ???!
>>
>> I'm not sure why you'd want to do this, but it's certainly possible
>
> It is very good for implementing the Command Dispatch pattern, which in
> turn is very good for building little command interpreters or mini-
> shells. Python even comes with a battery for that:
>
>
> import cmd
> import sys
> class MyShell(cmd.Cmd):
>      # Override default behaviour of empty lines.
>      def emptyline(self):
>          pass
>      # Define commands for our shell by prefixing them with "do_".
>      def do_hello(self, person):
>          if person:
>              print("Hello, %s!" % person)
>          else:
>              print("Hello!")
>      def do_echo(self, line):
>          print(line)
>      def do_double(self, num):
>          print(2*float(num))
>      def do_bye(self, line):
>          return True
>
> MyShell().cmdloop()
>
>
> This defines and runs a command interpreter that understands commands
> "bye", "double", "echo", "hello" and "help". (Help is predefined for you.)
>
> See also http://drunkenpython.org/dispatcher-pattern-safety.html for
> another use of command dispatch.
>

Neat, and yet another Python site to add to my list to keep an eye on, 
thanks.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Fire Method by predefined string! Tamer Higazi <th982a@googlemail.com> - 2013-11-17 22:46 +0100
  Re: Fire Method by predefined string! Roy Smith <roy@panix.com> - 2013-11-17 17:20 -0500
    Re: Fire Method by predefined string! Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-18 01:41 +0000
      Re: Fire Method by predefined string! Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-18 02:29 +0000
  Re: Fire Method by predefined string! Rick Johnson <rantingrickjohnson@gmail.com> - 2013-11-17 14:23 -0800
    Re: Fire Method by predefined string! Rick Johnson <rantingrickjohnson@gmail.com> - 2013-11-17 14:38 -0800

csiph-web