Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Data Representation?

Reply
Thread Tools

Data Representation?

 
 
Kris Caselden
Guest
Posts: n/a
 
      10-11-2003
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], [b]]' instead of '[[1],
[2]]'? Any help most appreciated.
 
Reply With Quote
 
 
 
 
AK
Guest
Posts: n/a
 
      10-11-2003
In article <(E-Mail Removed) >, Kris
Caselden wrote:
> 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], [b]]' 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
 
Reply With Quote
 
 
 
 
John Roth
Guest
Posts: n/a
 
      10-11-2003

"Kris Caselden" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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], [b]]' 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



 
Reply With Quote
 
Kris Caselden
Guest
Posts: n/a
 
      10-12-2003
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 <(E-Mail Removed)> wrote in message news:<PH0ib.32215$(E-Mail Removed) hlink.net>...
> In article <(E-Mail Removed) >, Kris
> Caselden wrote:
> > 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], [b]]' 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

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      10-12-2003
Kris Caselden wrote:

> 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], [b]]' instead of '[[1],
> [2]]'? Any help most appreciated.


I'm not sure what you mean by [[a], [b]]. 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple programs to deal with data format, data synchronisation Ram VHDL 1 02-24-2005 05:34 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi Cisco 0 12-29-2004 05:03 PM
Data Recovery Book and Online Business Promotion, Products Sales Promotion, Search Engine Optimization and Online Data Recovery Training services Author Tarun Tyagi Cisco 0 12-15-2004 01:52 AM
Data type question for OLEdb data types Brian Henry ASP .Net 1 10-05-2003 03:18 PM
Bind different rows to different data sources in data grid AD ASP .Net 1 08-23-2003 10:34 PM



Advertisments