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 8015025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:07:45+00:00 2026-06-04T20:07:45+00:00

I’m trying to improve (if possible) upon the DataContext class below or find alternate

  • 0

I’m trying to improve (if possible) upon the DataContext class below or find alternate solutions:

class Store(object):

    def __init__( self, contents=None):
        self.contents = contents

class DataContext(object):

    def __init__( self, datastore ) : # datastore is of type store
        self.store = datastore

    def __enter__( self ) :
        self.temp_store = copy.copy( self.store.contents )  # Improve upon this!

    def __exit__( self, type, value, traceback ) :
        self.store.contents = self.temp_store

Example usage:

data = Store( [1,2,3] )
print "Before context: ", data.contents
with DataContext( data ):
    data.contents.append( 4 )         # Tampering with the data 
    print "Within context: ", data.contents

print "Outside context: ", data.contents

Output:

Before context:  [1, 2, 3]
Within context:  [1, 2, 3, 4]
Outside context:  [1, 2, 3]

The copy can be expensive for large data structures. What would be (or is there) a clean way to only store changes to the data structure inside the context and then undo those specific changes to data during exit?

  • 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-06-04T20:07:48+00:00Added an answer on June 4, 2026 at 8:07 pm

    You could use some sort of checkpointing. For instance, you could keep a separate structure where you would insert new elements. When you exited the context, you would need to remove these new elements from the container.

    Clearly, whether this is worth compared to doing a deep cooping will depend on several factors such as:

    1. The underlying container. Not all of them have the same API, so you will need to specialize the checkpointing mechanism for the different supported container types (and that may not be trivial).
    2. What is the ratio of elements before entering the context vs the number of elements inserted while inside the context.

    Moreover, in terms of correctness the type of container also affects the design of such a mechanism. For instance, if the container is a list you would need to remove all the elements that you inserted while in the context. On the other hand, if the container is a set, you would only need to remove them if they were not initially present.

    For simplicity, let’s assume you are only interested in lists and just supporting append operation, then a possible solution is actually very easy:

    class DataContext(object):

    def __init__( self, datastore ) : # datastore is of type store
        self.store = datastore
        self.num_added = 0
    
    def __enter__( self ) :
        pass
    
    def __exit__( self, type, value, traceback ) :
        l = len(self.store.contents)
        del self.store.contents[l - self.num_added : l]
    
    def append( self, elem ) :
        self.store.contents.append(elem)
        self.num_added += 1
    

    Definitely this is a very simple case, and just for adding support for removing elements from anywhere in the list, you will need to keep some sort of log of the operations performed on the list. The log could be a list of entries specifying the type of operation (insertion, deletion) and its arguments (e.g., index, data). Moreover, you will need to use a proxy or wrapper to intercept every modifying operation on the list (e.g., append, extend, insert, remove, etc.) if you want to give support for all the list operations.

    If you want to go even more generic and support not only lists but other types of containers, then you would need wrappers for the operations of the different containers. For instance, for set you would need to support add, remove, update, intersection_update, etc. To do that you could create a class hierarchy descending from DataContext with the specialized operations for each type.

    Anyway, as you can see, the complexity to implement this grows significantly depending on how much generic you want to be. So, probably if you just want a specific solution tailored for lists, it may pay off to implement it, otherwise it may just be better to pay the price of performing a copy of the container.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.