Data Representation?

K

Kris Caselden

Say I have some data:

The simplest why to write this to a file represents it as
[[1], [2]]

Unfortunately, if this is read back in via execfile(), the whole
dynamic nature of changing 'link' by changing 'a' and 'b' is lost. Is
there any way to write data so that the list name is written instead
of the list's values? Essentially, '[[a], ]' instead of '[[1],
[2]]'? Any help most appreciated.
 
A

AK

Say I have some data:
a=[1]
b=[2]
link=[a,b]

The simplest why to write this to a file represents it as
[[1], [2]]

Unfortunately, if this is read back in via execfile(), the whole
dynamic nature of changing 'link' by changing 'a' and 'b' is lost. Is
there any way to write data so that the list name is written instead
of the list's values? Essentially, '[[a], ]' instead of '[[1],
[2]]'? Any help most appreciated.


Sure, look at pickle and shelve modules in library reference.

shelve works kind of like this:

s = shelve.open('filename')
s['a'] = a
s.close()

s = shelve.open('filename')
a = s['a']
print a

code not tested..

-AK
 
J

John Roth

Kris Caselden said:
Say I have some data:
a=[1]
b=[2]
link=[a,b]

The simplest why to write this to a file represents it as
[[1], [2]]

Unfortunately, if this is read back in via execfile(), the whole
dynamic nature of changing 'link' by changing 'a' and 'b' is lost. Is
there any way to write data so that the list name is written instead
of the list's values? Essentially, '[[a], ]' instead of '[[1],
[2]]'? Any help most appreciated.


Not the way you're doing it. The thing you're getting hung
up over is that what's stored in the list is the objects, not the
names that they're bound to.

The code you give, by the way, doesn't allow you to change
'link' by changing the object bound to 'a' and 'b', and in any
case doesn't change the object bound to 'link' at all. In other
words, if you say:

a = "xxx"

the list you have bound to 'link' will not be changed to
["xxx", [2]].

However, if you say:
a[0] = "xxx"

then the list you have bound to 'link' will become:

[["xxx"], [2]]

The reason is that the first simply rebinds 'a', without
affecting the list '[1]' at all, while the second changes
the first element of that list, without rebinding 'a'.

If this makes your head ache, you're in good company.
I don't know what you're trying to accomplish; possibly
if you supplied some more details we could give you
some suggestions about which way to proceed.

My suspicion is that you need to use an object of
some kind, and possibly the pickle or marshal
modules rather than simple lists.

John Roth
 
K

Kris Caselden

Yes, thanks. Pickle/marshal/shelve are pretty close to what I'm
looking for. Shelve in particular. However, consider:
d = shelve.open('data.txt',writeback=True)
d['a']=[1]
d['b']=d['a']
d['c']=[d['a']]
print d {'a': [1], 'c': [[1]], 'b': [1]}
d['a'][0]=2
print d {'a': [2], 'c': [[2]], 'b': [2]}
d.close()
d = shelve.open('data.txt',writeback=True)
print d {'a': [2], 'c': [[2]], 'b': [2]}
d['a'][0]=3
print d {'a': [3], 'c': [[2]], 'b': [2]}

Note how the dynamic link to d['a'] is lost. Is there any module
similar to shelve that could maintain this link?

AK said:
Say I have some data:
a=[1]
b=[2]
link=[a,b]

The simplest why to write this to a file represents it as
print str(link)
[[1], [2]]

Unfortunately, if this is read back in via execfile(), the whole
dynamic nature of changing 'link' by changing 'a' and 'b' is lost. Is
there any way to write data so that the list name is written instead
of the list's values? Essentially, '[[a], ]' instead of '[[1],
[2]]'? Any help most appreciated.


Sure, look at pickle and shelve modules in library reference.

shelve works kind of like this:

s = shelve.open('filename')
s['a'] = a
s.close()

s = shelve.open('filename')
a = s['a']
print a

code not tested..

-AK
 
P

Peter Otten

Kris said:
Say I have some data:
a=[1]
b=[2]
link=[a,b]

The simplest why to write this to a file represents it as
[[1], [2]]

Unfortunately, if this is read back in via execfile(), the whole
dynamic nature of changing 'link' by changing 'a' and 'b' is lost. Is
there any way to write data so that the list name is written instead
of the list's values? Essentially, '[[a], ]' instead of '[[1],
[2]]'? Any help most appreciated.


I'm not sure what you mean by [[a], ]. If it is sufficcient that

link[0] is a and link[1] is b

i. e. they refer to the same list when restored, I suggest that you
introduce a namespace class and make a, b and link attributes. You can then
use the standard pickle procedure:

import pickle

class Namespace:
def __str__(self):
return str(self.__dict__)

ns = Namespace()
ns.a = [1]
ns.b = [2]
ns.link = [ns.a, ns.b]


pickle.dump(ns, file("tmp", "w"))

ns2 = pickle.load(file("tmp", "r"))

assert ns2.a is ns2.link[0]
assert ns2.b is ns2.link[1]

print ns2

ns2.b.append(3)
print ns2

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top