Normalizing arguments

D

Dan Ellis

Given some function, f(a, b, c=3), what would be the best way to go
about writing a function, g(f, *args, **kwargs), that would return a
normalized tuple of arguments that f would receive when calling
f(*args, **kwargs)? By normalized, I mean that the result would always
be (a, b, c) regardless of how g was called, taking into account
positional arguments, keyword arguments, and f's default arguments.

g(f, 1, 2, 3) -> (1, 2, 3)
g(f, 1, 2, c=3) -> (1, 2, 3)
g(f, 1, c=3, b=2) -> (1, 2, 3)
g(c=3, a=1, b=2) -> (1, 2, 3)
g(1, 2) -> (1, 2, 3)

All the required information is available between args, kwargs and f
(the function object), but I don't know the exact algorithm. Has
anyone already done this, or should I just dig around in the CPython
source and extract an algorithm from there?
 
S

Steve Holden

Dan said:
Given some function, f(a, b, c=3), what would be the best way to go
about writing a function, g(f, *args, **kwargs), that would return a
normalized tuple of arguments that f would receive when calling
f(*args, **kwargs)? By normalized, I mean that the result would always
be (a, b, c) regardless of how g was called, taking into account
positional arguments, keyword arguments, and f's default arguments.

g(f, 1, 2, 3) -> (1, 2, 3)
g(f, 1, 2, c=3) -> (1, 2, 3)
g(f, 1, c=3, b=2) -> (1, 2, 3)
g(c=3, a=1, b=2) -> (1, 2, 3)
g(1, 2) -> (1, 2, 3)

All the required information is available between args, kwargs and f
(the function object), but I don't know the exact algorithm. Has
anyone already done this, or should I just dig around in the CPython
source and extract an algorithm from there?

You'd get a lot further a lot faster by looking at the documentation for
the inspect module instead.

Here's your starter for 10 ...
.... pass
....
inspect.getargspec(f) (['a', 'b', 'c'], None, None, (3,))

regards
Steve
 
D

Dan Ellis

You'd get a lot further a lot faster by looking at the documentation for
the inspect module instead.

Yeah, I've looked at that already, but it only gives (in a nicer way)
the information I already have from the function object and its code
object.
 
C

Chris Rebert

Given some function, f(a, b, c=3), what would be the best way to go
about writing a function, g(f, *args, **kwargs), that would return a
normalized tuple of arguments that f would receive when calling
f(*args, **kwargs)? By normalized, I mean that the result would always
be (a, b, c) regardless of how g was called, taking into account
positional arguments, keyword arguments, and f's default arguments.

g(f, 1, 2, 3) -> (1, 2, 3)
g(f, 1, 2, c=3) -> (1, 2, 3)
g(f, 1, c=3, b=2) -> (1, 2, 3)
g(c=3, a=1, b=2) -> (1, 2, 3)
g(1, 2) -> (1, 2, 3)

All the required information is available between args, kwargs and f
(the function object), but I don't know the exact algorithm. Has
anyone already done this, or should I just dig around in the CPython
source and extract an algorithm from there?

Why do you want/need this magical g() function considering that, as
you yourself point out, Python already performs this normalization for
you?

Cheers,
Chris
 
D

Dan Ellis

Why do you want/need this magical g() function considering that, as
you yourself point out, Python already performs this normalization for
you?

A caching idea I'm playing around with.

@cache
def some_query(arg1, arg2):
# Maybe do SQL query or something
return result

cache returns a function that does:
- Make a key from its arguments
- If key is in the cache:
- Return result from cache
- If it isn't:
- Call some_query with the same arguments
- Cache and return the result
 
A

Aaron \Castironpi\ Brady

A caching idea I'm playing around with.

@cache
def some_query(arg1, arg2):
    # Maybe do SQL query or something
    return result

cache returns a function that does:
    - Make a key from its arguments
    - If key is in the cache:
        - Return result from cache
    - If it isn't:
        - Call some_query with the same arguments
        - Cache and return the result

George Sakkis has a recipe that might help.

http://code.activestate.com/recipes/551779/

It was discussed here:

http://groups.google.com/group/comp...4/31357f9cb4c7bdeb?lnk=st&q=#31357f9cb4c7bdeb
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top