durusmail: durus-users: Offline batch modifications
Offline batch modifications
2006-11-17
2006-11-17
2006-11-19
2006-11-20
2007-02-22
2007-02-22
2007-02-22
2007-02-22
Offline batch modifications
David Binger
2006-11-17
On Nov 17, 2006, at 8:10 AM, Paolo Losi wrote:

> Hi all,
>
> I'm using with great satisfaction Durus...
>
> There are however a couple of use cases that I cannot
> still handle effectively...
>
> Is there any suggestion?
>
> 1. DB Bulk data modification.
>
> I want to find all objects of type FOO
> and I want to rename a property without leveraging
> object graph structures (just to be sure that I do not
> miss any).

from durus.connection import gen_every_instance
for foo_instance in gen_every_instance(my_connection, FOO):
   foo_instance.new_name = foo_instance.old_name
   del foo_instance.old_name
my_connection.commit()

>
> 2. Data migration between two Durus instances.
>
> I have identical object graph structures in two different
> databases. I want to get all object in a btree structure
> on DB "A" and copy them to btree structure on DB "B"...

If you have a structure that contains no references to
any persistent instances outside of the structure, there is
a clever way to do this.  I learned about it from
Andrew Kuchling.

#initialize connection_to_old and connection_to_new

from cPickle import dumps, loads
btree = connection_to_old.get_root()['x']
giant_pickle = dumps(btree_in_old, 2)
new_btree = loads(giant_pickle)
connection_to_new.get_root()['x'] = new_btree
connection_to_new.commit()

# Note that new oids are assigned to the new_btree objects
# when they are committed. Make sure that your application does
# not depend on specific oid values.

# If necessary, run pickle with output to a file instead of
# using a string.

>
> 3. Human readable import/export (XML,JSON) format
>
> This would be great and would allow solving also problem
> 1 e 2...

This doesn't do quite what you like, but it might be helpful
for me to point out.

from pickletools import dis
from cStringIO import StringIO

def readable_representation(obj):
     pickle = connection.get_stored_pickle(obj._p_oid)
     s = StringIO()
     dis(pickle, s)
     return s.getvalue()

If someone has an inverse for dis, then you could use it
similarly for import.

reply