Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Function to apply superset of arguments to a function

Reply
Thread Tools

Function to apply superset of arguments to a function

 
 
Andrey Fedorov
Guest
Posts: n/a
 
      09-09-2009
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
 
Reply With Quote
 
 
 
 
David Stanek
Guest
Posts: n/a
 
      09-09-2009
On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
7stud
Guest
Posts: n/a
 
      09-09-2009
On Sep 9, 10:45*am, Andrey Fedorov <(E-Mail Removed)> 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.
 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      09-09-2009
David Stanek wrote:
> On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<(E-Mail Removed)>
> 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.


 
Reply With Quote
 
David Stanek
Guest
Posts: n/a
 
      09-09-2009
On Wed, Sep 9, 2009 at 5:03 PM, Mel<(E-Mail Removed)> wrote:
> David Stanek wrote:
>> On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<(E-Mail Removed)>
>> 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
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      09-09-2009
On Sep 9, 10:40*am, David Stanek <(E-Mail Removed)> wrote:
> On Wed, Sep 9, 2009 at 12:45 PM, Andrey Fedorov<(E-Mail Removed)> 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
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      09-09-2009
On Sep 9, 11:47*am, 7stud <(E-Mail Removed)> wrote:
> On Sep 9, 10:45*am, Andrey Fedorov <(E-Mail Removed)> 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
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      09-09-2009
On Sep 9, 9:45*am, Andrey Fedorov <(E-Mail Removed)> 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
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      09-10-2009
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
 
Reply With Quote
 
Carl Banks
Guest
Posts: n/a
 
      09-10-2009
On Sep 9, 8:14*pm, Steven D'Aprano
<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C as a Subset of C++ (or C++ as a superset of C) Ansel C Programming 191 12-17-2012 11:11 PM
C as a Subset of C++ (or C++ as a superset of C) Ansel C++ 130 09-04-2012 01:10 PM
Newbie: Are Ruby regexp's a subset, superset, or equal to Perl's? Harry Ruby 12 09-23-2009 09:45 PM
Frighteners superset, any news? Grand Inquisitor DVD Video 0 11-20-2003 03:02 AM
[XSLT] could not apply "apply-templates" Stefan Siegl XML 1 07-18-2003 09:43 AM



Advertisments