Help Create Good Data Model

mwt michaeltaft at gmail.com
Tue Mar 14 02:11:46 CET 2006


fumanchu wrote:
> If you used a Queue, it wouldn't be the container itself; rather, it
> would be a gatekeeper between the container and consumer code. A
> minimal example of user-side code would be:
>
> class Request:
>     def __init__(self, op, data):
>         self.op = op
>         self.data = data
>         self.reply = None
> req = Request('get', key)
> data_q.put(req, block=True)
> while req.reply is None:
>     time.sleep(0.1)
> do_something_with(req.reply)
>
> The container-side code would be:
>
> while True:
>     request = data_q.get(block=True)
>     request.reply = handle(request)
>
> That can be improved with queue timeouts on both sides, but it shows
> the basic idea.
>
>
> Robert Brewer
> System Architect
> Amor Ministries
> fumanchu at amor.org

I get it. That's cool.
Here's the latest incarnation of this code. I haven't implemented the
lock you suggested yet, although this version seems to be working fine.


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

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

    def __init__(self):
        observable.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...
        self.mutex.acquire()
        try:
            return copy.deepcopy(self.data)
        finally:
            self.mutex.release()

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

    def set_value(self, key, value):
        """Sets value of <key> data element."""
        self.mutex.acquire()
        try:
            self.data[key] = value
            observable.Observable.notifyObservers(self, event =
'VALUE_CHANGED', key = key)
        finally:
            self.mutex.release()

    def set_values(self, values):
        """Sets a list of values"""
        self.mutex.acquire()
        try:
            for value in values:
                self.data[value] = values[value]
            #print 'values input are: %s' %self.data
            observable.Observable.notifyObservers(self, event =
'VALUES_CHANGED', keys = values.keys())
        finally:
            self.mutex.release()

    def set_data(self, data):
        """Sets entire data dictionary."""
        self.mutex.acquire()
        try:
            self.data = data
            observable.Observable.notifyObservers(self, event =
'DATA_SET')
        finally:
            self.mutex.release()

    def clear_data(self):
        """Clears entire data dictionary."""
        self.mutex.acquire()
        try:
            self.data = {}
            observable.Observable.notifyObservers(self, event =
'DATA_CLEARED')
        finally:
            self.mutex.release()




More information about the Python-list mailing list