473,395 Members | 1,504 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,395 software developers and data experts.

Overloaded Functions

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
3 1067
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. ...
44
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
12
by: Jack Daly | last post by:
I've inherited some code which uses an undocumented feature of a third-party vendor's library. Essentially, this vendor has kept the details of an interface struct secret, but we can pass a pointer...
10
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
4
by: king kikapu | last post by:
Hi, i am trying, to no avail yet, to take a C#'s overloaded functions skeleton and rewrite it in Python by using closures. I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-...
1
by: bob | last post by:
I was just wondering if overloaded type cast functions must always be member functions of some class.
2
by: SlimT88 | last post by:
I recently created a simple class for a homework assignment and ran into compilation errors concerning overloaded global functions. I've copied the relevant parts of my code below and the errors...
9
by: Lawrence Krubner | last post by:
Possibly a dumb question but is type hinting allowed with overloaded methods? Can one class have these two methods? public function doSelect(Criteria $c) { // some code here } public...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.