2

Is there any way to unpickle multiple objects in reverse in order to load pickled objects from the last one pickled?

For example:

import pickle
a = 'string1'
b = 'string2'
with open('test', 'wb') as f:
    pickle(a, f)
    pickle(b, f)

After unpickling and printing each unpickling, I'd like to see: 'string2', 'string1'.

itf
  • 559
  • 1
  • 3
  • 15

1 Answers1

2

Use pickle and dbm together. This code works on python2 and python3 (tested on Raspbian linux)

import pickle
import dbm

OBJECTS = (
  'string1',
  'string2',
)

def store():
  db = dbm.open('test','c')
  index = 0
  for ob in OBJECTS:
    db[str(index)] =    pickle.dumps(ob)
    index += 1
  db['N'] = str(index)   # store object count
  db.close()

def load():
  db = dbm.open('test','r')
  nb = int(db['N']) 
  while nb>0:
    nb -= 1
    ob = pickle.loads(db[str(nb)])
    print (ob)
  db.close()

store()
load()

Key 'N' tells how many objects are stored. This should work for a very large number of objects.

Edited

This is not restricted to strings, it will also work with any object that pickle can serialize. For example, you can write

OBJECTS = (
  ( 'A', 'b', '123' ),
  'SomeString', 
  {1: 'ABC', 2:((1,2),(3,4)), 3: [9,8,7,6]}, 
  [[],[1],[2,3],[4,5,6]],   
)
Sci Prog
  • 2,651
  • 1
  • 10
  • 18
  • Thanks @sci-prog! This doesn't quite fit my needs since I will be storing arrays and not strings (I know, I should have made my initial question more explicit and my toy example less toy), BUT your suggestion of dbm got me pointed in the right direction. I think that I will end up using python's shelve or sqlite3. – itf Mar 22 '16 at 03:57
  • This works with lists (in fact, with any object that `pickle` can serialize). See my edit. But your solution of using other "Data persistence" module should also work. – Sci Prog Mar 23 '16 at 03:22