Function to apply superset of arguments to a function

A

Andrey Fedorov

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
 
D

David Stanek

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

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

7stud

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.
 
M

Mel

David said:
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.
 
D

David Stanek

David said:
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.
 
C

Carl Banks

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:
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
 
C

Carl Banks

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:
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?

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
 
C

Carl Banks

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
 
S

Steven D'Aprano

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".
 
C

Carl Banks

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top