473,382 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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.

Jul 18 '05 #1
5 1395

"Rodrigo Benenson" <ro******@elo.utfsm.cl> wrote in message news:40********@nova.entelchile.net...
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).

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.
Jul 18 '05 #2
In article <40********@nova.entelchile.net>,
Rodrigo Benenson <ro******@elo.utfsm.cl> wrote:

I need to implement a stack on a file.


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

A: No.
Q: Is top-posting okay?
Jul 18 '05 #3
Rodrigo,
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 ?


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(filename)
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(data)
self.write(st)
self.write(struct.pack('!i', len(st)))

def pop(self):
posn = self.f.tell()
if posn <= 0:
raise IndexError
self.f.seek(posn-self.s)
s = struct.unpack('!i', self.f.read(self.s))
self.f.seek(posn-self.s-s)
ret = cPickle.loads(self.f.read(s))
self.f.seek(posn-self.s-s)
return ret
Jul 18 '05 #4
On Mon, Jan 19, 2004 at 06:43:14PM -0800, Josiah Carlson wrote:
Rodrigo,
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 ?
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.


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


Jp

Jul 18 '05 #5
> > 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.


condone
v : excuse, overlook, or make allowances for; be lenient with;

Jp


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

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is...
2
by: news.tkdsoftware.com | last post by:
Aside from comp.compilers, is there any other forum, newsgroup or medium where I can post questions concerning the development of a byte code compiler & virtual stack machine? --
20
by: Sushil | last post by:
Hi gurus I was reading FAQ "alloca cannot be written portably, and is difficult to implement on machines without a conventional stack." I understand that the standard does not mandate...
2
by: Nick McCamy | last post by:
I have a question related to allocating on the stack. In this program below, are my following assumptions true? - variable a is allocated on the heap since it's static - variable b is...
4
by: alisaee | last post by:
plz check what i have made wrong what is requierd her is to creat class queue and class stack and run the push,pop operation . #include<iostream.h> #include<conio.h> #include<stdio.h> class...
10
by: ypjofficial | last post by:
Hello All, since the programs' stack is shared among all the function inside the program, I was just doing some R&D to see whether the same stack space is used for the variables inside the...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
1
by: alfie27 | last post by:
I currently have a working program that is a stack that stores integers. Now i have to convert it to store strings instead of integers. I have been working on this for hours and just keep getting...
1
by: cront | last post by:
All I was able to come up with is this code. I am get confused on how to solve this problem provided a very small amount of Stack operations. /* C++ Primer - 4/e * * Exercise 9.42 *...
9
by: Tarique | last post by:
Hello all.I am trying to implement a stack which can store either integer or float values. The code is given below: #include<stdio.h> #include<stdlib.h> #include<string.h> #define...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.