Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'syntax': 0.03; '"""': 0.05; 'things.': 0.05; '*args,': 0.07; 'arguments': 0.07; 'decorator': 0.07; 'collections': 0.09; 'positional': 0.09; 'preserve': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; 'def': 0.10; '"""a': 0.16; '**kwds)': 0.16; '**kwds):': 0.16; '__new__': 0.16; '_logger': 0.16; 'args:': 0.16; 'namedtuple': 0.16; 'namedtuples': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'time.time()': 0.16; 'url:gmane': 0.16; 'wrote:': 0.17; 'skip': 0.17; 'url:article': 0.17; 'feb': 0.19; 'import': 0.21; 'info.': 0.22; 'os,': 0.22; 'skip:_ 20': 0.22; 'seems': 0.23; 'raise': 0.24; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'logging': 0.27; 'header:X-Complaints-To:1': 0.28; 'loop,': 0.29; 'sleep': 0.29; 'workaround': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'keyword': 0.30; '(and': 0.32; 'url:python': 0.32; 'print': 0.32; 'doubt': 0.33; 'rid': 0.33; 'to:addr:python-list': 0.33; 'requirements': 0.33; 'thanks': 0.34; 'list': 0.35; 'needed': 0.35; 'adds': 0.35; 'pm,': 0.35; 'received:org': 0.36; 'really': 0.36; 'but': 0.36; 'url:org': 0.36; 'depends': 0.36; 'skip:m 40': 0.36; 'skip:p 20': 0.36; 'display': 0.36; 'turn': 0.36; 'skip:t 40': 0.37; 'does': 0.37; 'well.': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'john': 0.60; 'back': 0.62; 'time,': 0.62; 'skip:n 10': 0.63; 'more': 0.63; 'readers': 0.65; 'skip:n 30': 0.69; 'construction': 0.72; 'construct': 0.84; 'reid': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: John Reid Subject: Re: Differences creating tuples and collections.namedtuples Date: Tue, 19 Feb 2013 09:36:50 +0000 References: <7a40a426-baa9-46f8-8f9d-59ba32b044f3@googlegroups.com> <0c9a97b8-60f7-4050-aa65-1907bec75d8d@ou9g2000pbb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: cpc6-dals15-2-0-cust115.hari.cable.virginmedia.com User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 In-Reply-To: <0c9a97b8-60f7-4050-aa65-1907bec75d8d@ou9g2000pbb.googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1361266628 news.xs4all.nl 6932 [2001:888:2000:d::a6]:36203 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:39196 On 19/02/13 01:47, alex23 wrote: > On Feb 18, 9:47 pm, John Reid wrote: >> See http://article.gmane.org/gmane.comp.python.ipython.user/10270 for more info. > > One quick workaround would be to use a tuple where required and then > coerce it back to Result when needed as such: > > def sleep(secs): > import os, time, parallel_helper > start = time.time() > time.sleep(secs) > return tuple(parallel_helper.Result(os.getpid(), time.time() - > start)) > > rc = parallel.Client() > v = rc.load_balanced_view() > async_result = v.map_async(sleep, range(3, 0, -1), ordered=False) > for ar in async_result: > print parallel_helper.Result(*ar) > > You can of course skip the creation of Result in sleep and only turn > it into one in the display loop, but it all depends on additional > requirements (and adds some clarity to what is happening, I think). > Thanks all I really need is a quick work around but it is always nice to discuss these things. Also this class decorator seems to do the job for ipython although it does change the construction syntax a little and is probablty overkill. No doubt the readers of this list can improve it somewhat as well. import logging _logger = logging.getLogger(__name__) from collections import namedtuple def make_ipython_friendly(namedtuple_class): """A class decorator to make namedtuples more ipython friendly. """ _logger.debug('Making %s ipython friendly.', namedtuple_class.__name__) # Preserve original new to use if needed with keyword arguments original_new = namedtuple_class.__new__ def __new__(cls, *args, **kwds): _logger.debug('In decorator __new__, cls=%s', cls) if args: if kwds: raise TypeError('Cannot construct %s from an positional and keyword arguments.', namedtuple_class.__name__) _logger.debug('Assuming construction from an iterable.') return namedtuple_class._make(*args) else: _logger.debug('Assuming construction from keyword arguments.') return original_new(namedtuple_class, **kwds) namedtuple_class.__new__ = staticmethod(__new__) # set the class' __new__ to the new one del namedtuple_class.__getnewargs__ # get rid of getnewargs return namedtuple_class Result = make_ipython_friendly(namedtuple('Result', 'pid duration'))