[Python-ideas] 回复:Python-ideas Digest, Vol 146, Issue 13

Moon丶sun uamr567 at sina.com
Sun Jan 6 00:12:39 EST 2019


Thanks for your reply.But the answer is not I except, I will show you some examples to explain what result I except:
@contextmanagerdef cm():    print('open file')    yield    print('close file')with cm():    1/0
If I use a contextmanager ,I except it can help me to close the file anytime,even raise an error,but if I define a function with @contextmanager like the example which I have showed for you, it will never print('close file')
I can only modify it like this:@contextmanagerdef cm():    try:        print('open file')        yield    except Exception as e:        print('Error',e)    finally:        print('close file')
It is not friendly for us to use it, so I modify the contextlib to fix it,you can catch it from the e-mail attachment.It's in the line 79 and line 97----------------------------------------------------------------------
发件人:python-ideas-request at python.org
收件人:python-ideas at python.org
主题:Python-ideas Digest, Vol 146, Issue 13
日期:2019年01月06日 01点05分


Send Python-ideas mailing list submissions to
	python-ideas at python.org
To subscribe or unsubscribe via the World Wide Web, visit
	https://mail.python.org/mailman/listinfo/python-ideas
or, via email, send a message with subject or body 'help' to
	python-ideas-request at python.org
You can reach the person managing the list at
	python-ideas-owner at python.org
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Python-ideas digest..."
Today's Topics:
   1. Re: Make the @contextmanager of contextlib to be a real
      contextmanager (Serhiy Storchaka)
   2. Re: Fixed point format for numbers with locale based
      separators (?ukasz Stelmach)
----------------------------------------------------------------------
Message: 1
Date: Sat, 5 Jan 2019 16:45:34 +0200
From: Serhiy Storchaka <storchaka at gmail.com>
To: python-ideas at python.org
Subject: Re: [Python-ideas] Make the @contextmanager of contextlib to
	be a real contextmanager
Message-ID: <q0qfqd$q1q$1 at blaine.gmane.org>
Content-Type: text/plain; charset=UTF-8; format=flowed
05.01.19 14:52, Moon?sun ????:
> As we know,when we import the module--'contextlib',we can use the 
> decorator '@contextmanager' and keyword ?yield? to make a 'instance' of 
> Class '_GeneratorContextManager' in 'contextlib' module,then we can use 
> it like:
> with 'instance' as 'xx':
>  ? ? 'code block'
>  ? ? pass
> But there is a little bug,when the code block raise a error,the instance 
> cannot run the code which after the keyword 'yield'.
This is not a bug.
Consider the following example:
     @contextmanager
     def cm():
         try:
             yield
         except BaseException as err:
             print('Fail:', err)
             raise
         else:
             print('Success')
     with cm():
         1/0
What result would you expect? Test it with the stdlib implementation and 
with your implementation.
------------------------------
Message: 2
Date: Sat, 05 Jan 2019 16:41:20 +0100
From: ?ukasz Stelmach <steelman at post.pl>
To: Steven D'Aprano <steve at pearwood.info>
Cc: python-ideas at python.org
Subject: Re: [Python-ideas] Fixed point format for numbers with locale
	based separators
Message-ID: <87y37z9knz.fsf%steelman at post.pl>
Content-Type: text/plain; charset="utf-8"
Steven D'Aprano <steve at pearwood.info> writes:
> On Fri, Jan 04, 2019 at 03:57:53PM +0100, ?ukasz Stelmach wrote:
>> Hi,
>> 
>> I would like to present two pull requests[1][2] implementing fixed point
>> presentation of numbers and ask for comments. The first is mine. I
>> learnt about the second after publishing mine.
>
> Before I look at the implementation, can you explain the functional 
> requirements please?
As I stated in the original message below the table:
>> In the application I want to create I am going to present users numbers
>> ranging up to 3 orders of magnitude and I (my users) want them to be
>> presented consistently with regards to number of decimal digits AND I
>> want to conform to rules of languages of my users. And I would like to
>> avoid the exponent notation by all means.
The pint[1] library I use, implements formatting of physical quantities
using the format()/__format__ code. As far as I can tell my patch for
Python is shorter and more straightforward than a patch for pint to use
locale.format().
Because the "g" based "n" formatter has been present since the advanced
string formatting was described in PEP-3101, I think it is necessary to
add the "m" formatter based on "f". The advanced string formatting
facility in Python is very convenient and programmers shouldn't forced
to use locale.format() like this
    "The total length of {} sticks is {} meters.".format(n_sticks, locale.format(".2f", l_sticks))
instead of
    "The total length of {} sticks is {:.2f} meters.".format(n_sticks, l_sticks)
> In other words, what is the new feature you hope to have excepted? 
> Explain the intention and the API (the interface). The implementation is 
> the least important part :-)
I wish to add a new formatter "m" for float/complex/decimal numbers,
which behaves like the existing "f", but uses the decimal separator from
the locale database. There is "n" formmatter which behaves like "g" but
it does not fit my needs.
> [...]
>> Formatting 1.23456789 * n (LC_ALL=3Dpl_PL.UTF-8)
>>     | n |    ".2f" |    ".3n" |
>>     |---+----------+----------|
>>     | 1 |     1.23 |     1,23 |
>>     | 2 |    12.35 |     12,3 |
>>     | 3 |   123.46 |      123 |
>>     | 4 |  1234.57 | 1,23e+03 |
>
> I'm afraid I cannot work out what that table means. You say "Formatting 
> 1.23... * n" (multiplying by n) but the results shown aren't multiplied 
> by n=2, n=3, n=4 as the table suggests.
>
> Can you show what Python code you expect will produce the expected 
> output?
    for n in range(1,5): print("| {} | {:8.2f} | {:8.3n} |".format(n,1.23456789 * 10**n, 1.23456789 * 10**n))
[1] http://pint.readthedocs.io/
-- 
By?o mi bardzo mi?o.                                  --- Rurku. --- ...
>?ukasz<                                --- To dobrze, ?e mnie s?uchasz.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 617 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190105/9a2fc15d/attachment-0001.sig>
------------------------------
Subject: Digest Footer
_______________________________________________
Python-ideas mailing list
Python-ideas at python.org
https://mail.python.org/mailman/listinfo/python-ideas
------------------------------
End of Python-ideas Digest, Vol 146, Issue 13
*********************************************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190106/c346d316/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: _contextlib.py
Type: text/x-python
Size: 14088 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20190106/c346d316/attachment-0001.py>


More information about the Python-ideas mailing list