Function arguments
Hello! Is there any way that I can get at all the arguments passed to a function as a map without using keyword arguments? def foo(a, b, c): # Can I access all the arguments in a collection somewhere? I'm mainly curious since I have stumbled on to some cases where it might have been nice to be able to do that. :.:: mattias -- http://mail.python.org/mailman/listinfo/python-list
Re: Function arguments
On Jan 26, 10:11 am, Chris Rebert wrote: > On Mon, Jan 26, 2009 at 1:03 AM, brasse wrote: > > Hello! > > > Is there any way that I can get at all the arguments passed to a > > function as a map without using keyword arguments? > > > def foo(a, b, c): > > # Can I access all the arguments in a collection somewhere? > > You can use positional arguments: > > def foo(*args): > print args > > foo("a", "b", "c") #==> ["a", "b", "c"] > > Though if you explained your situation more, the newsgroup could > probably be of greater help. > This is an abbreviated version of what I am doing now: def make_data(**kw): ''' make_data(foo='123', bar=42, time=time.time()) ''' template = '%(foo)s - %(bar)d - %(time)s' kw['time'] = time.strftime('%c', kw['time'] return template % kw This works, but the function signature doesn't say much about arguments I should pass to it. What I would like to do is something like this: def make_data(foo, bar time): template = '%(foo)s - %(bar)d - %(time)s' args = magic_get_args_function() args['time'] = time.strftime('%c', args['time'] return template % args I hope this should clear things up a bit. :-) :.:: mattias -- http://mail.python.org/mailman/listinfo/python-list
Re: Function arguments
On Jan 26, 10:39 am, Chris Rebert wrote: > On Mon, Jan 26, 2009 at 1:34 AM, brasse wrote: > > On Jan 26, 10:11 am, Chris Rebert wrote: > >> On Mon, Jan 26, 2009 at 1:03 AM, brasse wrote: > >> > Hello! > > >> > Is there any way that I can get at all the arguments passed to a > >> > function as a map without using keyword arguments? > > >> > def foo(a, b, c): > >> > # Can I access all the arguments in a collection somewhere? > > >> You can use positional arguments: > > >> def foo(*args): > >> print args > > >> foo("a", "b", "c") #==> ["a", "b", "c"] > > >> Though if you explained your situation more, the newsgroup could > >> probably be of greater help. > > > This is an abbreviated version of what I am doing now: > > > def make_data(**kw): > > ''' > > make_data(foo='123', bar=42, time=time.time()) > > ''' > > template = '%(foo)s - %(bar)d - %(time)s' > > kw['time'] = time.strftime('%c', kw['time'] > > return template % kw > > > This works, but the function signature doesn't say much about > > arguments I should pass to it. What I would like to do is something > > like this: > > > def make_data(foo, bar time): > > template = '%(foo)s - %(bar)d - %(time)s' > > args = magic_get_args_function() > > args['time'] = time.strftime('%c', args['time'] > > return template % args > > Just use locals() as was pointed out by Diez: > > def make_data(foo, bar, time): > template = '%(foo)s - %(bar)d - %(time)s' > time = time.strftime('%c', time) > return template % locals() > Nice, thank you! :.:: mattias -- http://mail.python.org/mailman/listinfo/python-list
Builing Python 2.6 on AIX 5.2
Hello! I am having some trouble building Python 2.6 on AIX. The steps I have taken are: export PATH=/usr/bin/:/usr/vacpp/bin/ ./configure --with-gcc=xlc_r --with-cxx=xlC_r --disable-ipv6 make This is the error message I'm seeing: ./Modules/ld_so_aix xlc_r -bI:Modules/python.exp build/ temp.aix-5.2-2.6/home/mabr/Python-2.6/Modules/_multiprocessing/ multiprocessing.o build/temp.aix-5.2-2.6/home/mabr/Python-2.6/Modules/ _multiprocessing/socket_connection.o build/temp.aix-5.2-2.6/home/mabr/ Python-2.6/Modules/_multiprocessing/semaphore.o -L/usr/local/lib -o build/lib.aix-5.2-2.6/_multiprocessing.so ld: 0711-317 ERROR: Undefined symbol: .sem_timedwait ld: 0711-317 ERROR: Undefined symbol: .CMSG_SPACE ld: 0711-317 ERROR: Undefined symbol: .CMSG_LEN ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information. *** WARNING: renaming "_multiprocessing" since importing it failed: No such file or directory error: No such file or directory make: The error code from the last command is 1. Have someone on this list had similar problems? Am I missing some libraries? The configure script runs without errors, I would have expected some kind of error there if I was missing something. Regards, Mattias -- http://mail.python.org/mailman/listinfo/python-list
Re: Builing Python 2.6 on AIX 5.2
On Oct 6, 5:03 pm, "Jesse Noller" <[EMAIL PROTECTED]> wrote: > Looks like AIX is missing sem_timedwait - see:http://bugs.python.org/issue3876 > > Please add your error to the bug report just so I can track it. > > -jesse > OK, I have now added my information to the bug tracker. I would be quite happy if I could build Python on AIX without the multiprocessing module. Is there some way I can skip some selected modules when building Python. I tried with ./configure --without- multiprocessing, but that didn't work. :.:: mattias > On Mon, Oct 6, 2008 at 4:16 AM, brasse <[EMAIL PROTECTED]> wrote: > > Hello! > > > I am having some trouble building Python 2.6 on AIX. The steps I have > > taken are: > > > export PATH=/usr/bin/:/usr/vacpp/bin/ > > ./configure --with-gcc=xlc_r --with-cxx=xlC_r --disable-ipv6 > > make > > > This is the error message I'm seeing: > > ./Modules/ld_so_aix xlc_r -bI:Modules/python.exp build/ > > temp.aix-5.2-2.6/home/mabr/Python-2.6/Modules/_multiprocessing/ > > multiprocessing.o build/temp.aix-5.2-2.6/home/mabr/Python-2.6/Modules/ > > _multiprocessing/socket_connection.o build/temp.aix-5.2-2.6/home/mabr/ > > Python-2.6/Modules/_multiprocessing/semaphore.o -L/usr/local/lib -o > > build/lib.aix-5.2-2.6/_multiprocessing.so > > ld: 0711-317 ERROR: Undefined symbol: .sem_timedwait > > ld: 0711-317 ERROR: Undefined symbol: .CMSG_SPACE > > ld: 0711-317 ERROR: Undefined symbol: .CMSG_LEN > > ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more > > information. > > *** WARNING: renaming "_multiprocessing" since importing it failed: No > > such file or directory > > error: No such file or directory > > make: The error code from the last command is 1. > > > Have someone on this list had similar problems? Am I missing some > > libraries? The configure script runs without errors, I would have > > expected some kind of error there if I was missing something. > > > Regards, > > Mattias > > -- > >http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
Re: Builing Python 2.6 on AIX 5.2
On Oct 6, 10:16 am, brasse <[EMAIL PROTECTED]> wrote: > Hello! > > I am having some trouble building Python 2.6 on AIX. The steps I have > taken are: > > export PATH=/usr/bin/:/usr/vacpp/bin/ > ./configure --with-gcc=xlc_r --with-cxx=xlC_r --disable-ipv6 > make > > This is the error message I'm seeing: > ./Modules/ld_so_aix xlc_r -bI:Modules/python.exp build/ > temp.aix-5.2-2.6/home/mabr/Python-2.6/Modules/_multiprocessing/ > multiprocessing.o build/temp.aix-5.2-2.6/home/mabr/Python-2.6/Modules/ > _multiprocessing/socket_connection.o build/temp.aix-5.2-2.6/home/mabr/ > Python-2.6/Modules/_multiprocessing/semaphore.o -L/usr/local/lib -o > build/lib.aix-5.2-2.6/_multiprocessing.so > ld: 0711-317 ERROR: Undefined symbol: .sem_timedwait > ld: 0711-317 ERROR: Undefined symbol: .CMSG_SPACE > ld: 0711-317 ERROR: Undefined symbol: .CMSG_LEN > ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more > information. > *** WARNING: renaming "_multiprocessing" since importing it failed: No > such file or directory > error: No such file or directory > make: The error code from the last command is 1. > > Have someone on this list had similar problems? Am I missing some > libraries? The configure script runs without errors, I would have > expected some kind of error there if I was missing something. > > Regards, > Mattias OK. I have made some changes in the source that lets me build on AIX 5.2. I thought I could post the patch here and perhaps someone can tell me if I am on the wrong track or if this is an OK fix on AIX. Basically I have changed setup.py to define HAVE_SEM_TIMED_WAIT=0 on aix. I have also defined CMESG_SPACE and CMESG_LEN in terms of _CMSG_ALIGN (see http://homepage.mac.com/cjgibbons/rubyonaixhowto/x72.html) in multipocessing.c. (I realise that this breaks some other platforms, but right now I just need to build on AIX). Here is a patch: diff -Naur Python-2.6/Modules/_multiprocessing/multiprocessing.c Python-2.6-clean-patch/Modules/_multiprocessing/multiprocessing.c --- Python-2.6/Modules/_multiprocessing/multiprocessing.c 2008-06-14 00:38:33.0 +0200 +++ Python-2.6-clean-patch/Modules/_multiprocessing/ multiprocessing.c 2008-10-07 12:23:55.0 +0200 @@ -8,6 +8,13 @@ #include "multiprocessing.h" +#ifndef CMSG_SPACE +#define CMSG_SPACE(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + _CMSG_ALIGN(len)) +#endif +#ifndef CMSG_LEN +#define CMSG_LEN(len) (_CMSG_ALIGN(sizeof(struct cmsghdr)) + (len)) +#endif + PyObject *create_win32_namespace(void); PyObject *pickle_dumps, *pickle_loads, *pickle_protocol; diff -Naur Python-2.6/setup.py Python-2.6-clean-patch/setup.py --- Python-2.6/setup.py 2008-09-30 02:15:45.0 +0200 +++ Python-2.6-clean-patch/setup.py 2008-10-07 12:23:34.0 +0200 @@ -1277,6 +1277,14 @@ ) libraries = [] +elif platform.startswith('aix'): +macros = dict( +HAVE_SEM_OPEN=1, +HAVE_SEM_TIMEDWAIT=0, +HAVE_FD_TRANSFER=1 +) +libraries = ['rt'] + else: # Linux and other unices macros = dict( HAVE_SEM_OPEN=1, Perhaps this should go to some other list? :.:: mattias -- http://mail.python.org/mailman/listinfo/python-list
contextlib.nested()
Hello! I have been running in to some problems when using contextlib.nested(). My problem arises when using code similar to this: from __future__ import with_statement from contextlib import nested class Foo(object): def __init__(self, tag, fail=False): print 'ctor', tag self.tag = tag if fail: raise Exception() def __enter__(self): print '__enter__', self.tag return self def __exit__(self, *args): print '__exit__', self.tag with nested(Foo('a'), Foo('b', True)) as (a, b): print a.tag print b.tag Here the construction of b fails which in turn means that the contextmanager fails to be created leaving me a constructed object (a) that needs to be deconstructed in some way. I realize that nested() is in a tight spot here to do anything about it since it doesn't exist. This behavior makes it hard for me to use the with statement (using nested()) the way I want. Has anyone else been running in to this? Any tips on how to handle multiple resources? Regards, Mattias -- http://mail.python.org/mailman/listinfo/python-list
Re: contextlib.nested()
On Nov 6, 11:43 am, Robert Lehmann <[EMAIL PROTECTED]> wrote: > On Thu, 06 Nov 2008 01:02:34 -0800, brasse wrote: > > Hello! > > > I have been running in to some problems when using contextlib.nested(). > > My problem arises when using code similar to this: > > > from __future__ import with_statement > > > from contextlib import nested > > > class Foo(object): > > > def __init__(self, tag, fail=False): > > print 'ctor', tag > > self.tag = tag > > if fail: > > raise Exception() > > > def __enter__(self): > > print '__enter__', self.tag > > return self > > > def __exit__(self, *args): > > print '__exit__', self.tag > > > with nested(Foo('a'), Foo('b', True)) as (a, b): > > print a.tag > > print b.tag > > > Here the construction of b fails which in turn means that the > > contextmanager fails to be created leaving me a constructed object (a) > > that needs to be deconstructed in some way. I realize that nested() is > > in a tight spot here to do anything about it since it doesn't exist. > > This behavior makes it hard for me to use the with statement (using > > nested()) the way I want. > > > Has anyone else been running in to this? Any tips on how to handle > > multiple resources? > > Your problem does not seem to be connected to context managers. The error > occurs before calling `contextlib.nested` at all:: > > >>> foo = [Foo('a')] > ctor a > >>> with nested(*foo) as a: print a > ... > __enter__ a > [<__main__.Foo object at 0x7fbc29408b90>] > __exit__ a > >>> foo = [Foo('a'), Foo('b', True)] > ctor a > ctor b > Traceback (most recent call last): > File "", line 1, in > File "", line 7, in __init__ > raise Exception() > Exception > > If you need to deconstruct object `a` from your example, your staging is > probably broken. Allocate the resource in `__init__` but only go live > just in `__enter__`. If you do not enter the context, then, you won't > need to deconstruct it as well. > > HTH, > > -- > Robert "Stargaming" Lehmann Diez, Robert, OK. The practice of "going live" or doing non-trivial initialization in __enter__ is new to me. I'm new to Python with a C++ background, so that shouldn't be a surprise. :-) Ideally I would like to put all initialization in __init__ since then I would be able to use my object right after constructing it, without having to use it in a with statement. The reason I'm struggling with this is probably my C++ background. I'm rally accustomed to design with RAII in mind. Acquiring all resources in the ctor and releasing all resources in the dtor is *really* handy. If you had a class that wanted to acquire some external resources that must be released at some point, how would you rewrite the code from my example? :.:: mattias -- http://mail.python.org/mailman/listinfo/python-list
Re: contextlib.nested()
On Nov 6, 5:45 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: > > If you had a class that wanted to acquire some external resources that > > must be released at some point, how would you rewrite the code from my > > example? > > If you *can*, use a context. Use __enter__ and __exit__. Try really hard to > use it that way. > My case becomes something like this: from __future__ import with_statement from contextlib import nested class Foo(object): def __init__(self, tag, fail=False): print 'ctor', tag self.tag = tag self.fail = fail def __enter__(self): if self.fail: print 'fail', self.tag raise Exception() print '__enter__ acquire resource', self.tag return self def __exit__(self, *args): print '__exit__ release resource', self.tag with nested(Foo('a'), Foo('b', True)) as (a, b): print a.tag print b.tag When using Foo objects in a with statement this works good for me. But what if I want to use Foo objects as members in a class for example? Since we now must contruct an instance of Foo in two stages the code becomes less than ideal. def __init__(self): self.x = Foo() self.x.__enter__() Perhaps there is no way to write classes that fits neatly into all (or even these two) usage scenarios? > If not - create a specific finalize-method or some such, and try not to > forget to call that. Potentially with an atexit-handler or some such. > It seems to me that I have to use the with statement (or some try- finally construct) to be able to release all resources when my code throws exceptions(). Just remembering to call close/finalize/destroy will not be enough. :.:: mattias -- http://mail.python.org/mailman/listinfo/python-list
Re: contextlib.nested()
On Nov 7, 10:33 am, Peter Otten <[EMAIL PROTECTED]> wrote: > brasse wrote: > > with nested(Foo('a'), Foo('b', True)) as (a, b): > > print a.tag > > print b.tag > > If been watching this thread for a while, and I think that your problems > will go away if you write actual nested with-blocks: > > with Foo("a") as a: > with Foo("b") as b: > print a.tag > print b.tag > > Why look for a complex solution if there is a simple one? > That works great if you are only working with two objects. It gets a bit uglier when you need to use three or more objects. I'm just trying to figure out if there is some kind of best practice in the Python community that works well (even with more than two objects) for the two usage scenarios I have described. :.:: mattias -- http://mail.python.org/mailman/listinfo/python-list
Cleaning up after failing to contructing objects
Hello! I have been thinking about how write exception safe constructors in Python. By exception safe I mean a constructor that does not leak resources when an exception is raised within it. The following is an example of one possible way to do it: class Foo(object): def __init__(self, name, fail=False): self.name = name if not fail: print '%s.__init__(%s)' % (self.__class__.__name__, self.name) else: print '%s.__init__(%s), FAIL' % (self.__class__.__name__, self.name) raise Exception() def close(self): print '%s.close(%s)' % (self.__class__.__name__, self.name) class Bar(object): def __init__(self): try: self.a = Foo('a') self.b = Foo('b', fail=True) except: self.close() def close(self): if hasattr(self, 'a'): self.a.close() if hasattr(self, 'b'): self.b.close() bar = Bar() As you can see this is less than straight forward. Is there some kind of best practice that I'm not aware of? :.:: mattias -- http://mail.python.org/mailman/listinfo/python-list