Overloaded Functions

T

Tim Henderson

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_normalized),
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/trunk/overload/overloading.py?rev=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
 
G

Gary Herron

Tim said:
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_normalized),
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/trunk/overload/overloading.py?rev=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_normalized), 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
 
T

Tim Henderson

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
 
D

Daniel da Silva

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top