multimethod (or rather overloading) in Python
Dear pythonistas! I'd like to emulate overloading in Python (like C++). Let's consider an example: class A(object): pass class B(object): pass Of course, there are some standard overloading implementations for Python. For example: def overload(cls): def wrapper(f): gl = f.func_globals next = gl.get(f.func_name, failedOverload_(f.func_name)) def _(obj, *args, **kwargs): if isinstance(obj, cls): return f(obj, *args, **kwargs) else: return next(obj, *args, **kwargs) return _ @overload(A) def foo(_): print '[EMAIL PROTECTED]' @overload(B) def foo(_): print '[EMAIL PROTECTED]' However, it obviously doesn't work for classes: I cannot overload instance methods with such a decorator. The best way I found is closures. Unfortunatley, in this case I need a hack: gl = f.func_globals turns into: gl = sys._getframe(1).f_locals and with this hack one can make the following trick: def poorManOverloadedMethods(someInfo): @overload(A) def _(_): print '%s: [EMAIL PROTECTED]' % someInfo @overload(B) def _(_): print '%s: [EMAIL PROTECTED]' % someInfo return _ PMOM = poorManOverloadedMethods('test') ... Of course, I can imagine some metaclasses magic that would allow to code: class MyClass(WithOverloading): @overloadMethod(A) def someMetod(self, _): ... But it would rather convoluted: the best idea I have so far is to mangle methods name in the manner most of C++ compilers do. Is there better way? Can I unify both @overload and @overloadMethod? with the best regards, anton. -- http://mail.python.org/mailman/listinfo/python-list
Re: multimethod (or rather overloading) in Python
anton muhin wrote: Correction: Of course, I can imagine some metaclasses magic that would allow to code: class MyClass(WithOverloading): @overloadMethod(A) def someMetod(self, _): ... But it would rather convoluted: the best idea I have so far is to mangle methods name in the manner most of C++ compilers do. Stupid me. Of course, name magling is impossible and unnecessary. Sorry. Still the question remains. with the best regards, anton. -- http://mail.python.org/mailman/listinfo/python-list
Re: [perl-python] exercise: partition a list by equivalence
Xah Lee wrote: here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of pairs, each pair indicates the sameness of the two indexes. Returns a partitioned list of same indexes. For example, if the input is merge( [ [1,2], [2,4], [5,6] ] ); that means 1 and 2 are the same. 2 and 4 are the same. Therefore 1==2==4. The result returned is [[4,2,1],[6,5]]; (ordering of the returned list and sublists are not specified.) =cut Almost a joke: from numarray import * def merge(*pairs): flattened = reduce(tuple.__add__, pairs, tuple()) m, M = min(flattened), max(flattened) d = M - m + 1 matrix = zeros((d, d), type = Bool) for x, y in pairs: X, Y = x - m, y - m matrix[X, X] = 1 matrix[X, Y] = 1 matrix[Y, X] = 1 matrix[Y, Y] = 1 while True: next = greater(dot(matrix, matrix), 0) if alltrue(ravel(next == matrix)): break matrix = next results = [] for i in range(d): eqls, = nonzero(matrix[i]) if eqls.size(): if i == eqls[0]: results.append(tuple(x + m for x in eqls)) return results Disclaimer: I'm not an expert in numarray and suppose the something can be dramatically imporved. -- http://mail.python.org/mailman/listinfo/python-list
Re: multimethod (or rather overloading) in Python
Nick Coghlan wrote: anton muhin wrote: anton muhin wrote: Correction: Of course, I can imagine some metaclasses magic that would allow to code: class MyClass(WithOverloading): @overloadMethod(A) def someMetod(self, _): ... But it would rather convoluted: the best idea I have so far is to mangle methods name in the manner most of C++ compilers do. PEAK has a fairly sophisticated implementation of method dispatch you may want to look at. http://peak.telecommunity.com/Articles/WhatisPEAK.html http://dirtsimple.org/2004/11/generic-functions-have-landed.html http://peak.telecommunity.com/doc/src/dispatch/__init__.html I'm compelled to point out that PEAK should still be considered a 'work in progress', but PJE's ideas should help you out :) Cheers, Nick. Thank you very much, Nick! anton. -- http://mail.python.org/mailman/listinfo/python-list
Re: Syntax for extracting multiple items from a dictionary
Stefan Behnel wrote: shark schrieb: row = {"fname" : "Frank", "lname" : "Jones", "city" : "Hoboken", "state" : "Alaska"} cols = ("city", "state") Is there a best-practices way to ask for an object containing only the keys named in cols out of row? In other words, to get this: {"city" : "Hoboken", "state" : "Alaska"} Untested: dict( (key,value) for (key,value) in row.iteritems() if key in cols ) Works in Py2.4 Stefan Or dict((key, row[key]) for key in cols). regards, anton. -- http://mail.python.org/mailman/listinfo/python-list