By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,360 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,360 IT Pros & Developers. It's quick & easy.

Overloaded Functions

P: n/a
Hi,

So this may have been asked before but i haven't found the answer by
googling so far. My situation is this:

I want this structure for my code:

@overloaded
def sign_auth(secret, salt, auth_normalized):
return __sign_auth(saltedhash_bin(secret, salt), auth_normalized)

@sign_auth.register(str, str)
def sign_auth(secret_hash_normalized, auth_normalized):
return __sign_auth(qcrypt.denormalize(secret_hash_normali zed),
auth_normalized)

def __sign_auth(secret_hash_bin, auth_normalized):
auth = qcrypt.denormalize(auth_normalized)
aes = AES.new(secret_hash_bin, AES.MODE_CBC)
plaintext = aes.decrypt(auth)
ciphertext = qcrypt.normalize(xor_in_pi(plaintext))
if debug:
print '\n------sign_auth------'
print qcrypt.normalize(secret_hash_bin)
print qcrypt.normalize(plaintext)
print ciphertext
print '-----sign_auth-------\n'
return ciphertext

I am using the overloading module from:
http://svn.python.org/view/sandbox/t...43727&view=log

However it doesn't actually support this functionality. Does any one
know of a decorator that does this? It would be really nice to have a
unified interface into the __sign_auth function for the two different
use cases.

Tim Henderson
Jul 29 '08 #1
Share this Question
Share on Google+
3 Replies

P: n/a
Tim Henderson wrote:
Hi,

So this may have been asked before but i haven't found the answer by
googling so far. My situation is this:

I want this structure for my code:

@overloaded
def sign_auth(secret, salt, auth_normalized):
return __sign_auth(saltedhash_bin(secret, salt), auth_normalized)

@sign_auth.register(str, str)
def sign_auth(secret_hash_normalized, auth_normalized):
return __sign_auth(qcrypt.denormalize(secret_hash_normali zed),
auth_normalized)

def __sign_auth(secret_hash_bin, auth_normalized):
auth = qcrypt.denormalize(auth_normalized)
aes = AES.new(secret_hash_bin, AES.MODE_CBC)
plaintext = aes.decrypt(auth)
ciphertext = qcrypt.normalize(xor_in_pi(plaintext))
if debug:
print '\n------sign_auth------'
print qcrypt.normalize(secret_hash_bin)
print qcrypt.normalize(plaintext)
print ciphertext
print '-----sign_auth-------\n'
return ciphertext

I am using the overloading module from:
http://svn.python.org/view/sandbox/t...43727&view=log

However it doesn't actually support this functionality. Does any one
know of a decorator that does this? It would be really nice to have a
unified interface into the __sign_auth function for the two different
use cases.
Are you aware that you can do this kind of thing yourself, without using
a module/decorator.

def sign_auth(*args):
if len(args) == 3:
secret, salt, auth_normalized = args
return __sign_auth(saltedhash_bin(secret, salt), auth_normalized)
elif len(args) == 2:
secret_hash_normalized, auth_normalized = args
return __sign_auth(qcrypt.denormalize(secret_hash_normali zed), auth_normalized)

The checks could be more involved, perhaps checking not jsut the nuimber of args, but their types, and even their values, before calling __sign_auth(...).
Gary Herron

Tim Henderson
--
http://mail.python.org/mailman/listinfo/python-list
Jul 29 '08 #2

P: n/a
Yes i am aware of that but I want the code to be self documenting, so
the intent is clear. I actually have an implementation using that
style which you suggest. I would like cleaner style, like the one i
suggested in my first post.

Cheers
Tim Henderson
Jul 29 '08 #3

P: n/a
With a little hacking, you might be able to do something like this:

@overload("f", (int, int, str))
def f1(x, y, z):
pass

@overload("f", (str, str))
def f2(x, y):
pass
The way I would typically do method overloading would be as follows
(this has been tested):

class Person:
def __init__(*args):
argTypes = tuple(map(type,args))

methods = {
(str,int) : Person.initAllInfo,
(str,) : Person.initOnlyName,
}

methods[argTypes[1:]](*args)

# ........................

def initAllInfo(self, name, age):
self.name = name
self.age = age

def initOnlyName(self, name):
self.name = name
self.age = 100
With this overload-dictionary approach, it may be possible to
elegantly implement the overloading decorator I described at the top
of this email. I hoped this helped.

Daniel
On Tue, Jul 29, 2008 at 12:05 PM, Tim Henderson <ti******@gmail.comwrote:
Yes i am aware of that but I want the code to be self documenting, so
the intent is clear. I actually have an implementation using that
style which you suggest. I would like cleaner style, like the one i
suggested in my first post.

Cheers
Tim Henderson
--
http://mail.python.org/mailman/listinfo/python-list
Jul 30 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.