Rewriting a bash script in python

Ben Finney bignose+hates-spam at benfinney.id.au
Tue Nov 4 23:11:17 EST 2008


Chris Jones <cjns1989 at gmail.com> writes:

> I am thinking of rewriting it in python using OOP tactics/strategy.
> 
> Please advise.

I advise you not to have the object-oriented programming hammer as
your only tool, because it's easy to then treat every problem as
though it were a nail.

What you describe seems trivially a procedural sequence of branching
steps using simple types from the standard library. Use functions to
encapsulate the conceptually-related steps, and call those functions
in a simple branch as you demonstrated. I see no need to apply
object-oriented techniques for this problem.

    import datetime

    def is_full_backup_required(when):
        """ Determine whether a full backup is required for datetime ‘when’ """
        result = False
        if determine_whether_full_backup_required_for_datetime(when):
            result = True
        return result

    def most_recent_incremental_backup(when):
        """ Return the most recent incremental backup before datetime ‘when’ """
        backup = most_recent_backup(when)
        return backup

    def save_previous_full_archive(when):
        """ Save the full archive previous to datetime ‘when’ """

    def save_previous_incremental_archive(when):
        """ Save the incremental archive previous to datetime ‘when’ """

    def perform_full_backup(system):
        """ Do a full backup of the system """

    def perform_incremental_backup(system):
        """ Do an incremental backup of the system """

    def get_this_system():
        """ Return the current system for backups """
        system = determine_what_this_system_is()
        return system

    system = get_this_system()
    when = datetime.datetime.now()
    if is_full_backup_required(when):
        save_previous_month_archive(when)
        perform_full_backup(system)
    else:
        save_previous_incremental_archive(when)
        perform_incremental_backup(system)


I don't see any need for creating new classes for any of this, unless
a “system” is more complicated than just a string for the system
name, or a “backup” is more complicated than a tarball file.

Instead, the problem seems one best solved simply by abstracting the
steps involved at each point of the *procedure*, as I've done in my
example. Each one of those functions could be very simple or could be
complex enough to spawn a whole host of helper functions, possibly
even separate modules. That seems a more useful approach than trying
to force-fit object-orientation to this procedural problem.

-- 
 \     “Some mornings, it's just not worth chewing through the leather |
  `\                                             straps.” —Emo Philips |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list