Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!ecngs!feeder2.ecngs.de!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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; 'classes,': 0.05; 'output': 0.05; 'say,': 0.05; 'encoded': 0.07; 'json': 0.07; 'suppose': 0.07; 'wednesday,': 0.07; '[0,': 0.09; '__init__': 0.09; 'arguments': 0.09; 'attributes': 0.09; 'defines': 0.09; 'delimited': 0.09; 'encode': 0.09; 'encoder': 0.09; 'occasionally': 0.09; 'run,': 0.09; 'subject:using': 0.09; 'def': 0.12; '(0,': 0.16; '2):': 0.16; ':-(': 0.16; 'ah,': 0.16; 'arguments:': 0.16; 'called,': 0.16; 'created.': 0.16; 'dict': 0.16; 'dictionary.': 0.16; 'encoders': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'json,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'objects.': 0.16; 'parentheses:': 0.16; 'positional': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'set:': 0.16; 'string).': 0.16; 'summary,': 0.16; 'tuple.': 0.16; 'tuples,': 0.16; '(0)': 0.16; 'followed': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'passing': 0.19; 'work,': 0.20; '(the': 0.22; 'creating': 0.23; 'header:User-Agent:1': 0.23; 'parse': 0.24; 'string,': 0.24; 'looks': 0.24; 'sort': 0.25; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'implicit': 0.31; 'int,': 0.31; 'invoke': 0.31; 'josh': 0.31; 'object.': 0.31; 'pickle': 0.31; 'this.': 0.32; 'another': 0.32; 'could': 0.34; 'something': 0.35; 'received:84': 0.35; 'but': 0.35; 'add': 0.35; 'keyword': 0.36; 'method': 0.36; 'should': 0.36; 'example,': 0.37; 'list': 0.37; 'being': 0.38; 'handle': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'structure': 0.39; 'to:addr:python.org': 0.39; 'days': 0.60; 'ago.': 0.61; 'new': 0.61; 'name:': 0.61; 'name': 0.63; 'frustrated': 0.68; 'batchelder': 0.84; "class's": 0.84; 'float,': 0.84 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=vf5ZprvwxAXaFXEfmb8A:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett:2500 Date: Thu, 04 Sep 2014 00:39:07 +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> In-Reply-To: <46e782a5-b087-4f95-aadb-26e233bf5419@googlegroups.com> 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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1409787549 news.xs4all.nl 2972 [2001:888:2000:d::a6]:42112 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:77513 On 2014-09-03 23:30, Josh English wrote: > On Wednesday, September 3, 2014 1:53:23 PM UTC-7, Ned Batchelder > wrote: > >> Pickle (and it looks like jsonpickle) does not invoke the class' >> __init__ method when it reconstitutes objects. Your new __init__ >> is not being run, so new attributes it defines are not being >> created. >> >> This is one of the reasons that people avoid pickle: being >> completely implicit is very handy, but also fragile. >> > > I seem to remember having this exact same frustration when I used > pickle and shelve 15 years ago. I had hoped to have another way > around this. I spent over a decade trying to make an XML-based > database work, in part because of this limitation. > > Some days I get so frustrated I think the only data structure I > should ever use is a dictionary. > > I suppose to make this sort of thing work, I should look at creating > custom json encoders and decoders. > I occasionally think about a superset of JSON, called, say, "pyson" ... ah, name already taken! :-( In summary, it would be something like this: JSON supports int, float, string, None (as 'null'), list and dict (the key must be string). It would add tuples, delimited by (...), which are not used otherwise (no expressions): () => () (0, ) => (0) (0, 1) => (0, 1) The key of a dict could also be int, float, or tuple. It could support other classes, and could handle named arguments. For example, sets: To encode {0, 1, 2): 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}.