[ZODB-Dev] Advice On ZODB

Chris McDonough chrism@zope.com
24 Jan 2003 11:21:59 -0500


If you want to keep track of a set of processes (process ids?) for each
host, you could store them in an attribute of your host object that is a
list.  You could use a PersistentList instance, but it's more of a
distraction in this particular case.

class Host(Persistent):
   def __init__(self, ip):
       self.ip = ip
       self.last_polled = None
       self.processes = []

   def setLastPolled(self, t):
       self.last_polled = t

   def setProcesses(self, l):
       self.processes = list(l)

Note that this is just basic Python.  You can think of ZODB as just a
way to persist the state of your program at various points so when it
shuts down, you don't lose all of your data.  ZODB is only a "database"
in as much as it stores Python data structures persistently without you
needing to think much about it.  The rest is just pure Python.

On Fri, 2003-01-24 at 11:13, John Abel wrote:
> Ok, but what if I need to store more data, per root?  I have details on, 
> for example, a number of processes, for each IP, that I need to store. 
>  Is that were PersistentList() comes into it?  Would I need something 
> like this?
> 
> class Host(Persistent):
>     def __init__(self, ip):
>         self.ip = ip
>         self.last_polled = None
> 	self.process = PersistentList()
> 
>     def setLastPolled(self, t):
>         self.last_polled = t
> 
> In which case, how do I update self.process?
> 
> Sorry if this is a bit simple, but I really appreciate the help you guys 
> are giving.
> 
> John
> 
> Chris McDonough wrote:
> 
> >This may help:
> >
> >http://www.zope.org/Members/mcdonc/HowTos/gainenlightenment
> >
> >ZODB is not table-driven like a relational database.  It's an object
> >database, so you should try to create objects that represent your
> >problem domain and store them in ZODB.  So for instance if the data
> >you're trying to store is information about internet hosts, you might
> >have a Host class:
> >
> >import ZODB
> >from ZODB.FileStorage import FileStorage
> >from ZODB import DB
> >from Persistence import Persistent
> >
> >class Host(Persistent):
> >    def __init__(self, ip):
> >        self.ip = ip
> >        self.last_polled = None
> >
> >    def setLastPolled(self, t):
> >        self.last_polled = t
> >
> ># create a storage
> >fs = FileStorage('afile')
> >db = DB(fs)
> >
> >#... get a ZODB connection ...
> >conn = db.open()
> >
> ># get the root ZODB namespace
> >root = conn.root()
> >root['host1'] = Host('192.168.0.1')
> >root['host2'] = Host('192.168.0.2')
> >
> >  
> >
-- 
Chris McDonough <chrism@zope.com>
Zope Corporation