2

I am trying to use the eieio-persistent class to make persistent objects. My goal is to store many objects in a file.

I am not sure how to achieve this though. If I use this code, I do get a persistent object saved in test.db, but (perhaps predictably) the second save overwrites the first one.

(defclass animal (eieio-persistent)
  ((type :initarg :type)
   (file :initform "/Users/jkitchin/Dropbox/org-mode/test.db")))


(let ((a1 (animal :type 'mammal)))
  (eieio-persistent-save a1))

(let ((a1 (animal :type 'rodent)))
  (eieio-persistent-save a1))

Is there a way to save a list of these "animals" using the eieio framework? I would prefer not to create individual files for each object (I am anticipating thousands of objects later that need to be read in). Ideally, there would be another class that had a list of these animal objects in a records field.

Something like this is close

(defclass animal-db (eieio-named eieio-persistent)
  ((id :initarg :id)
   (file :initform "/Users/jkitchin/Dropbox/org-mode/test.db")
   (records :initarg :records :type listp))
  "Base db for animal class")

but the list in the records doesn't appear to be actual objects, when I read the code back in, but code that needs to be eval'd to get the object.

Any ideas?

  • I see an eieio-persistent-convert-list-to-object function: perhaps you can convert the list to a single object and save that? Mind you, my eieio knowledge is less than $\epsilon$. – NickD Mar 24 '17 at 15:33
  • That doesn't seem to be the purpose of that function, it seems to take a list of object creation terms, e.g. a class, name and slots. I thought I could use it on each record to create the objects, but it doesn't work for a reason I don't understand yet. – John Kitchin Mar 24 '17 at 16:48
  • Yes, you are right. Reading the eieio-persistent section in the doc seems to indicate that each object is associated with its own file, so you'd have to extend the class. ("IIUC" is implied :-) ) – NickD Mar 24 '17 at 17:05
  • So if you eval the code your problem is solved? Why is that insufficient? – Lyn Headley Dec 29 '17 at 1:27
  • I guess I would not say it is insufficient, just unexpected I guess. Maybe it makes sense, it only reads in the top object, not all the records internally. It looks like it would take a custom reader for something like that. – John Kitchin Dec 30 '17 at 3:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.