Data Representation?

Discussion in 'Python' started by Kris Caselden, Oct 12, 2003.

  1. 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.
     
    Kris Caselden, Oct 12, 2003
    #1
    1. Advertising

  2. Kris Caselden

    AK Guest

    In article <>, 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], ]' 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
     
    AK, Oct 12, 2003
    #2
    1. Advertising

  3. Kris Caselden

    John Roth Guest

    "Kris Caselden" <> wrote in message
    news:...
    > 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.


    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
     
    John Roth, Oct 12, 2003
    #3
  4. 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 <> wrote in message news:<PH0ib.32215$>...
    > In article <>, 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], ]' 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
     
    Kris Caselden, Oct 12, 2003
    #4
  5. Kris Caselden

    Peter Otten Guest

    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], ]' 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
     
    Peter Otten, Oct 12, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeremy Pyle

    Re: Representation of real numbers

    Jeremy Pyle, Jun 27, 2003, in forum: VHDL
    Replies:
    1
    Views:
    1,283
    Ken Smith
    Jun 27, 2003
  2. Jim Lewis

    Re: Representation of real numbers

    Jim Lewis, Jul 1, 2003, in forum: VHDL
    Replies:
    0
    Views:
    1,834
    Jim Lewis
    Jul 1, 2003
  3. Hendrik Greving

    VHDL boolean representation

    Hendrik Greving, Jun 10, 2005, in forum: VHDL
    Replies:
    2
    Views:
    1,242
    Ira Baxter
    Jun 24, 2005
  4. Charles T.

    Binary data representation

    Charles T., Feb 4, 2004, in forum: C++
    Replies:
    11
    Views:
    818
    Charles T.
    Feb 5, 2004
  5. Replies:
    1
    Views:
    254
    Pascal J. Bourguignon
    Jul 1, 2008
Loading...

Share This Page