Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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; 'python.': 0.02; 'syntax': 0.04; 'classes,': 0.05; 'encoding': 0.05; 'mrab': 0.05; 'output': 0.05; 'say,': 0.05; '(so': 0.07; 'encoded': 0.07; 'json': 0.07; '[0,': 0.09; '[1,': 0.09; 'arguments': 0.09; 'delimited': 0.09; 'encode': 0.09; 'encoder': 0.09; 'here?': 0.09; 'occasionally': 0.09; 'second.': 0.09; 'subject:using': 0.09; 'subset': 0.09; 'subtle': 0.09; 'trailing': 0.09; 'python': 0.11; 'def': 0.12; "wouldn't": 0.14; "'set',": 0.16; '(0,': 0.16; '2):': 0.16; ':-(': 0.16; 'advocating': 0.16; 'ah,': 0.16; 'arguments:': 0.16; 'bracket': 0.16; 'brackets': 0.16; 'called,': 0.16; 'comma': 0.16; 'comma,': 0.16; 'comma.': 0.16; 'delimiter': 0.16; 'dict': 0.16; 'fine.': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'interpreter,': 0.16; 'it),': 0.16; 'json,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'notation': 0.16; 'omitted.': 0.16; 'one-element': 0.16; 'parentheses': 0.16; 'parentheses:': 0.16; 'pity': 0.16; 'plop': 0.16; 'positional': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'repl': 0.16; 'set:': 0.16; 'superfluous': 0.16; 'syntax,': 0.16; 'tremendous': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'tuple.': 0.16; 'tuples,': 0.16; 'typos': 0.16; '(0)': 0.16; 'followed': 0.16; 'prevent': 0.16; 'wrote:': 0.18; '(not': 0.18; 'passing': 0.19; 'thu,': 0.19; 'seems': 0.21; 'header:User-Agent:1': 0.23; "aren't": 0.24; 'issue,': 0.24; 'parse': 0.24; '(or': 0.24; 'equivalent': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'chris': 0.29; 'am,': 0.29; "doesn't": 0.30; 'list:': 0.30; 'sets': 0.30; 'start,': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'went': 0.31; "skip:' 10": 0.31; 'that.': 0.31; 'convenience': 0.31; 'correctly.': 0.31; 'int,': 0.31; 'object.': 0.31; 'sep': 0.31; 'sets.': 0.31; "they'll": 0.31; 'tuples': 0.31; 'types.': 0.31; 'there,': 0.34; "i'd": 0.34; 'could': 0.34; 'something': 0.35; 'case,': 0.35; 'received:84': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'format.': 0.36; 'keyword': 0.36; 'example,': 0.37; 'list': 0.37; 'handle': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'though,': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'even': 0.60; 'problems.': 0.60; 'name:': 0.61; 'first': 0.61; 'making': 0.63; 'name': 0.63; 'more': 0.64; 'different': 0.65; 'worth': 0.66; 'between': 0.67; 'default': 0.69; 'hoping': 0.75; 'calm': 0.84; "class's": 0.84; 'collision': 0.84; 'confusion.': 0.84; 'float,': 0.84; "it'd": 0.84; 'pike': 0.84; 'afford': 0.91; 'good,': 0.91; 'whereas': 0.91; 'yes!': 0.91; 'anywhere,': 0.93; 'suited': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=S6lXwecP c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=8GIvbBF9nK0A:10 a=MQH2_SA4b-8A:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=25DJcswZqZSGcy_3nI0A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Thu, 04 Sep 2014 12:07:14 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Storing instances using jsonpickle References: <46e782a5-b087-4f95-aadb-26e233bf5419@googlegroups.com> <5407A69B.3030707@mrabarnett.plus.com> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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: 145 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409828837 news.xs4all.nl 2975 [2001:888:2000:d::a6]:59879 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77541 On 2014-09-04 06:17, Chris Angelico wrote:> On Thu, Sep 4, 2014 at 9:39 AM, MRAB wrote: >> I occasionally think about a superset of JSON, called, say, "pyson" >> ... ah, name already taken! :-( > > While I'm somewhat sympathetic to the concept, there are some parts > of your description that I disagree with. Am I misreading something? > Are there typos in the description and I'm making something out of > nothing? > >> It would add tuples, delimited by (...), which are not used >> otherwise (no expressions): >> >> () => () >> (0, ) => (0) > I'm thinking now that it might be better to have 'tuple()' and 'tuple(0)'. > This seems odd. Part of JSON's convenience is that it's a subset of > JavaScript syntax, so you can just plop a block of JSON into a REPL > and it'll decode correctly. With PyON (or whatever you call it), it'd > be nice to have the same correspondence; for a start, I would > strongly> encourage the "trailing comma is permitted" rule (so > [1,2,3,] is equivalent to [1,2,3]), and then I'd have the default > encoding for a single-element tuple include that trailing comma. If > (0) is a one-element tuple, you end up with a subtle difference > between a PyON decode and the Python interpreter, which is likely to > cause problems. It might even be worth actually mandating (not just > encouraging) that one-element tuples have the trailing comma, just to > prevent that. > In that case, if you wanted to encode a (0, ), it would have to be 'tuple([0])', whereas 1+2j would have to be 'complex(i, 2)'. The encoder would need to return [[0]] for the first case and [1, 2] for the second. >> The key of a dict could also be int, float, or tuple. > > Yes! Yes! DEFINITELY do this!! Ahem. Calm down a little, it's not > that outlandish an idea... > >> It could support other classes, and could handle named arguments. >> >> For example, sets: >> >> To encode {0, 1, 2): > > Do you mean {0, 1, 2} here? I'm hoping you aren't advocating a syntax > that mismatches bracket types. That's only going to cause confusion. > Yes, that's a typo. >> Look in encoder dict for encoder function with class's name >> ('set') and call it, passing object. >> >> Encoder returns positional and keyword arguments: [0, 1, 2] and >> {}. >> >> Output name, followed by encoded arguments in parentheses. >> >> Encoder for set: >> >> def encode_set(obj): >> return list(obj), {} >> >> To decode 'set(0, 1, 2)': >> >> Parse name: 'set'. >> >> Parse contents of parentheses: [0, 1, 2] and {}. >> >> Look in decoder dict for decoder function with given name >> ('set') and call it, passing arguments. >> >> Result would be {0, 1, 2}. >> >> Decoder for set: >> >> def decode_set(*args): >> return set(args) >> >> pyson.dumps({0, 1, 2}, decoders={'set': decode_set}) would return >> 'set(0, 1, 2)'. >> >> pyson.loads('set(0, 1, 2)', encoders={'set': encode_set}) would >> return {0, 1, 2}. > > This seems very much overengineered. Keep it much more simple; adding > set notation is well and good, but keyword arguments aren't necessary > there, and I'm not seeing a tremendous use-case for them. > > It's a pity Python has the collision of sets and dicts both using > braces. Pike went for two-character delimiters, which might be better > suited here; round brackets aren't used in JSON anywhere, so you can > afford to steal them: > > {'this':'is', 'a':'dict'} > ({'this','is','a','set'}) > > Empty sets would be an issue, though, as they'll be different in > Python and this format. But everything else would work fine. You have > a two-character delimiter in PyON, and superfluous parentheses around > set notation in Python. > > (Sadly, this doesn't make it Pike-compatible, as Pike uses () > for sets. But it wouldn't have been anyway.) > To encode {0, 1, 2}: Look in encoder dict for encoder function with class's name ('set') and call it, passing object. Encoder returns name and list of arguments: 'set' and [[0, 1, 2]]. Output name, followed by encoded arguments in parentheses: 'set([0, 1, 2])'. Encoder for set: def encode_set(obj): return 'set', [list(obj)] To decode 'set([0, 1, 2])': Parse name: 'set'. Parse contents of parentheses as list: [[0, 1, 2]]. Look in decoder dict for decoder function with given name ('set') and call it, passing arguments. Result would be {0, 1, 2}. Decoder for set: def decode_set(args): # Error-checking omitted. return set(args[0]) pyson.dumps({0, 1, 2}, decoders={'set': decode_set}) would return 'set([0, 1, 2])'. pyson.loads('set([0, 1, 2])', encoders={'set': encode_set}) would return {0, 1, 2}.