Overloaded Functions
Gary Herron
gherron at islandtraining.com
Tue Jul 29 11:58:13 EDT 2008
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_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
> Tim Henderson
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list