Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Normalizing arguments

Reply
Thread Tools

Normalizing arguments

 
 
Dan Ellis
Guest
Posts: n/a
 
      10-17-2008
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?
 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      10-17-2008
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
the inspect module instead.

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/

 
Reply With Quote
 
 
 
 
Dan Ellis
Guest
Posts: n/a
 
      10-17-2008
On Oct 17, 5:13*pm, Steve Holden <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      10-17-2008
On Fri, Oct 17, 2008 at 8:37 AM, Dan Ellis <(E-Mail Removed)> 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?
> --
> 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
 
Reply With Quote
 
Dan Ellis
Guest
Posts: n/a
 
      10-17-2008
On Oct 17, 6:17*pm, "Chris Rebert" <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Aaron \Castironpi\ Brady
Guest
Posts: n/a
 
      10-17-2008
On Oct 17, 12:37*pm, Dan Ellis <(E-Mail Removed)> wrote:
> On Oct 17, 6:17*pm, "Chris Rebert" <(E-Mail Removed)> wrote:
>
> > 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:

http://groups.google.com/group/comp....357f9cb4c7bdeb
 
Reply With Quote
 
Dan Ellis
Guest
Posts: n/a
 
      10-19-2008
On Oct 17, 7:16*pm, "Aaron \"Castironpi\" Brady"
<(E-Mail Removed)> wrote:

> George Sakkis has a recipe that might help.
>
> http://code.activestate.com/recipes/551779/


Looks like just the thing. Thanks!
 
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
What is normalizing in XML? sivasu.india@gmail.com Java 1 04-09-2007 09:38 PM
Normalizing mp3's. What do you use? JohnF Computer Support 9 12-20-2006 03:58 PM
Normalizing XHTML with XML Ryan Stewart XML 3 05-11-2006 04:42 PM
Method for normalizing URL? Chris Java 6 01-12-2005 05:38 AM
Normalizing tm structure past 2038 Stu C Programming 5 11-01-2003 07:49 AM



Advertisments