Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #61003
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'programmer': 0.03; 'string.': 0.05; 'affected': 0.07; 'json': 0.07; 'problem:': 0.07; 'reason,': 0.07; 'string': 0.09; 'conversions': 0.09; 'objects,': 0.09; 'postgresql,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'seemed': 0.09; 'testing,': 0.09; 'type,': 0.09; 'python': 0.11; 'stored': 0.12; 'changes': 0.15; 'cleaner': 0.16; 'columns': 0.16; 'equal.': 0.16; 'integer,': 0.16; 'preserve': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'spotted': 0.16; 'types,': 0.16; 'underlying': 0.16; 'so.': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'basically': 0.19; 'server,': 0.19; 'value.': 0.19; 'written': 0.21; 'seems': 0.21; 'handles': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'error': 0.23; 'string,': 0.24; 'url:dev': 0.24; 'question': 0.24; 'changes,': 0.26; 'compare': 0.26; 'updating': 0.26; 'certain': 0.27; 'header:X-Complaints- To:1': 0.27; 'point': 0.28; 'correct': 0.29; 'xml': 0.29; 'change,': 0.30; 'database,': 0.30; 'waste': 0.30; 'program,': 0.31; 'fine,': 0.31; 'fixing': 0.31; 'ok.': 0.31; 'types.': 0.31; 'url:python': 0.33; 'ago': 0.33; 'cases': 0.33; 'noticed': 0.34; 'could': 0.34; 'convert': 0.35; 'objects': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'object,': 0.36; 'done': 0.36; 'url:org': 0.36; 'should': 0.36; 'two': 0.37; 'list.': 0.37; 'being': 0.38; 'url:library': 0.38; 'handle': 0.38; 'to:addr :python-list': 0.38; 'list,': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'how': 0.40; 'days': 0.60; 'eventually': 0.60; 'hope': 0.61; 'free': 0.61; 'conversion': 0.61; 'new': 0.61; 'back': 0.62; 'decided': 0.64; 'places': 0.64; 'telling': 0.64; 'provide': 0.64; 'more': 0.64; 'account': 0.65; 'life': 0.66; 'hours': 0.66; 'direct': 0.67; 'between': 0.67; 'date,': 0.68; 'frank': 0.68; 'saving': 0.69; 'sharing': 0.69; 'business': 0.70; 'felt': 0.74; 'obvious': 0.74; 'transfer': 0.82; 'etc,': 0.84; 'yet?': 0.84; 'mistake': 0.91; 'same,': 0.91; 'saving,': 0.91; 'url:latest': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: A cautionary tale |
| Date | Wed, 04 Dec 2013 11:08:23 +0100 |
| Organization | None |
| References | <l7mrt7$cme$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084b2f9.dip0.t-ipconnect.de |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3551.1386151711.18130.python-list@python.org> (permalink) |
| Lines | 76 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1386151711 news.xs4all.nl 2972 [2001:888:2000:d::a6]:45162 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:61003 |
Show key headers only | View raw
Frank Millman wrote: > Hi all > > There is no question at the end of this, it is just an account of a couple > of days in the life of a programmer (me). I just felt like sharing it. > Feel free to ignore. > > The business/accounting system I am writing involves a lot of reading data > from a database, and if changed, writing it back again. > > There are a number of data types involved - string, integer, decimal, > boolean, date, datetime. I currently support PostgreSQL, MS SQL Server, > and sqlite3. In all cases, they have a DB-API 2.0-compliant adaptor which > handles the conversion to and from python objects transparently. > > Over the last year or so I have added two new types. I added a JSON type, > to handle 'lists' and 'dicts', and an XML type to handle more complex > structures. In both cases they are stored in the database as strings, so I > have to handle the conversions myself. > > I don't allow direct access to the objects, as they can be affected by > various business rules, so I use getters and setters. For the new types, I > used the getter to convert from the string to the underlying object, and > the setter to convert it back to a string. > > Then a couple of days ago I decided that this was not the correct place to > do it - it should be done when reading from and writing to the database. > That way the data is always represented by the underlying object, which > can be passed around without worrying about conversions. > > It was a bit of effort, as I had to add extra getters and setters to > handle the transfer between the database and the program, and then > over-ride them in the case of the new data types to provide the required > functionality. But after a few hours of hunting down all the places that > required changes, testing, fixing errors, etc, it seemed to be working > fine, so I thought I could carry on with the meat of my program. > > Then I noticed that certain changes were not being written back to the > database. After some investigation, I found the error in a part of my > program that I have not had to look at for ages. When reading data in from > the database, I preserve a copy of the original value. When saving, I > compare that to the current value when deciding which columns need > updating. I do this in the obvious way - > > on reading - > orig_value = value > > on saving - > if value != orig_value: > this one needs updating > > Have you spotted the deliberate mistake yet? In the case of a JSON list, > orig_value and value point to the same, mutable, list. So when I compare > value with orig_value, they are always the same, whether changes have been > made or not! > > The obvious answer is to store a copy of the list. It was not so obvious > where to make the change, as there were other implications. Eventually I > decided to over-ride the 'getter' for the JSON type, and return > copy(value) instead of value. That way if it is changed and then put back > using the 'setter', the two objects are no longer equal. I have made that > change, done some more testing, and for now it seems ok. > > So have the last couple of days been a waste of time? I don't think so. Is > the program a bit cleaner and conceptually sounder? I hope so. > > Why am I telling you all this? No particular reason, just thought some of > you might find it interesting. You might like to know how others cope with basically the same problem: http://docs.python.org/dev/library/shelve.html#example http://www.zodb.org/en/latest/documentation/tutorial.html#persistent-objects
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: A cautionary tale Peter Otten <__peter__@web.de> - 2013-12-04 11:08 +0100
csiph-web