Data Representation?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kris Caselden

    Data Representation?

    Say I have some data:
    [color=blue][color=green][color=darkred]
    >>> a=[1]
    >>> b=[2]
    >>> link=[a,b][/color][/color][/color]

    The simplest why to write this to a file represents it as
    [color=blue][color=green][color=darkred]
    >>> print str(link)[/color][/color][/color]
    [[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.
  • AK

    #2
    Re: Data Representation?

    In article <abc3fdd3.03101 11540.37400ed6@ posting.google. com>, Kris
    Caselden wrote:[color=blue]
    > Say I have some data:
    >[color=green][color=darkred]
    >>>> a=[1]
    >>>> b=[2]
    >>>> link=[a,b][/color][/color]
    >
    > The simplest why to write this to a file represents it as
    >[color=green][color=darkred]
    >>>> print str(link)[/color][/color]
    > [[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.[/color]

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

    shelve works kind of like this:

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

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

    code not tested..

    -AK

    Comment

    • John Roth

      #3
      Re: Data Representation?


      "Kris Caselden" <google@hanger. snowbird.net> wrote in message
      news:abc3fdd3.0 310111540.37400 ed6@posting.goo gle.com...[color=blue]
      > Say I have some data:
      >[color=green][color=darkred]
      > >>> a=[1]
      > >>> b=[2]
      > >>> link=[a,b][/color][/color]
      >
      > The simplest why to write this to a file represents it as
      >[color=green][color=darkred]
      > >>> print str(link)[/color][/color]
      > [[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.[/color]

      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



      Comment

      • Kris Caselden

        #4
        Re: Data Representation?

        Yes, thanks. Pickle/marshal/shelve are pretty close to what I'm
        looking for. Shelve in particular. However, consider:
        [color=blue][color=green][color=darkred]
        >>> d = shelve.open('da ta.txt',writeba ck=True)
        >>> d['a']=[1]
        >>> d['b']=d['a']
        >>> d['c']=[d['a']]
        >>> print d[/color][/color][/color]
        {'a': [1], 'c': [[1]], 'b': [1]}[color=blue][color=green][color=darkred]
        >>> d['a'][0]=2
        >>> print d[/color][/color][/color]
        {'a': [2], 'c': [[2]], 'b': [2]}[color=blue][color=green][color=darkred]
        >>> d.close()
        >>> d = shelve.open('da ta.txt',writeba ck=True)
        >>> print d[/color][/color][/color]
        {'a': [2], 'c': [[2]], 'b': [2]}[color=blue][color=green][color=darkred]
        >>> d['a'][0]=3
        >>> print d[/color][/color][/color]
        {'a': [3], 'c': [[2]], 'b': [2]}[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

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

        AK <ak@nospam.co m> wrote in message news:<PH0ib.322 15$mQ2.5062@new sread1.news.atl .earthlink.net> ...[color=blue]
        > In article <abc3fdd3.03101 11540.37400ed6@ posting.google. com>, Kris
        > Caselden wrote:[color=green]
        > > Say I have some data:
        > >[color=darkred]
        > >>>> a=[1]
        > >>>> b=[2]
        > >>>> link=[a,b][/color]
        > >
        > > The simplest why to write this to a file represents it as
        > >[color=darkred]
        > >>>> print str(link)[/color]
        > > [[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.[/color]
        >
        > Sure, look at pickle and shelve modules in library reference.
        >
        > shelve works kind of like this:
        >
        > s = shelve.open('fi lename')
        > s['a'] = a
        > s.close()
        >
        > s = shelve.open('fi lename')
        > a = s['a']
        > print a
        >
        > code not tested..
        >
        > -AK[/color]

        Comment

        • Peter Otten

          #5
          Re: Data Representation?

          Kris Caselden wrote:
          [color=blue]
          > Say I have some data:
          >[color=green][color=darkred]
          >>>> a=[1]
          >>>> b=[2]
          >>>> link=[a,b][/color][/color]
          >
          > The simplest why to write this to a file represents it as
          >[color=green][color=darkred]
          >>>> print str(link)[/color][/color]
          > [[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.[/color]

          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(fil e("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

          Comment

          Working...