473,322 Members | 1,690 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 1432
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/

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

http://groups.google.com/group/comp....357f9cb4c7bdeb
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: +---------+----------+------------+------------+-------------+ | work_id | title | media ...
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: +---------+----------+------------+------------+-------------+ | work_id | title | media ...
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 several many to many relationships between the new...
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 build a table of ingredients, all the recipes...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.