472,337 Members | 1,687 Online

# Normalizing arguments

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?
Oct 17 '08 #1
6 1353
Dan Ellis wrote:
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

Here's your starter for 10 ...
>>def f(a, b, c=3):
.... pass
....
>>inspect.getargspec(f)
(['a', 'b', 'c'], None, None, (3,))
>>>
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Oct 17 '08 #2
On Oct 17, 5:13*pm, Steve Holden <st...@holdenweb.comwrote:
You'd get a lot further a lot faster by looking at the documentation for
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.
Oct 17 '08 #3
On Fri, Oct 17, 2008 at 8:37 AM, Dan Ellis <da*@remember.this.namewrote:
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?
--
http://mail.python.org/mailman/listinfo/python-list
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
--
Follow the path of the Iguana...
http://rebertia.com
Oct 17 '08 #4
On Oct 17, 6:17*pm, "Chris Rebert" <c...@rebertia.comwrote:
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
Oct 17 '08 #5
On Oct 17, 12:37*pm, Dan Ellis <d...@remember.this.namewrote:
On Oct 17, 6:17*pm, "Chris Rebert" <c...@rebertia.comwrote:
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
George Sakkis has a recipe that might help.

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

It was discussed here:

Oct 17 '08 #6
On Oct 17, 7:16*pm, "Aaron \"Castironpi\" Brady"
<castiro...@gmail.comwrote:
George Sakkis has a recipe that might help.

http://code.activestate.com/recipes/551779/
Looks like just the thing. Thanks!
Oct 19 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 0 by: Evan Escently | last post by: Hi, I've laid out a _very_ simple database that tracks my artwork the table 'works' looks like: ... 4 by: Evan Escently | last post by: Hi, I've laid out a _very_ simple database that tracks my artwork the table 'works' looks like: ... 3 by: Megan | last post by: hello everybody- i'm normalizing a database i inherited. i'm breaking up a huge table named case into several smaller tables. i am creating... 8 by: Richard Hollenbeck | last post by: I have a recipe database that I've been building but I haven't yet put any of the ingredients in because of a little problem of normalization. If I... 0 by: concettolabs | last post by: In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool... 0 by: better678 | last post by: Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented... 0 by: teenabhardwaj | last post by: How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of... 0 by: Kemmylinns12 | last post by: Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and... 0 by: CD Tom | last post by: This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right... 0 by: jalbright99669 | last post by: Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made... 0 by: Matthew3360 | last post by: Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ... 2 by: Matthew3360 | last post by: Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it... 0 by: AndyPSV | last post by: HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...