Path: csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'else:': 0.03; 'from:addr:yahoo.co.uk': 0.04; 'interpreter': 0.05; 'method.': 0.07; 'sys': 0.07; 'string': 0.09; 'defines': 0.09; 'lawrence': 0.09; 'lines.': 0.09; 'override': 0.09; 'predefined': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'subject:string': 0.09; 'tismer': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; 'cmd': 0.16; 'converted,': 0.16; 'param': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'language': 0.16; 'wrote:': 0.18; 'implementing': 0.19; 'thanks.': 0.20; 'command': 0.22; '>>>': 0.22; 'programming': 0.22; 'import': 0.22; 'shell': 0.22; 'header:User-Agent:1': 0.23; 'certainly': 0.24; 'define': 0.26; 'second': 0.26; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'on,': 0.29; "i'm": 0.30; "d'aprano": 0.31; 'steven': 0.31; 'class': 0.32; 'another': 0.32; 'but': 0.35; 'add': 0.35; 'building': 0.35; 'possible': 0.36; 'url:org': 0.36; 'turn': 0.37; 'list': 0.37; 'christian': 0.38; 'nov': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'commands': 0.60; 'eye': 0.61; 'world.': 0.61; 'our': 0.64; 'fire': 0.65; 'smith': 0.68; 'default': 0.69; 'article': 0.77; '(help': 0.84; 'battery': 0.84; 'person)': 0.84; 'you.)': 0.84; '2013': 0.98 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Fire Method by predefined string! Date: Mon, 18 Nov 2013 02:29:52 +0000 References: <5289704c$0$29975$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-78-147-25-45.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 In-Reply-To: <5289704c$0$29975$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1384741808 news.xs4all.nl 15949 [2001:888:2000:d::a6]:44380 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:59826 On 18/11/2013 01:41, Steven D'Aprano wrote: > On Sun, 17 Nov 2013 17:20:52 -0500, Roy Smith wrote: > >> In article , >> Tamer Higazi 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