stack-like file object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rodrigo Benenson

    stack-like file object

    Hi,
    I need to implement a stack on a file.
    The idea is to have a big list, and put part of his head on the disk. The
    model of access to the file is like a stack (read in order only the tail,
    write only at the tail).
    How could I create this, knowing that I need to pickle arbritary objects ?

    (to my eyes the problem is: how to pickle various objects in file and access
    them as separate)

    RodrigoB.



  • Serge Orlov

    #2
    Re: stack-like file object


    "Rodrigo Benenson" <rodrigob@elo.u tfsm.cl> wrote in message news:400be8f3_2 @nova.entelchil e.net...[color=blue]
    > Hi,
    > I need to implement a stack on a file.
    > The idea is to have a big list, and put part of his head on the disk. The
    > model of access to the file is like a stack (read in order only the tail,
    > write only at the tail).[/color]
    Why don't you use a collection of file organized as stack in one directory
    or in many directories if your underlying file system cannot effectively
    support many files in one directory. If your pickles are small (much less
    than block size of the file system) you can keep several of them in one
    file about the size of the block size. Since the files will be small you can
    simply rewrite them competely each time you need change them.

    -- Serge.


    Comment

    • Aahz

      #3
      Re: stack-like file object

      In article <400be8f3_2@nov a.entelchile.ne t>,
      Rodrigo Benenson <rodrigob@elo.u tfsm.cl> wrote:[color=blue]
      >
      >I need to implement a stack on a file.[/color]

      Why not use a real database?
      --
      Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

      A: No.
      Q: Is top-posting okay?

      Comment

      • Josiah Carlson

        #4
        Re: stack-like file object

        Rodrigo,
        [color=blue]
        > I need to implement a stack on a file.
        > The idea is to have a big list, and put part of his head on the disk. The
        > model of access to the file is like a stack (read in order only the tail,
        > write only at the tail).
        > How could I create this, knowing that I need to pickle arbritary objects ?[/color]

        While I don't condone the use of files as stacks, the below should work
        for you. It doesn't reduce in size when an object is removed, but as
        long as you don't try to share the stack between processes or threads,
        it should work fine.

        - Josiah

        import cPickle
        import struct
        import os

        class filestack:
        def __init__(self, filename):
        try: os.remove(filen ame)
        except: pass
        self.fn = filename
        self.f = open(filename, 'wb+')
        self.s = len(struct.pack ('!i', 0))

        def __del__(self):
        self.f.close()
        del self.f
        os.remove(self. fn)

        def append(self, data):
        st = cPickle.dumps(d ata)
        self.write(st)
        self.write(stru ct.pack('!i', len(st)))

        def pop(self):
        posn = self.f.tell()
        if posn <= 0:
        raise IndexError
        self.f.seek(pos n-self.s)
        s = struct.unpack(' !i', self.f.read(sel f.s))
        self.f.seek(pos n-self.s-s)
        ret = cPickle.loads(s elf.f.read(s))
        self.f.seek(pos n-self.s-s)
        return ret


        Comment

        • Jp Calderone

          #5
          Re: stack-like file object

          On Mon, Jan 19, 2004 at 06:43:14PM -0800, Josiah Carlson wrote:[color=blue]
          > Rodrigo,
          >[color=green]
          > > I need to implement a stack on a file.
          > > The idea is to have a big list, and put part of his head on the disk. The
          > > model of access to the file is like a stack (read in order only the tail,
          > > write only at the tail).
          > > How could I create this, knowing that I need to pickle arbritary objects ?[/color]
          >
          > While I don't condone the use of files as stacks, the below should work
          > for you. It doesn't reduce in size when an object is removed, but as
          > long as you don't try to share the stack between processes or threads,
          > it should work fine.[/color]

          condone
          v : excuse, overlook, or make allowances for; be lenient with;
          [color=blue]
          > [snip - file-based stack implementation][/color]

          Jp

          Comment

          • Josiah Carlson

            #6
            Re: stack-like file object

            > > While I don't condone the use of files as stacks, the below should work[color=blue][color=green]
            > > for you. It doesn't reduce in size when an object is removed, but as
            > > long as you don't try to share the stack between processes or threads,
            > > it should work fine.[/color]
            >
            > condone
            > v : excuse, overlook, or make allowances for; be lenient with;
            >
            > Jp[/color]

            I was using it in a similar tone as: "I don't condone the robbing of
            banks, but below is a plan to do so, if one were to want to."

            - Josiah

            Comment

            Working...