473,695 Members | 2,477 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 1448
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.getar gspec(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...@holdenwe b.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.t his.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...@gma il.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 598 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 | category | year | etc.... +---------+----------+------------+------------+-------------+ | 1 | One | oil | painting | 2002 | +---------+----------+------------+------------+-------------+ 4 2301 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 | category | year | etc.... +---------+----------+------------+------------+-------------+ | 1 | One | oil | painting | 2002 | +---------+----------+------------+------------+-------------+ 3 1702 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 case table and the other newly created tables. however, i have run into a problem when trying to create a many to many relationship between 2 of my tables. 8 6641 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 will have one or more ingredient(s), but no set number of ingredients, so that doesn't seem possible to normalize it. Any given recipe would use one, some, or all of the ingredients within the ingredients table, so I can't imagine an indefinite... 0 8636 by: marktang | last post by: ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of... 0 8577 by: Hystou | last post by: Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,... 0 9120 by: Oralloy | last post by: Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in... 0 8996 by: jinu1996 | last post by: In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is... 0 7667 by: agi2029 | last post by: Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea... 1 6494 by: isladogs | last post by: The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will... 0 5841 by: conductexam | last post by: I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();... 0 4345 by: TSSRALBI | last post by: Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls... 0 4583 by: adsilva | last post by: A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.