[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Sebastian Kreft
On Thu, Aug 8, 2019 at 7:09 PM Andrew Barnert via Python-ideas < python-ideas@python.org> wrote: > On Aug 8, 2019, at 15:01, Ryan Fox wrote: > > I don't see why you would want to access arguments by their position. > > > Because that’s the way it’s worked since Python 1.x, and there’s tons of >

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Andrew Barnert via Python-ideas
On Aug 8, 2019, at 15:01, Ryan Fox wrote: > > I don't see why you would want to access arguments by their position. Because that’s the way it’s worked since Python 1.x, and there’s tons of existing code that expects it, including the default __str__ and __repr__ for exceptions and the code

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Richard Musil
The hash was an argument for having the means to preserve the precision (or representation) of the float (in its textual form) over decode-encode. The example was just chosen because I had experienced it in my project. But I never said that I need the underlying binary representation to be bit

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Andrew Barnert via Python-ideas
On Aug 8, 2019, at 13:43, __pete...@web.de wrote: > > You have overwritten the wrong method: Unfortunately, he hasn’t. Overriding encode isn’t particularly useful. > $ cat demo.py > import io > import json > import decimal > > class DecimalEncoder(json.JSONEncoder): >def encode(self, o): >

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Peter Otten
Richard Musil wrote: > I have found myself in an awkward situation with current (Python 3.7) JSON > module. Basically it boils down to how it handles floats. I had been hit > on this particular case: > > In [31]: float(0.6441726684570313) > Out[31]: 0.6441726684570312 > > but I guess it really

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Ryan Fox
> Your magic template has apparently turned positional-or-keyword parameters into keyword-only. This means that every exception class that you’d normally construct with positional args today (which apparently includes even your example, since that’s what you did in version 1 and 2) now requires

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread __peter__
Richard Musil wrote: > I have found myself in an awkward situation with current (Python 3.7) JSON > module. Basically it boils down to how it handles floats. I had been hit > on this particular case: > > In [31]: float(0.6441726684570313) > Out[31]: 0.6441726684570312 > > but I guess it really

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Chris Angelico
On Fri, Aug 9, 2019 at 6:31 AM Richard Musil wrote: > > Chris Angelico wrote: > > > 2) Should there be a protocol obj.__json__() to return a string > > representation of an object for direct insertion into a JSON file? > > > However, this is a much broader topic, and if you want to push for > >

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Andrew Barnert via Python-ideas
On Aug 8, 2019, at 13:29, Richard Musil wrote: > > I am not sure I have ever asked for bit-for-bit identical JSON representation. Your complaint is that the hash is different. If you don’t get bit-for-bit identical JSON representation, the hash will not be the same. So either you _are_ asking

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Richard Musil
I meant Chris (Angelico), sorry for that :(. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Richard Musil
I have not asked for means to serialize invalid JSON objects. Yes, with the "raw" output, you can create invalid JSON, but it does not mean you have to. Let's take a look at it from a different POV and focus on the original problem. Imagine this situation, I have a JSON string with this value:

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Paul Moore
OK, going back to your original question: On Thu, 8 Aug 2019 at 11:24, Richard Musil wrote: > If I use Decimal, the value is preserved, but there seems to be no way to > "serialize it back". Writing a custom serializer: > > class DecimalEncoder(json.JSONEncoder): > def default(self, o): >

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Richard Musil
Chris Angelico wrote: > 2) Should there be a protocol obj.__json__() to return a string > representation of an object for direct insertion into a JSON file? > However, this is a much broader topic, and if you want to push for > that, I would recommend starting a new thread. As Andrew pointed

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Richard Musil
The hash is calculated over the "normalized" JSON output, where "normalized" basically means stripped of all whitespaces by the "generator". This is as canonical as it gets. Then the same data are transmitted in "loose" form, i.e. with some indentation so it is humanly readable. The other party

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Andrew Barnert via Python-ideas
On Aug 8, 2019, at 08:52, Ryan Fox wrote: > > >>> class MyException(Exception): > ... def __init__(self, action, context): > ... super().__init__(f'Bad thing happened during {action} in > {context}') > >>> raise MyException(current_action, current_context) ... > Version 2 looks

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Rob Cliffe via Python-ideas
Would this work for you? import struct floatval = 0.6441726684570312 s = struct.pack("d", floatval) # converts floatval into a string (Python 2) or bytes (Python 3), of 8 raw bytes # then serialise s To be portable between different platforms, you would also need to store whether the format

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Chris Angelico
On Fri, Aug 9, 2019 at 5:06 AM Richard Musil wrote: > > I am not sure `(str(o),)` is what I want. For a comparison here are three > examples: > ``` > json:orig = {"val": 0.6441726684570313} > json:pyth = {'val': 0.6441726684570312} > json:seri = {"val": 0.6441726684570312} > > dson:orig =

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Andrew Barnert via Python-ideas
On Aug 8, 2019, at 10:33, Richard Musil wrote: > > I also did not want to discuss the design decisions about having JSON > unreliable for the hash calculation. In my app, I know I am only dealing with > integers, floats and strings (and arrays and maps), all withing the range of > a standard

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Richard Musil
I am not sure `(str(o),)` is what I want. For a comparison here are three examples: ``` json:orig = {"val": 0.6441726684570313} json:pyth = {'val': 0.6441726684570312} json:seri = {"val": 0.6441726684570312} dson:orig = {"val": 0.6441726684570313} dson:pyth = {'val':

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Ryan Fox
Thanks for the comments. I'm not really sure what you mean with regards to backwards compatibility. Would it suffice to have ExceptionTemplate.__init__ accept *args and pass that into super().__init__? I see that BaseException does something with args in __new__ and __init__, but I'm not very

[Python-ideas] Re: Exceptions with Message Templates

2019-08-08 Thread Brett Cannon
Three things. One, this isn't backwards-compatible as you are not passing any details down into Exception.__init__() to make sure that BaseException.args gets populated. Two, you can simplify your code by using str.format_map() instead of the string module. Three, I don't see enough overhead

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Richard Musil
I appreciate the responses though I feel like I stirred a completely different topic than I wanted :). I am aware of the limitations of the float binary representation, and consequently also the Python handling of that matter, and I am perfectly fine with it. This was not the point of my

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Andrew Barnert via Python-ideas
On Aug 8, 2019, at 03:22, Richard Musil wrote: > > What matters is that I did not find a way how to fix it with the standard > `json` module. I have the JSON file generated by another program (C++ code, > which uses nlohmann/json library), which serializes one of the floats to the > value

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Jonathan Fine
On Thu, Aug 8, 2019 at 4:01 PM Steven D'Aprano wrote: > In [31]: float(0.6441726684570313) > > Out[31]: 0.6441726684570312 > > The call to float() there is a no-op, because the literal 0.64417... is > already compiled to a float before the function is called. Calling > float() on a float does

