Wrapper functions and arguments


One little issue I have is to write a little wrapper which can generally
pass standard and keyword arguments to a callee:

def a(x, y, z):
print x, y, z
def b(x, y, z='fruitbat')
print x, y, z

for func in a, b:
def wrapper(func=func, *args, **argsk):
# do something
func(*args, **argsk)
x.append(wrapper)

x[0](1, 2, 3)
x[1](1, 2)
....

Is there any way to do this Can you capture arguments in a tuple and dict,
but still receive other keyword arguments The only solution I found was to
implement wrapper as a class (like I would in c++):

class wrapper(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **argsk):
self.func(*args, **argsk)

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Posted On: Monday 5th of November 2012 02:26:48 AM Total Views:  474
View Complete with Replies

RELATED TOPICS OF Python Programming PROGRAMMING LANGUAGE




method-wrapper?

Within Python (2.5): >>> help("__str__") Help on method-wrapper object: __str__ = class method-wrapper(object) | Methods defined here: | | __call__(...) | x.__call__(...) x(...) | | __cmp__(...) | x.__cmp__(y) cmp(x,y) [...] What is "method-wrapper" Google turns up hardly any hits, same with searching python.org.
VIEWS ON THIS POST

118

Posted on:

Sunday 4th November 2012
View Replies!

announce: OraPIG (python wrappers for Oracle)

I'm pleased to announce the initial release of OraPIG, the Oracle Python Interface Generator. OraPIG generates Python wrapper classes for Oracle packages. If you use Oracle and Python, this might be a pretty useful package for you. Download it and let us know what you think! Cheers, Mark Download and docs: http://code.google.com/p/orapig ---------------- Quick Example: Suppose you have a package called tiny with a single procedure p: create or replace package tiny as procedure p(x in number); end tiny; OraPIG will generate a wrapper class that will allow you to use the package without having to use the various database calls. Instead, the wrapper class will be instantiated and called just like any other Python class: class Tiny: def __init__(self,curs): # instantiate class with a cursor ... def p(self,x): # call procedure p(x) ... Here's how it's used: import cx_Oracle import tiny conn = cx_Oracle.connect('scott/tiger') curs = conn.cursor() mytiny = tiny.Tiny(curs) mytiny.p(2) # call a procedure curs.commit() # not done automatically -- Mark Harrison Pixar Animation Studios
VIEWS ON THIS POST

186

Posted on:

Sunday 4th November 2012
View Replies!

generator functions in another language

