with ignored
I was recently watching that Raymond Hettinger video on creating Beautiful Python from this years PyCon. He mentioned pushing up the new idiom with ignored(): # do some work I tracked down his commit here http://hg.python.org/cpython/rev/406b47c64480 But am unsure how the yield works in the given situation. I know about creating generators with yield and have read the docs on how it maintains state. I think it works because it is returning control back to the caller while maintaining the try so if the caller throws it is caught by the context. Is this correct? I would love an in depth explanation of how this is working. I am trying to learn as much as possible about the actual python internals. Thanks in advance! -Barrett -- http://mail.python.org/mailman/listinfo/python-list
Re: with ignored
> > However, ignored() is actually implemented as a generator function > with the @contextmanager decorator shortcut. This decorator takes a > generator function and wraps it up as a class with the necessary > __enter__ and __exit__ methods. The __enter__ method in this case > calls the .next() method of the generator and returns after it yields > once. I looked up the source to the decorator found here:http://hg.python.org/cpython/file/406b47c64480/Lib/contextlib.py for anyone interested. It appears that they are using yield so that they can call it again and force it to throw the stop iteration exception. > The __exit__ method calls it again -- or it calls the .throw() > method if an exception was passed in -- and expects the generator to > exit as a result. > > And here to go with what I said above, they want to be able to use the generators throw ability to capture any exception and throw it to the generator, but then why do they *need* the generator to not iterate again? Or in other words, why when they create the context manager via _GeneratorContextManager on line 33, is there the clause in __exit__ to have stop iteration happen or else throw a runtime error? I am thinking this is because it needs the yield to be over so that it can return the control flow to normal, and that subroutine will be done. But I am new to the whole subroutine paradigm so I could be misunderstanding this. Also, if the value was none, why are they instantiating a new exception with value = type()? How could there be a type for the exception without the exception? (From what I understand type is the error type, and value is the error object). So from the perspective of the generator it does its context setup (in > this case, setting up a try block) prior to the yield, and then does > the cleanup (in this case, selectively catching and suppressing the > exceptions) after the yield. > This part makes sense logically. And I like the simplicity. Now I just need to make sure I understand how the yield semantics are allowing this construct to work. Thanks for the reply Ian! > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Formatting lost in hg-web (was Re: with ignored)
I am viewing it on Chrome Version 26.0.1410.43 m for windows and it works perfectly for me. On Mon, Apr 8, 2013 at 12:32 AM, Chris Angelico wrote: > On Mon, Apr 8, 2013 at 4:38 PM, Barrett Lewis > wrote: > > I looked up the source to the decorator > > found here: > http://hg.python.org/cpython/file/406b47c64480/Lib/contextlib.py > > for anyone interested. > > Strangely, line indentation seems to be swallowed in the web view of > the Mercurial tree. The code directly follows the line numbers, so it > goes ragged between (eg) lines 9 and 10. Is this a browser bug? I > tried on Chrome 26.0.1410.40 and Firefox 19.0.2 on Windows. > > ChrisA > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: How to do a Lispy-esque read?
> For example, if the input stream contained the text: > [1, # python should ignore this comment > 2] > > and I do a "read" on it, I should obtain the result > [1, 2] > -- > I don't know much about lisp but given that input and the desired output you can write functions like the following def strtolist(l): if l.startswith('['): l = l[1:] if l.endswith(']'): l = l[:-1] # treat newlines as if they are commas so we can easily split l = l.replace('\n', ',').split(',') # list comprehension # strip to remove whitespace and aggregate all elements without the comment # you can further refine this to create ints by using the int() method return [x for x in l if not x.strip().startswith('#')] you would have to use input() to read the input or open a file. Either way you can pass that value to strtolist and it will convert it to a list of strings. However, this implementation is not very robust and doesn't cover a lot of edge cases (more a demo of how it might be done, I don't know your python experience so forgive me if this is all self evident). The real solution that I would use would be to use the json module. Docs: http://docs.python.org/3.3/library/json.html It allows you to take a string and turn it into a native dict or list in python. The great part is that it is fairly robust and it has a simple interface. You could take input() and send it to loads and it will return an array. The downside is json doesn't allow comments. If you really want comments, you could look into some of the yaml libraries, a quick google search shoes PyYaml is out there. I however don't have any knowledge of that module or other yaml modules as I find json is enough for anything I've ever attempted. I hope that helps -- http://mail.python.org/mailman/listinfo/python-list
Re: help needed
Do you happen to be on windows? Because if you are then you need to edit the registry. If you are on windows let me know and I will walk you through the fix, but if not then it would be a waste of time for me to explain it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Process tuple contents on the fly
> d = {} > for key, d[key] in (("this",18), ("that",17), ("other",38)): > print key > do_something(d) > Why not use a dict comprehension? d = {k:v for k,v in (("this",18), ("that",17), ("other",38))} I feel this is more straightforward and easier to read. the results are the same however. -- http://mail.python.org/mailman/listinfo/python-list
Re: Process tuple contents on the fly
> In the particular case I did it in, I needed the incremental results > passed to a function, not just the final result. I don't think this > made it into the final code, rather it was expanded to be more > readable. But the discovery made me feel a disturbance in the > Pythonic force of the universe. :*) > > -tkc > I see what you are saying, but I agree that is a disturbance I didn't even know you could do for key, d[key], that just feels like bad news. however for what you are saying it makes sense. TIL that you can use an unpacked value during unpacking! -- http://mail.python.org/mailman/listinfo/python-list