Function to apply superset of arguments to a function

Discussion in 'Python' started by Andrey Fedorov, Sep 9, 2009.

  1. Hi all,

    I've written a function [1] called apply_some which takes a set of
    keywords arguments, filters only those a function is expecting, and
    calls the function with only those arguments. This is meant to
    suppress TypeErrors - a way to abstract the logic which checks what
    arguments a passed-in function accepts.

    For example:

    > def foo(x=1, y=2):
    > return (x,y)
    >
    > apply_some(foo, y=0, z="hi") // calls foo(y=0)
    > -> (1,0)


    I'd like to expand this to fill undefined arguments with None, but
    before I do, does anyone know of any packages/libraries which either
    do something similar or would make this code cleaner?

    Cheers,
    Andrey

    1. http://gist.github.com/183375
     
    Andrey Fedorov, Sep 9, 2009
    #1
    1. Advertising

  2. Andrey Fedorov

    David Stanek Guest

    On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<> wrote:
    > Hi all,
    >
    > I've written a function [1] called apply_some which takes a set of
    > keywords arguments, filters only those a function is expecting, and
    > calls the function with only those arguments. This is meant to
    > suppress TypeErrors - a way to abstract the logic which checks what
    > arguments a passed-in function accepts.
    >
    > For example:
    >
    >> def foo(x=1, y=2):
    >>    return (x,y)
    >>
    >> apply_some(foo, y=0, z="hi") // calls foo(y=0)
    >> -> (1,0)

    >
    > I'd like to expand this to fill undefined arguments with None, but
    > before I do, does anyone know of any packages/libraries which either
    > do something similar or would make this code cleaner?
    >
    > Cheers,
    > Andrey
    >
    > 1. http://gist.github.com/183375
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >


    What is your use-case for using this? It seems really odd to me.

    --
    David
    blog: http://www.traceback.org
    twitter: http://twitter.com/dstanek
     
    David Stanek, Sep 9, 2009
    #2
    1. Advertising

  3. Andrey Fedorov

    7stud Guest

    On Sep 9, 10:45 am, Andrey Fedorov <> wrote:
    > Hi all,
    >
    > I've written a function [1] called apply_some which takes a set of
    > keywords arguments, filters only those a function is expecting, and
    > calls the function with only those arguments. This is meant to
    > suppress TypeErrors - a way to abstract the logic which checks what
    > arguments a passed-in function accepts.
    >
    > For example:
    >
    > > def foo(x=1, y=2):
    > >    return (x,y)

    >
    > > apply_some(foo, y=0, z="hi") // calls foo(y=0)
    > > -> (1,0)

    >
    > I'd like to expand this to fill undefined arguments with None, but
    > before I do, does anyone know of any packages/libraries which either
    > do something similar or would make this code cleaner?
    >
    > Cheers,
    > Andrey
    >
    > 1.http://gist.github.com/183375


    It sounds like all you are doing is moving type checking out of the
    original function and into another function. In scripting languages,
    like python, type checking is frowned upon. The accepted idiom is to
    use try-except.
     
    7stud, Sep 9, 2009
    #3
  4. Andrey Fedorov

    Mel Guest

    David Stanek wrote:
    > On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<>
    > wrote:


    >> I've written a function [1] called apply_some which takes a set of
    >> keywords arguments, filters only those a function is expecting, and
    >> calls the function with only those arguments. This is meant to
    >> suppress TypeErrors - [ ... ]


    > What is your use-case for using this? It seems really odd to me.


    I may have run into one possible use. I have a program that uses DB-API 2.0
    to work with a database, sometimes SQLite3, sometimes PostgreSQL. Connect
    parameters are built up in a dict from a combination of defaults,
    environment values, and command-line arguments. This dictionary has to be
    pruned down before calling connect, because sqlite3.connect doesn't take the
    wide assortment of arguments that are needed for PostgreSQL. The existing
    program does this "by hand", being programmed in advance to know which
    parameters are acceptable to which database module.


    Mel.
     
    Mel, Sep 9, 2009
    #4
  5. Andrey Fedorov

    David Stanek Guest

    On Wed, Sep 9, 2009 at 5:03 PM, Mel<> wrote:
    > David Stanek wrote:
    >> On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<>
    >> wrote:

    >
    >>> I've written a function [1] called apply_some which takes a set of
    >>> keywords arguments, filters only those a function is expecting, and
    >>> calls the function with only those arguments. This is meant to
    >>> suppress TypeErrors - [ ... ]

    >
    >> What is your use-case for using this? It seems really odd to me.

    >
    > I may have run into one possible use.  I have a program that uses DB-API 2.0
    > to work with a database, sometimes SQLite3, sometimes PostgreSQL.  Connect
    > parameters are built up in a dict from a combination of defaults,
    > environment values, and command-line arguments.  This dictionary has to be
    > pruned down before calling connect, because sqlite3.connect doesn't take the
    > wide assortment of arguments that are needed for PostgreSQL.  The existing
    > program does this "by hand", being programmed in advance to know which
    > parameters are acceptable to which database module.
    >


    The way I would normally do it is to use the URI format and based on
    the scheme choose a handler. For example: mysql://user@host:server/db,
    would translate to a mysql specific function that knows what to do.
    Adding support for new schemes does mean an additional function, but I
    like the control. And it's explicit.

    I worry about the magic of the OP's approach. In your example generate
    a dictionary in a generic way and apply it to a set of functions. What
    happens if in the future if connect_mysql adds adds a parameter with
    the same name as one in connect_postgres, but different semantics. Now
    the magic is broken and you have an ugly special case.


    --
    David
    blog: http://www.traceback.org
    twitter: http://twitter.com/dstanek
     
    David Stanek, Sep 9, 2009
    #5
  6. Andrey Fedorov

    Carl Banks Guest

    On Sep 9, 10:40 am, David Stanek <> wrote:
    > On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<> wrote:
    > > Hi all,

    >
    > > I've written a function [1] called apply_some which takes a set of
    > > keywords arguments, filters only those a function is expecting, and
    > > calls the function with only those arguments. This is meant to
    > > suppress TypeErrors - a way to abstract the logic which checks what
    > > arguments a passed-in function accepts.

    >
    > > For example:

    >
    > >> def foo(x=1, y=2):
    > >>    return (x,y)

    >
    > >> apply_some(foo, y=0, z="hi") // calls foo(y=0)
    > >> -> (1,0)

    >
    > > I'd like to expand this to fill undefined arguments with None, but
    > > before I do, does anyone know of any packages/libraries which either
    > > do something similar or would make this code cleaner?

    >
    > What is your use-case for using this? It seems really odd to me.


    Use case seems perfectly obvious to me. You have a set of functions
    that use different strategies to accomplish a task, and there is a lot
    of overlap in the arguments used but no consistency. You want to be
    able to swap in different functions so as to try different strategies
    (either while editing, or programmatically). Instead of dealing with
    arguments that change everytime you want to swap in a different
    function, you can use this filter and work with a single argument set.

    The example that pops to my mind is numerical optimization. Many
    numerical optimizers have similar overall strategies but differ in
    underlying details, so they will tend to take different options. An
    argument filter like the OP wrote would be quite handy here.


    Carl Banks
     
    Carl Banks, Sep 9, 2009
    #6
  7. Andrey Fedorov

    Carl Banks Guest

    On Sep 9, 11:47 am, 7stud <> wrote:
    > On Sep 9, 10:45 am, Andrey Fedorov <> wrote:
    >
    >
    >
    >
    >
    > > Hi all,

    >
    > > I've written a function [1] called apply_some which takes a set of
    > > keywords arguments, filters only those a function is expecting, and
    > > calls the function with only those arguments. This is meant to
    > > suppress TypeErrors - a way to abstract the logic which checks what
    > > arguments a passed-in function accepts.

    >
    > > For example:

    >
    > > > def foo(x=1, y=2):
    > > >    return (x,y)

    >
    > > > apply_some(foo, y=0, z="hi") // calls foo(y=0)
    > > > -> (1,0)

    >
    > > I'd like to expand this to fill undefined arguments with None, but
    > > before I do, does anyone know of any packages/libraries which either
    > > do something similar or would make this code cleaner?

    >
    > > Cheers,
    > > Andrey

    >
    > > 1.http://gist.github.com/183375

    >
    > It sounds like all you are doing is moving type checking out of the
    > original function and into another function.


    I'm not sure where you got the idea that he was doing type checking,
    unless you just saw the word "TypeError" and made a knee-jerk
    assumption about what was he was doing. If you'd have bothered trying
    to understand the post you would have seen that he was filtering
    arguments by name.


    Carl Banks
     
    Carl Banks, Sep 9, 2009
    #7
  8. Andrey Fedorov

    Carl Banks Guest

    On Sep 9, 9:45 am, Andrey Fedorov <> wrote:
    > Hi all,
    >
    > I've written a function [1] called apply_some which takes a set of
    > keywords arguments, filters only those a function is expecting, and
    > calls the function with only those arguments. This is meant to
    > suppress TypeErrors - a way to abstract the logic which checks what
    > arguments a passed-in function accepts.
    >
    > For example:
    >
    > > def foo(x=1, y=2):
    > >    return (x,y)

    >
    > > apply_some(foo, y=0, z="hi") // calls foo(y=0)
    > > -> (1,0)

    >
    > I'd like to expand this to fill undefined arguments with None, but
    > before I do, does anyone know of any packages/libraries which either
    > do something similar or would make this code cleaner?


    I don't know of any, but I would be surprised of the code you posted
    could be made any cleaner. Even if it's possible to improve your
    implementation of apply_some, your usage is as clean and
    straightforward as it gets.


    Carl Banks
     
    Carl Banks, Sep 9, 2009
    #8
  9. On Wed, 09 Sep 2009 14:58:22 -0700, Carl Banks wrote:

    > Use case seems perfectly obvious to me. You have a set of functions
    > that use different strategies to accomplish a task, and there is a lot
    > of overlap in the arguments used but no consistency. You want to be
    > able to swap in different functions so as to try different strategies
    > (either while editing, or programmatically). Instead of dealing with
    > arguments that change everytime you want to swap in a different
    > function, you can use this filter and work with a single argument set.
    >
    > The example that pops to my mind is numerical optimization. Many
    > numerical optimizers have similar overall strategies but differ in
    > underlying details, so they will tend to take different options. An
    > argument filter like the OP wrote would be quite handy here.


    Even more handy would be an abstraction layer that gives you a consistent
    API to work with, instead of the fragile and brittle anti-pattern of
    "suppress errors from passing invalid arguments".



    --
    Steven
     
    Steven D'Aprano, Sep 10, 2009
    #9
  10. Andrey Fedorov

    Carl Banks Guest

    On Sep 9, 8:14 pm, Steven D'Aprano
    <> wrote:
    > On Wed, 09 Sep 2009 14:58:22 -0700, Carl Banks wrote:
    > > Use case seems perfectly obvious to me.  You have a set of functions
    > > that use different strategies to accomplish a task, and there is a lot
    > > of overlap in the arguments used but no consistency.  You want to be
    > > able to swap in different functions so as to try different strategies
    > > (either while editing, or programmatically).  Instead of dealing with
    > > arguments that change everytime you want to swap in a different
    > > function, you can use this filter and work with a single argument set.

    >
    > > The example that pops to my mind is numerical optimization.  Many
    > > numerical optimizers have similar overall strategies but differ in
    > > underlying details, so they will tend to take different options.  An
    > > argument filter like the OP wrote would be quite handy here.

    >
    > Even more handy would be an abstraction layer that gives you a consistent
    > API to work with, instead of the fragile and brittle anti-pattern of
    > "suppress errors from passing invalid arguments".


    It'd be better, but I don't know if it'd be handier.


    Carl Banks
     
    Carl Banks, Sep 10, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    1
    Views:
    998
    Marrow
    Jul 18, 2003
  2. jmborr
    Replies:
    1
    Views:
    458
    Stargaming
    Nov 3, 2007
  3. Harry
    Replies:
    12
    Views:
    309
    Simon Krahnke
    Sep 23, 2009
  4. Ansel
    Replies:
    191
    Views:
    3,158
    Ian Collins
    Dec 17, 2012
  5. Ansel
    Replies:
    130
    Views:
    1,825
    Alf P. Steinbach
    Sep 4, 2012
Loading...

Share This Page