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

  • SEARCH
  • Home
  • 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
  • 1 View
  • 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 batch script which copies files from the a folder
I have written a small class Dice that imitates the behavior of real dice
I have written a small util in an app that syncs the time from
I have written a small chat app that uses mysql + php to facilitate
I have written a small app that puts my bluetooth in discoverable mode for
I have written a small makefile for a few simple C programs that compiles
I have written a small XML validator, that takes in an XML file and
I have written a small program in C to get IP from remote user
I have written a small app, that has a Activity to control and display

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.