[Python-ideas] Exceptions with Message Templates

2019-08-08 Thread Ryan Fox
Exception definitions in Python are not great. There are two main ways they're used in the code that I've read: 1) By far the most common: >>> class MyException(Exception): ... pass >>> raise MyException(f'Bad thing happened during {action} in {context}') 2) Much rarer: >>> class

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Steven D'Aprano
On Thu, Aug 08, 2019 at 10:22:49AM -, Richard Musil wrote: > I have found myself in an awkward situation with current (Python 3.7) > JSON module. Basically it boils down to how it handles floats. I had > been hit on this particular case: > > In [31]: float(0.6441726684570313) > Out[31]:

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Chris Angelico
On Thu, Aug 8, 2019 at 11:50 PM Rhodri James wrote: > > On 08/08/2019 11:22, Richard Musil wrote: > > I have found myself in an awkward situation with current (Python 3.7) JSON > > module. Basically it boils down to how it handles floats. I had been hit on > > this particular case: > > > > In

[Python-ideas] Re: Namespace context managers

2019-08-08 Thread Ricky Teachey
> > Forgive me if this doesn't make sense, but what about a way to make a copy > of a function's locals while it is being called? > > This can be easily accomplished if you simply return locals() at the end of creating the namespace, but this isn't all that elegant: def namespace(ns):

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Rhodri James
On 08/08/2019 11:22, Richard Musil wrote: I have found myself in an awkward situation with current (Python 3.7) JSON module. Basically it boils down to how it handles floats. I had been hit on this particular case: In [31]: float(0.6441726684570313) Out[31]: 0.6441726684570312 but I guess it

[Python-ideas] Re: Namespace context managers

2019-08-08 Thread Ricky Teachey
> > > A class + decorator gets so almost there that it might be possible > > with just a tiny bit of language support. What if there could be a way > > to change a function's __globals__? Forgive me if this doesn't make sense, but what about a way to make a copy of a function's locals while it

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Chris Angelico
On Thu, Aug 8, 2019 at 11:34 PM Ronald Oussoren via Python-ideas wrote: > > > > On 8 Aug 2019, at 12:22, Richard Musil wrote: > > I have found myself in an awkward situation with current (Python 3.7) JSON > module. Basically it boils down to how it handles floats. I had been hit on > this

[Python-ideas] Re: adding support for a "raw output" in JSON serializer

2019-08-08 Thread Ronald Oussoren via Python-ideas
> On 8 Aug 2019, at 12:22, Richard Musil wrote: > > I have found myself in an awkward situation with current (Python 3.7) JSON > module. Basically it boils down to how it handles floats. I had been hit on > this particular case: > > In [31]: float(0.6441726684570313) > Out[31]:

[Python-ideas] adding support for a "raw output" in JSON serializer

2019-08-08 Thread Richard Musil
I have found myself in an awkward situation with current (Python 3.7) JSON module. Basically it boils down to how it handles floats. I had been hit on this particular case: In [31]: float(0.6441726684570313) Out[31]: 0.6441726684570312 but I guess it really does not matter. What matters is

[Python-ideas] Stdlib module - Draft

2019-08-08 Thread Abdur-Rahmaan Janhangeer
Greetings, Here is a draft after the stdlib module discussion . Abstract === Proposal: Adding a stdlib module to the standard library Defining the stdlib == Would the