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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:43:59+00:00 2026-05-24T20:43:59+00:00

I’ve encountered a situation where I’m working over a piece of code where I

  • 0

I’ve encountered a situation where I’m working over a piece of code where I command changes on a remote object (that is one I can’t duplicate to work over a clone), then ask the remote object for some operation in the new state and revert all the changes I made to it by a sequence of opposite commands.
The problem is that if in the middle of all these changes I encounter an error, I want to be able to roll-back all the changes I made so far.

The best fitting solution that came to my mind is the python try-finally workflow, but it’s rather problematic when the sequence of commands is long:

try:
    # perform action
    try:
        # perform action
        try: 
            # ...
        finally:
            # unroll
    finally:
        # unroll
finally:
    # unroll

This way, the more commands I need the deeper my indentation and nesting goes and the less my code is readable.
I’ve considered some other solutions such as maintaining a stack where for every command I push a rollback action, but this could get rather complicated, and I dislike pushing bound methods into stacks.
I’ve also considered incrementing a counter for every action I perform then in a single finally decide on the kind of rollback I want depending on the counter, but again, the maintainability of such code becomes a pain.

Most hits I got on searches for “transactions” and “rollback” were DB related and didn’t fit very well to a more generic kind of code…
Anyone has a good idea as to how to systematically flatten this atrocity?

  • 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-24T20:44:00+00:00Added an answer on May 24, 2026 at 8:44 pm

    Wouldn’t Context Manager objects and the with statement improve the situation? Especially if you can use a version of Python where the with statement supports multiple context expressions, as 2.7 or 3.x. Here’s an example:

    class Action(object):
        def __init__(self, count):
            self.count = count
        def perform(self):
            print "perform " + str(self.count)
            if self.count == 2:
                raise Exception("self.count is " + str(self.count))
        def commit(self):
            print "commit " + str(self.count)
        def rollback(self):
            print "rollback " + str(self.count)
        def __enter__(self):
            perform()
            return self
        def __exit__(self, exc_type, exc_value, traceback):
            if exc_value is None:
                self.commit()
            else:
                self.rollback()
    
    with Action(1), Action(2), Action(3):
        pass
    

    You’d have to move your code to a set of “transactional” classes, such as Action above, where the action to be performed is executed in the __enter__() method and, if this terminates normally, you would be guaranteed that the corresponding __exit()__ method would be called.

    Note that my example doesn’t correspond exactly to yours; you’d have to tune what to execute in the __enter__() methods and what to execute in the with statement’s body. In that case you might want to use the following syntax:

    with Action(1) as a1, Action(2) as a2:
        pass
    

    To be able to access the Action objects from within the body of the with statement.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.