Help Create Good Data Model

mwt michaeltaft at gmail.com
Sat Mar 11 17:26:28 EST 2006


fumanchu: Interesting. I'm trying to understand atomicity. Also, since
I want this class to work using the Observer pattern, I've complicated
things, as shown below. I'll look into Dejavu for persistence (although
most of the basic values are persisted elsewhere, so this app will
mainly need only in-memory values and configuration stuff (for which I
think a ConfigParser will probably be enough).

Sybren: That's a cool choice, but I don't think it's right for what I'm
trying to do. Even more massive overkill than what I'm already doing.
Plus, I'm trying to write this thing so that I can hand it *anything*
(a value, a list, another dict, whole objects, etc.), which might be
tough with a DB.

Here's what I've got cooking at this point (adapted heavily from Bruce
Eckel, as well as incorporating fumanchu's corrections). If you get a
chance, please let me know what you think.


#!/usr/bin/python
# author mwt
# Mar '06
import copy, threading, observable

class FAHData(Observable):
    """The data model for the F at H monitor."""

    def __init__(self):
        Observable.__init__(self)
        self.data = {}#this dict will hold all data
        self.mutex = threading.RLock()

    def get_all_data(self):
        """Returns a COPY of entire data dict."""
        #not sure deepcopy() is really necessary here
        #but using it for now
        #might cause some weird synchronization problems...
        try:
            self.mutex.acquire()
            return copy.deepcopy(self.data)
        finally:
            self.mutex.release()

    def get_data(self, key):
        """Returns a COPY of <key> data element."""
        try:
            self.mutex.acquire()
            return copy.deepcopy(self.data[key])
        finally:
            self.mutex.release()

    #these three methods don't need a mutex because they are atomic (I
think):
    #-------------------------------------------->
    def set_value(self, key, value):
        """Sets value of <key> data element."""
        self.data[key] = value
        Observable.notifyObservers(self, arg = 'ELEMENT_CHANGED')

    def set_data(self, data):
        """Sets entire data dictionary."""
        self.data = data
        Observable.notifyObservers(self, arg = 'DATA_CHANGED')

    def clear_data(self):
        """Clears entire data dictionary."""
        self.data = {}
        Observable.notifyObservers(self, arg = 'DATA_CHANGED')
    #<---------------------------------------------


#!/usr/bin/python
# author mwt
# Mar '06
import threading

class Observer(object):
    def update(observable, arg):
        """OVERRIDE ME"""
        pass


class Observable(object):
    def __init__(self):
        self.obs = []
        self.mutex = threading.RLock()

    def addObserver(self, observer):
        self.mutex.aquire()
        try:
            if observer not in self.obs:
                self.obs.append(observer)
        finally:
            self.mutex.release()

    def notifyObservers(self, arg = None):
        self.mutex.aquire()
        try:
            localArray = self.obs[:]
        finally:
            self.mutex.release()
        for observer in localArray:
            observer.update(self, arg)

    #these methods don't need a mutex because they are atomic (I
think):
    #-------------------------------------------->

    def deleteObserver(self, observer):
        self.obs.remove(observer)

    def deleteObservers(self):
        self.obs = []

    def countObservers(self):
        return len(self.obs)
    
    #<---------------------------------------------

mwt




More information about the Python-list mailing list