I'm actually curious if there's a way to write a generator function (not a generator expression) in C, or what the simplest way to do it is... besides link the Python run-time. , A 'generator function' -- a function that when called returns a generator object, a specific type of iterator, is a rather Python specific concept. Better to think, I think, in terms of writing an iterator 'class' (in C, struct with associated function). Looking at the implementation of two of the itertools in the link below (previously posted), I could see the 'template' that each was based on and that would be the basis for writing another. (And I have not programmed C for a decade. Raymond's code is quite clear.) ================================= [Outlook Express does not 'quote' this post properly] "Gabriel Genellina" wrote in message newsp.uanmdnsrx6zn5v@a98gizw.cpe.telecentro.net.ar... The itertools module may be used as reference - "cycle" and "chain" are the easiest I think, although they might be *too* easy to understand the structure. "groupby" is a more complex example but at the same time harder to understand. See http://svn.python.org/projects/pytho...rtoolsmodule.c Ok, I'll try to use Python code as an example. A generator for Fibonacci numbers: def fibo(): a = b = 1 while True: a, b = b, a+b yield b We can convert that function into this object; it should be written in C, not Python, but the idea is the same: class fibo: def __init__(self): self.a = 1 self.b = 1 def next(self): temp = self.a + self.b self.a = self.b self.b = temp return temp def __iter__(self): return self ============================= Terry again: one can think of a generator function as an abbreviation of an iterator class. Calling the gen. func. produces an iterator instance just like calling the class object. ============================= [back to Gabriel] It behaves exactly the same as the generator above; we can even use the same code to test it: py> for n in fibo(): .... if n>100: break .... print n .... 2 3 5 8 13 21 34 55 89 Converting that class into C code should be straightforward. And then you have a generator-like function written in C. ==================================
VIEWS ON THIS POST

106

Posted on:

Sunday 4th November 2012
View Replies!

Re: wrapping C functions in python

SWIG is a program that automatically generates code to interface Python (and many other languages) with C/C++. If you plan to create lasting software libraries to be accessed from Python and C it is quite a robust way to do so. Essentially, you feed it header files, compile your code and the code generated by swig as a shared library, then import the functions like you would a normal python module. If all you need is to make some numerical code run faster it may be more of a learning curve then you bargain for. Perhaps trying NumPy is more appropriate. http://numpy.scipy.org/ -Brian On Wed, Apr 9, 2008 at 2:44 PM, Paul Anton Letnes wrote: > and thanks. > > > However, being a newbie, I now have to ask: What is SWIG I have heard the > name before, but haven't understood what it is, why I need it, or similar. > Could you please supply some hints > > > -Paul > > > Den 9. april. 2008 kl. 22.22 skrev Brian Cole: > > > > > > > > > > We use the following SWIG (www.swig.org) typemap to perform such > operations: > > > > %typemap(in) (int argc, char **argv) { > > if (!PySequence_Check($input)) { > > PyErr_SetString(PyExc_ValueError,"Expected a sequence"); > > return NULL; > > } > > $1 = PySequence_Length($input); > > $2 = (char**)alloca($1*sizeof(char*)); > > for (Py_ssize_t i = 0; i < $1; ++i) { > > PyObject *o = PySequence_GetItem($input, i); > > $2[i] = PyString_AsString(o); > > } > > } > > > > That one works for mapping a python sequence (such as a list) into the > > argc, argv arguments commonly passed into main. > > > > -Brian > > > > On Wed, Apr 9, 2008 at 2:13 PM, Paul Anton Letnes > > wrote: > > > > > etc. > > > > > > > > > I am a "scientific" user of Python, and hence have to write some > performance > > > critical algorithms. Right now, I am learning Python, so this is a > "newbie" > > > question. > > > > > > I would like to wrap some heavy C functions inside Python, specifically > a > > > wavelet transform. I am beginning to become aquainted with the functions > > > PyArg_ParseTuple() and Py_BuildValue(). However, I am unable to figure > out > > > how to pass Python list -> C function or C array -> return value in > Python. > > > I manage to build and run the C function, print to screen, pass string > as > > > argument, return an int, etc. The thing which is missing is the magic > > > array/list... > > > > > > > > >
VIEWS ON THIS POST

130

Posted on:

Sunday 4th November 2012
View Replies!

Re: fromfunc functions

On Feb 13, 5:51 pm, azrael wrote: > I came across the fromfunc() function in numpy where you pass as an > argument the name of a function as a string and also the atributes for > the deed function. > > I find this extremly usefull and sexy. Can someone point me how write > a function of such capabilities getattr(object, 'function name')(*args)
VIEWS ON THIS POST

125

Posted on:

Sunday 4th November 2012
View Replies!

ANN: shm and shm_wrapper 1.2

Hi all, Versions 1.2 of shm and shm_wrapper -- two modules which enable IPC (shared memory and sempahores) via Python -- are now available. The former is Vladimir Marangozov's old shm module with some bug fixes, the latter is my wrapper which I find more Pythonic. Changes from version 1.1 include code that should enable shm to be used in conjunction with threads as well as setup and compilation improvements for various platforms. Enjoy! http://NikitaTheSpider.com/python/shm/ -- Philip http://NikitaTheSpider.com/ Whole-site HTML validation, link checking and more
VIEWS ON THIS POST

118

Posted on:

Sunday 4th November 2012
View Replies!

Creating a python c-module: passing double arrays to c functions.segmentation fault. swig

everybody, I'm building a python module to do some heavy computation in C (for dynamic time warp distance computation). The module is working perfectly most of the time, which makes it so difficult to track down the error. and I finally figured out that the strange segmentation faults I get from time to time have something to do with the length of the vectors I'm looking at. (So in this minimal example I'm doing completely useless distance measures on vectors with the same entry in all dimensions, but the errors seem to have nothing to with the actual values in the vectors, only with the lengths.) But somehow I haven't quite gotten the essential ideas of what one has to do to debug this module and I hope someone can give me some advice. The main problem is probably somewhere around passing the double vectors to the c module or reserving and freeing memory. so, for example list1,list2 = [0.1 for i in range(555)],[0.2 for i in range(1874)] ctimewarp(list1,list2) # see python file below works perfectly fine and if the vector has one more element list1,list2 = [0.1 for i in range(555)],[0.2 for i in range(1875)] ctimewarp(list1,list2) it dies with a simple Segmentation fault nothing more said :-S For very small lists again, no problem: list1,list2 = [0.1 for i in range(3)],[0.2 for i in range(4)] ctimewarp(list1,list2) for intermediate size I get an error and more information: list1,list2 = [0.1 for i in range(22)],[0.2 for i in range(99)] ctimewarp(list1,list2) give: *** glibc detected *** python: free(): invalid next size (fast): 0x0804d090 *** ======= Backtrace: ========= /lib/i686/libc.so.6[0xb7bed4e6] /lib/i686/libc.so.6(cfree+0x90)[0xb7bf1010] /home/kim/Documents/pythonprojects/alignator/ _timewarpsimple.so[0xb7aadc0c] /home/kim/Documents/pythonprojects/alignator/ _timewarpsimple.so[0xb7aae0db] /usr/lib/libpython2.5.so.1.0(PyCFunction_Call+0x107)[0xb7d5d8e7] ======= Memory map: ======== 08048000-08049000 r-xp 00000000 03:05 2344438 /usr/bin/python 08049000-0804a000 rwxp 00000000 03:05 2344438 /usr/bin/python 0804a000-080f3000 rwxp 0804a000 00:00 0 [heap] .....(truncated) When looping over these lengths of vectors, e.g. as I did in the python file below, it runs smooth for thousands of times before dying at 938 for the first list length and 1110 for the second. i noticed that the sum of the list lengths is 2048=2^11 and it often seems to die around that but that's just a guess... If anyone has a little advice or a code snippet on how to pass float lists to modules or anything of that kind, i would appreciate it very much! thanks a lot in advance cheers kim __________________________________ Below you can see the c file, the .i file for swig, and a little python script producing the errors: _______________ .c file: /* File : timewarpsimple.c */ #include #include #include double timewarp(double x[], int lenx, double y[], int leny) { // printf ("%d *******************************\n", lenx); // printf ("%d *******************************\n", leny); double prev; double recx[lenx+1]; double recy[leny+1]; double warp[lenx+2][leny+2]; int i,j; prev = 0.0; for (i = 0; i < lenx; i++) { recx[i]=x[i]-prev; prev = x[i]; } recx[lenx]=1.0-prev; prev = 0.0; for (i = 0; i < leny; i++) { recy[i]=y[i]-prev; prev = y[i]; } recy[leny]=1.0-prev; // recency vectors are done // let's warp warp[0][0]=0.0; for (i = 1; i < lenx+2; i++) { warp[i][0]=1.0; } for (j = 1; j < leny+2; j++) { warp[0][j]=1.0; } for (i = 1; i < lenx+2; i++) { for (j = 1; j < leny+2; j++) { warp[i][j]=fabs(recx[i-1]-recy[j-1]) + MIN(MIN(warp[i-1][j],warp[i] [j-1]),warp[i-1][j-1]); } } return warp[lenx+1][leny+1]; } ________________ .i file: %module timewarpsimple %include "carrays.i" %array_functions(double, doubleArray); %{ double timewarp(double x[], int lenx, double y[], int leny); %} double timewarp(double x[], int lenx, double y[], int leny); ________________ here's what I'm doing to compile: swig -python timewarpsimple.i gcc -c timewarpsimple.c timewarpsimple_wrap.c -I/usr/include/ python2.5/ ld -shared timewarpsimple.o timewarpsimple_wrap.o -o _timewarpsimple.so which goes thru without any problem. ________________ .py file: #!/usr/bin/python # -*- coding: UTF-8 -*- import timewarpsimple def ctimewarp(list1,list2): """ takes two lists of numbers between 0 and 1 and computes a timewarp distance """ print "timewarping" alen = len(list1) blen = len(list2) a = timewarpsimple.new_doubleArray(alen*4) # Create the first array # Why I have to reserve 4 times more space I don't know, but it's the only way to make it work... b = timewarpsimple.new_doubleArray(alen*4) # Create the second array for i,p in enumerate(list1): timewarpsimple.doubleArray_setitem(a,i,p) # Set a value. (a,i,0) gives identical errors for i,p in enumerate(list2): timewarpsimple.doubleArray_setitem(b,i,p) # Set a value warp = timewarpsimple.timewarp(a,alen,b,blen) print "result",warp timewarpsimple.delete_doubleArray(a) timewarpsimple.delete_doubleArray(b) #a,b = None,None return warp ########## the tests: #list1,list2 = [0.1 for i in range(22)],[0.2 for i in range(99)] #ctimewarp(list1,list2) for x in range(888,1111): for y in range(888,1111): list1,list2 = [0.1 for i in range(x)], [0.2 for i in range(y)] print len(list1),len(list2),len(list1)+len(list2) ctimewarp(list1,list2)
VIEWS ON THIS POST

200

Posted on:

Sunday 4th November 2012
View Replies!

Dispatching functions from a dictionary

To keep a simulation tidy, I created a dispatcher that generates random variables drawn from various distributions as follows: import random RVType = 1 #Type of random variable - pulled from RVDict RVDict= {'1': random.betavariate(1,1), '2': random.expovariate(1), '3': random.gammavariate(1,1), '4': random.gauss(0,1), '5': random.lognormvariate(1,1), '6': random.paretovariate(1), '7': random.uniform( -1,1), '8': random.weibullvariate(1,2) } x = [] y=[] rv = RVDict[str(RVType)] for i in range(N): x.append(rv) y.append(rv) Oddly, x and y get filled with a single value repeated N times. I expected to get a different random number appear each time I called rv ,but this does not happen. Instead, my first call to rv generates a random number from the appropriate distribution, while subsequent calls simply repeat the random number generated in the first call. Where am I going wrong
VIEWS ON THIS POST

137

Posted on:

Sunday 4th November 2012
View Replies!

dynamically adding some of one module's functions to another

, I'm trying to import some modules into another's namespace (network_utils) network_utils.py: import utils import constants as main_const from services import constants as srv_const from plugins import constants as plg_const # # Import all the message creation functions # f = dict([ (f, eval("utils." + f)) for f in dir(utils) if f.startswith("create") ]) __builtins__.update(f) del utils # # Create a virgin namespace # class constants: """ An aggragation of L{constants}, L{services.constants}, & L{plugins.constants} """ pass # # Import all the constants into the new namespace # f = dict([ (f, eval("main_const." + f)) for f in dir(main_const) if f.isupper() ]) constants.__dict__.update(f) del main_const f = dict([ (f, eval("srv_const." + f)) for f in dir(srv_const) if f.isupper() ]) constants.__dict__.update(f) del srv_const f = dict([ (f, eval("plg_const." + f)) for f in dir(plg_const) if f.isupper() ]) constants.__dict__.update(f) del plg_const now, if I import network_utils somewhere, and try to call network_utils.myFunction() I get : >>> import network_utils >>> network_utils.myFunction Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'myFunction' but if I try to access the constants class: >>> from network_utils import constants >>> constants.NO_DATA_TYPE 'none' it works, how come Thank you, Gabriel
VIEWS ON THIS POST

164

Posted on:

Sunday 4th November 2012
View Replies!

functools's partial and update_wrapper does not work together

! I wanted to use a decorator to wrap partially applied function like this: from functools import * def never_throw(f): @wraps(f) def wrapper(*args, **kwargs): try: return f(*args, **kwargs) except: pass return wrapper def foo(i): raise ValueError(str(i) + " is too little") never_throw(partial(foo, 3)) However this fails with an exception saying AttributeError: 'functools.partial' object has no attribute '__module__'. How can I use generic wrapper (never_throw) around partials Thank you, Piotr
VIEWS ON THIS POST

289

Posted on:

Sunday 4th November 2012
View Replies!

any python wrapper to call .lib static library(ufmod.lib)?

I want to play .XM music using Python, now I found ufmod http://ufmod.sourceforge.net It only provide a ufmod.lib to be compiled in C/C++/BASIC, and Python as a scripting language could not handle these static libraries. What could I do to with these .lib files Cheers
VIEWS ON THIS POST

156

Posted on:

Sunday 4th November 2012
View Replies!

extending python with array functions

I want to make numerical functions that can be called from python. I am programming in pascal the last few decades so I had a look at "python for delphi" (P4D). The demo09 gives as example add(a,b) using integers and pyarg_parsetuple. That works! However, I cannot figure out what to do when a, b and the result are arrays (or matrices) of float (for i:=0 to high(a) do c[i]:=a[i]+b[i]; and then return c to python). Although from the ALGOL60 school and always tried to keep far from pointers, I might also understand advise in C. Please get me started e.g. by giving a simple example. Many
VIEWS ON THIS POST

151

Posted on:

Sunday 4th November 2012
View Replies!

Re: fromfunc functions

On Wed, 13 Feb 2008 07:51:36 -0800, azrael wrote: > I came across the fromfunc() function in numpy where you pass as an > argument the name of a function as a string and also the atributes for > the deed function. If you mean `fromfunction()` then you don't give the name of the function as string but the function itself as argument. And what you call attributes are arguments. > I find this extremly usefull and sexy. Can someone point me how write > a function of such capabilities Functions are objects in Python, you can pass them around like any other object/value. In [18]: def f(func, arg): ....: return func(arg) ....: In [19]: f(int, '42') Out[19]: 42 In [20]: f(str.split, 'a b c') Out[20]: ['a', 'b', 'c'] Ciao, Marc 'BlackJack' Rintsch
VIEWS ON THIS POST

128

Posted on:

Sunday 4th November 2012
View Replies!

Re: fromfunc functions

On Feb 13, 5:51 pm, azrael wrote: > I came across the fromfunc() function in numpy where you pass as an > argument the name of a function as a string and also the atributes for > the deed function. > > I find this extremly usefull and sexy. Can someone point me how write > a function of such capabilities class test_class(): def test_func(self, string): print string test = test_class() getattr(test, 'test_class'){'this is the test argument') getattr raises an Attribute Error though if the function doesn't exist so you'll need to catch that.
VIEWS ON THIS POST

130

Posted on:

Sunday 4th November 2012
View Replies!

Re: Need to call functions/class_methods etc using string ref :How

On Mon, Nov 26, 2007 at 04:07:03PM +0530, Ravi Kumar wrote regarding Need to call functions/class_methods etc using string ref :How: > > > First of all, since this is my first mail to Python-List, I want to say > " world!" > After that; > I am stuck in a project. Actually I am writing a module (for testing > now), which takes URL, parses it, finds which modules and then which > method to call or which class to initiate and which string to load. > So far, I have done some basic URL manipulation and validation and > extracted the name of modules in a dict > { > 'ModuleName-1': None, > 'ModuleName-2': None > --ETC-- > } > Now I want your help about how to call the function i.e _render() in > the module. I have to iterate it calling all modules/also Class.methods > and assinging the values in dict for each key as modulename. > Means, > just take that moduleName is a string which contains the name of module > to load. > FuncName is the string which contains the name of def to > call > clName is the string which contains the name of the Class, which is to > init and get returned object. > means everything in string. > ALso, if there are multiple methods to do it, if you provide a little > pros/cons and comments, it would really be very nice of you. > Before I came here, I have searched Google, and found a way > hasattr()/getattr(), but that confused me a little and didnt worked for > me. I am missing something I know, so please ENLIGHTEN Me >
VIEWS ON THIS POST

169

Posted on:

Monday 5th November 2012
View Replies!

[ Please help ] how to create Python functions in C++ at runtime &call them

I want to compile following type of python function in my C++ program at runtime. def isSizeSmall(size,vol,ADV,prod): if ( (size < 1000) & (vol < (0.001 * ADV)) & (prod=="Stock")): print "OK"; return 10 else: print "NOK"; return 11 Using Py_CompileString, I compiled a code object from this function, like: char szExpr[2048]; sprintf(szExpr,"def isSizeSmall(size,vol,ADV,prod):\n if ( (size
VIEWS ON THIS POST

211

Posted on:

Monday 5th November 2012
View Replies!

Re: Need to call functions/class_methods etc using string ref :How

> I see someone already showed you eval.  Eval is evil.  Don't use it. > Especially if the functions are coming to you from a public URL! Yes, I suggested to him (by email) this: thisinstance =  SomeObject.__class__.__dict__ for f in yourlist:  if f in thisinstance: eval(f)(params) Which would vet the functions too. \d
VIEWS ON THIS POST

161

Posted on:

Monday 5th November 2012
View Replies!

Accessing Function Variables from Sub-functions

On Apr 14 2003, 10:30 pm, Alex Martelli wrote: > Sebastian Wilhelmi wrote: > > > > > I would like to do the following: > > > -------8 count = 0 > > def inc_count (): > > count += 1 > > inc_count () > > inc_count () > > print count > > > test () > > -------8 > def test(): > class Bunch(MetaBunch): count=0 > loc = MetaBunch() > def inc_count(): > loc.count += 1 > > etc. I do find such constructs often handy... > > Alex
VIEWS ON THIS POST

122

Posted on:

Monday 5th November 2012
View Replies!

Autogenerate functions (array of lambdas)

What I want to do is build an array of lambda functions, like so: a = [lambda: i for i in range(10)] (This is just a demonstrative dummy array. I don't need better ways to achieve the above functionality.) print [f() for f in a] results in: [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] rather than the hoped for: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Clearly, lambda is returning the object i, which is left at the last value of range(10). The following is my solution. t = lambda i: lambda: i a = [t(i) for i in range(10)] or the somewhat more terse: a = [(lambda i: lambda: i)(i) for i in range(10)] This gives the behavior which, intuitively, I expected from the original syntax. So my questions are: 1) Does this make sense as what should be done here That is, would this be the behavior you'd want more often than not As I said, intuitively, I would think the lambda would treat the iterator variable as a constant in this context. 2) Is there a better or preferred method than the one I've found
VIEWS ON THIS POST

145

Posted on:

Monday 5th November 2012
View Replies!

Replacing overloaded functions with closures.

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- not-java.html) that in Python we can reduce code duplication for overloaded functions by using closures. I do not quite understand this. Let's say we have the following simple C# code: int func(int i) { return i * 2; } string func(string s) { return s + s; } bool func(bool f) { return !f; } I wasn't able to find a way to express this thing in closures. I even tried to have the parameters i, s and f as parameters with default arguments with None value in the inner function and there to check for the None and do the needed work but i am not sure if this is the correct solution.
VIEWS ON THIS POST

252

Posted on:

Monday 5th November 2012
View Replies!