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


Groups > comp.lang.python > #28986 > unrolled thread

Re: avoid the redefinition of a function

Started byJabba Laci <jabba.laci@gmail.com>
First post2012-09-12 18:56 +0200
Last post2012-09-13 17:59 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: avoid the redefinition of a function Jabba Laci <jabba.laci@gmail.com> - 2012-09-12 18:56 +0200
    Re: avoid the redefinition of a function Alister <alister.ware@ntlworld.com> - 2012-09-12 18:04 +0000
      Re: avoid the redefinition of a function MRAB <python@mrabarnett.plus.com> - 2012-09-12 19:33 +0100
      Re: avoid the redefinition of a function D'Arcy Cain <darcy@druid.net> - 2012-09-12 17:49 -0400
      Re: avoid the redefinition of a function Peter Otten <__peter__@web.de> - 2012-09-13 10:23 +0200
        Re: avoid the redefinition of a function Alister <alister.ware@ntlworld.com> - 2012-09-13 17:59 +0000

#28986 — Re: avoid the redefinition of a function

FromJabba Laci <jabba.laci@gmail.com>
Date2012-09-12 18:56 +0200
SubjectRe: avoid the redefinition of a function
Message-ID<mailman.572.1347469027.27098.python-list@python.org>
> For example:
>
> def install_java():
>    pass
>
> def install_tomcat():
>    pass

Thanks for the answers. I decided to use numbers in the name of the
functions to facilitate function calls. Now if you have this menu
option for instance:

(5) install mc

You can type just "5" as user input and step_5() is called
automatically. If I use descriptive names like install_java() then
selecting a menu point would be more difficult. And I don't want users
to type "java", I want to stick to simple numbers.

Laszlo

[toc] | [next] | [standalone]


#28991

FromAlister <alister.ware@ntlworld.com>
Date2012-09-12 18:04 +0000
Message-ID<7944s.27167$CU7.24982@fx02.am4>
In reply to#28986
On Wed, 12 Sep 2012 18:56:46 +0200, Jabba Laci wrote:

>> For example:
>>
>> def install_java():
>>    pass
>>
>> def install_tomcat():
>>    pass
> 
> Thanks for the answers. I decided to use numbers in the name of the
> functions to facilitate function calls. Now if you have this menu option
> for instance:
> 
> (5) install mc
> 
> You can type just "5" as user input and step_5() is called
> automatically. If I use descriptive names like install_java() then
> selecting a menu point would be more difficult. And I don't want users
> to type "java", I want to stick to simple numbers.
> 
> Laszlo

No No NO!
you cant just pass user input to system calls without validating it first
(google sql injection for examples of the damage unsanitised input can 
cause, it is not just as SQL problem)

it is just as easy so select a reasonably named function as a bad one

option=raw_input('select your option :')

if option =="1": install_java()
if option =="2": install_other()

alternatively you cold add your functions into a dictionary an call them 
from that

opts={'1':install java,'2':install_other}

option=raw_input('select your option :')
opts[option]

Poorly named functions are a major example of poor programming style.

one of the fundamental pillars for python is readability!



-- 
<knghtbrd> He's a about half the size of the others.
<knghtbrd> But he's got a chainsaw.

[toc] | [prev] | [next] | [standalone]


#28992

FromMRAB <python@mrabarnett.plus.com>
Date2012-09-12 19:33 +0100
Message-ID<mailman.575.1347474795.27098.python-list@python.org>
In reply to#28991
On 12/09/2012 19:04, Alister wrote:
> On Wed, 12 Sep 2012 18:56:46 +0200, Jabba Laci wrote:
>
>>> For example:
>>>
>>> def install_java():
>>>    pass
>>>
>>> def install_tomcat():
>>>    pass
>>
>> Thanks for the answers. I decided to use numbers in the name of the
>> functions to facilitate function calls. Now if you have this menu option
>> for instance:
>>
>> (5) install mc
>>
>> You can type just "5" as user input and step_5() is called
>> automatically. If I use descriptive names like install_java() then
>> selecting a menu point would be more difficult. And I don't want users
>> to type "java", I want to stick to simple numbers.
>>
>> Laszlo
>
> No No NO!
> you cant just pass user input to system calls without validating it first
> (google sql injection for examples of the damage unsanitised input can
> cause, it is not just as SQL problem)
>
> it is just as easy so select a reasonably named function as a bad one
>
> option=raw_input('select your option :')
>
> if option =="1": install_java()
> if option =="2": install_other()
>
> alternatively you cold add your functions into a dictionary an call them
> from that
>
> opts={'1':install java,'2':install_other}
>
> option=raw_input('select your option :')
> opts[option]
>
> Poorly named functions are a major example of poor programming style.
>
> one of the fundamental pillars for python is readability!
>
Or you could do this:


