Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4108828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:42:39+00:00 2026-05-20T21:42:39+00:00

I have written a small wrapper for the dict built-in class that loads entries

  • 0

I have written a small wrapper for the dict built-in class that loads entries (values) of a dictionary from cPickled files as soon as the respective key is first accessed. When the dictionary is destroyed all loaded entries are written back to disk.

Now it would be convenient if I could check if any of the values has been changed and write out only those that in fact have been. My question therefore is: Does a dictionary know if a value has been changed? Or is there a clever way to transparently implement this?

For completeness I attach the code I use. It’s called with the path where the files are stored (keys are used as filenames) and with a list of keys for which files exist.

import cPickle

class DictDB(dict):

  def __init__(self, path, folders):
    self.picklepath     = path    # path to files on disk
    self.folders        = folders # available folders
    self.loaded_folders = {}

  def has_key(self, key):
    return key in self.folders

  def get(self, key):
    if not key in self.loaded_folders.keys():
      if not key in self.folders:
        raise KeyError("Folder "+key+" not available")
      # load from disk
      self.loaded_folders[key] = cPickle.load(file(self.picklepath + key + ".cpickle2"))
    return self.loaded_folders[key]

  def __getitem__(self, key):
    return self.get(key)

  def close(self):
    for folder in self.loaded_folders.keys():
      # write back
      cPickle.dump(self.loaded_folders[folder], file(picklepath + folder + '.cpickle2', 'w'), 2)

  def __del__(self):
    self.close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T21:42:40+00:00Added an answer on May 20, 2026 at 9:42 pm

    I might approach it with a sort of publish-subscribe model, where the containing dictionary subscribes to each of the sub dictionaries (or other values). Then when one of them is edited, it notifies any dictionaries that contain it.

    If you don’t want them all deal with the wiring for that and are willing to allow the containing dictionary to only check for changes on access or at set intervals, you can have each contained object keep track of a version number. Then when the containing dictionary is ready, it simply checks to see if that version number has changed.

    A final possibility would be to have a way of reliably calculating hash values for contained objects in place. This would let you write an external function and remove the need for the objects to track their own versions, but has its own complexities since you’ll either need to overload __hash__ on all of them or write another form of the hash() function that can identify the object and take some sort of intelligent hashed value from it

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small command-line application written in C that acts as a wrapper/launcher
I have written a small util in an app that syncs the time from
I have a small AJAX application, written in PHP that I did not secure
I have written an AIR Application that downloads videos and documents from a server.
I have written a small PHP file that gets the information that was posted
I have written a small android app that grabs some data and displays it
I have written a small program and server to upload large files over HTTP
I have written a small jquery application that allows a person to slide a
I have written a small little script that looks up a work on wikipedia
I have written a small java application for which I need to obtain performance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.