Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100722

Re: In Python 3, how to append a nested dictionary to a shelve file with the added difficulty of using a for loop?

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: In Python 3, how to append a nested dictionary to a shelve file with the added difficulty of using a for loop?
Date 2015-12-22 12:45 +0100
Organization None
Message-ID <mailman.56.1450784776.2237.python-list@python.org> (permalink)
References <CAOA=+NtE6p4XXnWr6TsUgc-8ewtDu5Xpy5ZcnZX=p5G+3f44DA@mail.gmail.com> <n5a56r$ka9$1@ger.gmane.org> <CAOA=+NsTQdQUXNxpVN9_DUDm7e=j=_GfbchrdF2ZFwrVvgZk-A@mail.gmail.com>

Show all headers | View raw


Aaron Christensen wrote:

> Thanks for the response!  Several things you stated definitely got me
> thinking.  I really appreciate the response.  I used what you said and I
> am able to accomplish what I needed.

Perhaps it becomes clearer when you write two helper functions

def read_record(key):
    return db[key]

def write_record(key, value):
    db[key] = value

Changing a person's data then becomes

person_data = read_record("person_1")
person_data["age"] = 123
write_record("person_1", person_data)

Also, I was mostly paraphrasing

>>> help(shelve)

so you might read that, too.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: In Python 3, how to append a nested dictionary to a shelve file with the added difficulty of using a for loop? Peter Otten <__peter__@web.de> - 2015-12-22 12:45 +0100

csiph-web