def install_java():
     "Install Java"
     print "Installing Java"

def install_tomcat():
     "Install Tomcat"
     print "Installing Tomcat"

menu = [install_java, install_tomcat]

for index, func in enumerate(menu, start=1):
     print "{0}) {1}".format(index, func.__doc__)

option = raw_input("Select your option : ")

try:
     opt = int(option)
except ValueError:
     print "Not a valid option"
else:
     if 1 <= opt < len(menu):
         menu[opt - 1]()
     else:
         print "Not a valid option"

[toc] | [prev] | [next] | [standalone]


#29002

FromD'Arcy Cain <darcy@druid.net>
Date2012-09-12 17:49 -0400
Message-ID<mailman.582.1347486566.27098.python-list@python.org>
In reply to#28991
On Wed, 12 Sep 2012 18:04:51 GMT
Alister <alister.ware@ntlworld.com> wrote:
> No No NO!
> you cant just pass user input to system calls without validating it first
> (google sql injection for examples of the damage unsanitised input can 
> cause, it is not just as SQL problem)

 http://xkcd.com/327/

-- 
D'Arcy J.M. Cain <darcy@druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.
IM: darcy@Vex.Net

[toc] | [prev] | [next] | [standalone]


#29020

FromPeter Otten <__peter__@web.de>
Date2012-09-13 10:23 +0200
Message-ID<mailman.596.1347524593.27098.python-list@python.org>
In reply to#28991
MRAB wrote:

> On 12/09/2012 19:04, Alister wrote:
>> On Wed, 12 Sep 2012 18:56:46 +0200, Jabba Laci wrote:
>>
>>>> For example:
>>>>
>>>> def install_java():
>>>>    pass
>>>>
>>>> def install_tomcat():
>>>>    pass
>>>
>>> Thanks for the answers. I decided to use numbers in the name of the
>>> functions to facilitate function calls. Now if you have this menu option
>>> for instance:
>>>
>>> (5) install mc
>>>
>>> You can type just "5" as user input and step_5() is called
>>> automatically. If I use descriptive names like install_java() then
>>> selecting a menu point would be more difficult. And I don't want users
>>> to type "java", I want to stick to simple numbers.
>>>
>>> Laszlo
>>
>> No No NO!
>> you cant just pass user input to system calls without validating it first
>> (google sql injection for examples of the damage unsanitised input can
>> cause, it is not just as SQL problem)
>>
>> it is just as easy so select a reasonably named function as a bad one
>>
>> option=raw_input('select your option :')
>>
>> if option =="1": install_java()
>> if option =="2": install_other()
>>
>> alternatively you cold add your functions into a dictionary an call them
>> from that
>>
>> opts={'1':install java,'2':install_other}
>>
>> option=raw_input('select your option :')
>> opts[option]
>>
>> Poorly named functions are a major example of poor programming style.
>>
>> one of the fundamental pillars for python is readability!
>>
> Or you could do this:
> 
> 
> def install_java():
>      "Install Java"
>      print "Installing Java"
> 
> def install_tomcat():
>      "Install Tomcat"
>      print "Installing Tomcat"
> 
> menu = [install_java, install_tomcat]
> 
> for index, func in enumerate(menu, start=1):
>      print "{0}) {1}".format(index, func.__doc__)
> 
> option = raw_input("Select your option : ")
> 
> try:
>      opt = int(option)
> except ValueError:
>      print "Not a valid option"
> else:
>      if 1 <= opt < len(menu):
>          menu[opt - 1]()
>      else:
>          print "Not a valid option"

I'd still argue that a function index is the wrong approach. You can use tab 
completion to make entering descriptive names more convenient:

import cmd

class Cmd(cmd.Cmd):
    prompt = "Enter a command (? for help): "

    def do_EOF(self, args):
        return True
    def do_quit(self, args):
        return True

    @classmethod
    def install_command(class_, f):
        def wrapped(self, arg):
            if arg:
                print "Discarding argument {!r}".format(arg)
            return f()

        wrapped.__doc__ = f.__doc__
        wrapped.__name__ = f.__name__
        class_._add_method("do_" + f.__name__, wrapped)
        return f

    @classmethod
    def _add_method(class_, methodname, method):
        if hasattr(class_, methodname):
            raise ValueError("Duplicate command {!r}".format(methodname))
        setattr(class_, methodname, method)

