alternative to with statement?

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Feb 29 01:30:16 CET 2012


Craig Yoshioka wrote:
>It is a bit non-normal.  but I think this is a good use case as I want to create a very simple-to-use system for non-python experts to safely wrap their CLI programs in a caching architecture... that's why I lament the inability to not use the more streamlined 'with' syntax-  abusing the for loop might just be confusing.  The with statement is also a good fit because the caching strategy does have to atomically acquire, create and release the appropriate locks.  With this statement the cached CLI wrappers can be called from simultaneously from different scripts and still coordinate their activity, by waiting for each other to finish, and reusing the cached results, etc.

> with cachingcontext(x):
>    # create cached resources here
> # return cached resources

On a separate note, how would you even return the cached resources without using the following ?
with cachingcontext(x) as blah:

The only thing cachingcontext seems to do is catch errors and since cachingcontext would be used by the "non-python experts",
 I have trouble understanding how this would help them any more than: 
try:
    # create cached resources here
except SomeError:
    # handle error


If you return the cached resources outside the caching context than how could you possibly release it 
via the context manager? It seems like you want some kind of resource pooling but if that is the 
case then just write your own pool.
See http://stackoverflow.com/questions/1514120/python-implementation-of-the-object-pool-design-pattern .

Sorry if that does not help, but I do not comprehend what you are trying to do. It seems inherently
conflicted to say you want to use "with" because it lets you atomically open, use, then close resources
but you do NOT want to atomically open, use, then close resources...


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--



This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list