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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:47:09+00:00 2026-05-30T15:47:09+00:00

I’m writing a script which receives two lists of data and looks for the

  • 0

I’m writing a script which receives two lists of data and looks for the differences, in order to update data into a database.
These lists are non-homogeneous: one is a list of database objects, the other is a list of dictionaries.
For many reasons, this tool offers the opportunity to preview the list of the differences before applying changes.
After analyzing the preview, if the update is accepted by a moderator, changes will be applied.

That means that the script, containing a lot of loops and conditional tests, will generate a list of preview (served to an output page) and a list of changes.

At the first stage of developing, I wrote a single script which would run in two modes: ‘preview’ and ‘update’.
This script performed all loops and conditional checks, and if changes were found at some point, it would add a message to an output string if running in ‘preview’ mode, or execute a command if running in ‘update’ mode.

Then I’ve began to think that maybe it would be better to pass between loops and conditional checks only once, adding preview messages to an output string and commands to a command list, whenever a change is found.

Then, after serving preview page to the moderator, if changes are accepted, run a second script, which would be such simple as:

def apply_changes(command_list, ...):
    for c in command_list:
        try:
            exec(c)
        except Exception, err:
            logger.error('Something went wrong while executing command %s: %s', c, err)
            raise Exception, err

Q: Would be ok to switch to the second version of the script? Which cautions/errors/strange_behaviors would involve?

I’ve been asking almost the same question under the language-agnostic tag,
because I was more interested in the algorithmical point of view of the problem than in its implementation.

But focusing more on the implementational point of view, seems that with Python the second version of this script performs better and is easier to maintain than the first one.

Are there any reasons for which I should prefer the first version?

EDIT: Adding some code, in order to clarify the question even more.

An excerpt from first version code would be something like this function (used in the easier possible case, in which the values compared are both strings), called by other functions inside of a nested for loop if some specific conditions are met:

def update_text_field(object, old_value, new_value, field_name, mode):
    ....
    if old_value != new_value:
        if mode is 'preview': output += print_old_new_values(old_value, new_value, field_name)
        if mode is 'update':
           if hasattr(object, field_name):
            setattr(object, field_name, new_value)
            object.save()
           else:
            ... 
         ....

In the second version, this excerpt would transform into something like this:

def update_text_field(object, old_value, new_value, field_name, mode):
    ....
    if old_value != new_value:
        output_list.append(print_old_new_values(old_value, new_value, field_name))
        command_list.append(generate_command(command, old_value, new_value, field_name))
    ...
  • 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-30T15:47:10+00:00Added an answer on May 30, 2026 at 3:47 pm

    One reason why you would prefer the first version is to have the ability to run the script in update mode only (skipping the mandatory-in-second-script preview step).

    Here’s a suggestion as to how to make the script easier to maintain: Abstract away the mode when the script is running. This can be done like this:

    def generate_diff(f, other parameters...):
        # ... Do computations ...
        # Whenever command "c" needs to be previewed/executed, do:
        f(c)
        # ... More computations...
    

    And then you can define both modes (this is just a simplistic example):

    def _previewMode(command):
        print command
    
    def _updateMode(command):
        exec(command)
    

    And optionally define convenience wrappers:

    def preview(other parameters...):
        return generate_diff(_previewMode, other parameters...)
    
    def update(other parameters...):
        return generate_diff(_updateMode, other parameters...)
    

    And even easily define new modes:

    def _interactiveMode(command):
        if raw_input('Execute command ' + command + '?').lower() in ('yes', 'y'):
            print 'Command returned:', exec(command)
    
    def interactive(other parameters...):
        return generate_diff(_interactiveMode, other parameters...)
    

    This is possible due to first-class functions in Python. This way, the only things you need to maintain are very cleanly separated and don’t have to care about one another:

    • The diff script
    • What to do with a command when in preview mode
    • What to do with a command when in update mode

    No duplicate version of the function, no possibly-expensive-in-the-long-run branching inside the diff function, easy as pie to maintain.

    If having the ability to run in update mode directly without going through preview is not important, then the best performance option is indeed to build a list of commands in preview mode and then run it in the update mode. It is about as easy to maintain as the above solution. However, even if you do go for this option, consider how easy it would be to implement using the above pattern:

    def buildCommandList(other parameters...):
        commandList = []
        generate_diff(commandList.append, other parameters...)
        return commandList
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
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 am currently running into a problem where an element is coming back from

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.