command = Cmd.install_command

@command
def install_java():
     "Install Java"
     print "Installing Java"

@command
def install_tomcat():
     "Install Tomcat"
     print "Installing Tomcat"

if __name__ == "__main__":
    Cmd().cmdloop()

[toc] | [prev] | [next] | [standalone]


#29059

FromAlister <alister.ware@ntlworld.com>
Date2012-09-13 17:59 +0000
Message-ID<8ap4s.78261$pt3.37809@fx25.am4>
In reply to#29020
On Thu, 13 Sep 2012 10:23:22 +0200, Peter Otten wrote:

> MRAB wrote:
> 
>> On 12/09/2012 19:04, Alister wrote:
>>> On Wed, 12 Sep 2012 18:56:46 +0200, Jabba Laci wrote:
>>>
>>>>> For example:
>>>>>
>>>>> def install_java():
>>>>>    pass
>>>>>
>>>>> def install_tomcat():
>>>>>    pass
>>>>
>>>> Thanks for the answers. I decided to use numbers in the name of the
>>>> functions to facilitate function calls. Now if you have this menu
>>>> option for instance:
>>>>
>>>> (5) install mc
>>>>
>>>> You can type just "5" as user input and step_5() is called
>>>> automatically. If I use descriptive names like install_java() then
>>>> selecting a menu point would be more difficult. And I don't want
>>>> users to type "java", I want to stick to simple numbers.
>>>>
>>>> Laszlo
>>>
>>> No No NO!
>>> you cant just pass user input to system calls without validating it
>>> first (google sql injection for examples of the damage unsanitised
>>> input can cause, it is not just as SQL problem)
>>>
>>> it is just as easy so select a reasonably named function as a bad one
>>>
>>> option=raw_input('select your option :')
>>>
>>> if option =="1": install_java()
>>> if option =="2": install_other()
>>>
>>> alternatively you cold add your functions into a dictionary an call
>>> them from that
>>>
>>> opts={'1':install java,'2':install_other}
>>>
>>> option=raw_input('select your option :')
>>> opts[option]
>>>
>>> Poorly named functions are a major example of poor programming style.
>>>
>>> one of the fundamental pillars for python is readability!
>>>
>> Or you could do this:
>> 
>> 
>> def install_java():
>>      "Install Java"
>>      print "Installing Java"
>> 
>> def install_tomcat():
>>      "Install Tomcat"
>>      print "Installing Tomcat"
>> 
>> menu = [install_java, install_tomcat]
>> 
>> for index, func in enumerate(menu, start=1):
>>      print "{0}) {1}".format(index, func.__doc__)
>> 
>> option = raw_input("Select your option : ")
>> 
>> try:
>>      opt = int(option)
>> except ValueError:
>>      print "Not a valid option"
>> else:
>>      if 1 <= opt < len(menu):
>>          menu[opt - 1]()
>>      else:
>>          print "Not a valid option"
> 
> I'd still argue that a function index is the wrong approach. You can use
> tab completion to make entering descriptive names more convenient:
> 
> import cmd
> 
> class Cmd(cmd.Cmd):
>     prompt = "Enter a command (? for help): "
> 
>     def do_EOF(self, args):
>         return True
>     def do_quit(self, args):
>         return True
> 
>     @classmethod def install_command(class_, f):
>         def wrapped(self, arg):
>             if arg:
>                 print "Discarding argument {!r}".format(arg)
>             return f()
> 
>         wrapped.__doc__ = f.__doc__
>         wrapped.__name__ = f.__name__ class_._add_method("do_" +
>         f.__name__, wrapped)
>         return f
> 
>     @classmethod def _add_method(class_, methodname, method):
>         if hasattr(class_, methodname):
>             raise ValueError("Duplicate command
>             {!r}".format(methodname))
>         setattr(class_, methodname, method)
> 
> command = Cmd.install_command
> 
> @command def install_java():
>      "Install Java"
>      print "Installing Java"
> 
> @command def install_tomcat():
>      "Install Tomcat"
>      print "Installing Tomcat"
> 
> if __name__ == "__main__":
>     Cmd().cmdloop()

To be honest I prefer the "if X do Y" approach for readability but a 
dictionary can be undated dynamically & used to automatically create the 
menu so it can have its place


-- 
I'll see you... on the dark side of the moon...
		-- Pink Floyd

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web