Suggest design to accomodate non-unix platforms ?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Apr 18 20:55:19 EDT 2012


On Wed, 18 Apr 2012 17:16:08 -0700, Miki Tebeka wrote:

>> So I'm interested in suggestions/examples where a user can update a
>> config file to specify by which means they want (in this case) the ssh
>> functionality to be supplied.
> You can do something like that (it's called a factory):
> 
> COMMANDS = {
>     'win32': 'win32 command goes here',
>     'linux2': 'linux command goes here',
>     'darwin': 'OSX command goes here',
> }
> def get_command():
>     return COMMANDS.get(sys.platform)


Your suggestion is good, but I think your terminology is off. COMMANDS is 
a dispatch table, or a lookup table, and get_command merely does a look-
up in that table.

A factory dynamically builds a new object on request, rather than always 
return the same old object time and time again. So a factory might look 
like this:

def get_command(flag1=True, flag2=False, flag3=True):
    cmd_parts = [COMMANDS.get(sys.platform)]
    if flag1:
        cmd_parts.append('--some_option')
    else:
        cmd_parts.append('--another_option')
    cmd_parts.append('-x')
    if flag2:
        cmd_parts.append('spam')
    else:
        cmd_parts.append('ham')
    if flag3:
        cmd_parts.append('--foo=bar')
    cmd = ' '.join(cmd_parts)
    return cmd


-- 
Steven



More information about the Python-list mailing list