472,139 Members | 1,729 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to use a contiguous memory location of n bytes in python

Hi,

I want to know how to instantiate a data structure which has n bytes
(given by me) and is internally stored in a contiguous fashion. I know
list is not implemented like this, so I am in a fix. There seems to be
a buffer object available, but haven't seen it being used anywhere.
Does anyone know how to use Buffer? Say I want to write some data onto
the buffer and then write the contents of the entire buffer to a file
(without making a new string, but lets first get the buffer issue out
of the way), how do I do it?

Thanks,
ssg
Nov 14 '08 #1
3 3035
chachi:
I want to know how to instantiate a data structure which has n bytes
(given by me) and is internally stored in a contiguous fashion.
array.array("B", ...) may be fit for you. You can also use a numpy
array of bytes.

Bye,
bearophile
Nov 14 '08 #2
On Nov 13, 6:40*pm, bearophileH...@lycos.com wrote:
chachi:
I want to know how to instantiate a data structure which has n bytes
(given by me) and is internally stored in a contiguous fashion.

array.array("B", ...) may be fit for you. You can also use a numpy
array of bytes.

Bye,
bearophile
Also, there is the 'mmap' module.
Nov 14 '08 #3
be************@lycos.com <be************@lycos.comwrote:
chachi:
I want to know how to instantiate a data structure which has n bytes
(given by me) and is internally stored in a contiguous fashion.

array.array("B", ...) may be fit for you. You can also use a numpy
array of bytes.
The mmap module is useful also for larger amounts (>4k say).
mmap's are individually free-able so they don't fragment your memory.

http://docs.python.org/library/mmap.html

Eg create a 1 GB anonymous mmap, access it and then delete it
>>from mmap import mmap
a = mmap(-1, 1000000000)
a[0]
'\x00'
>>a[0] = 'z'
a[999999999]
'\x00'
>>a[999999999]='q'
a[999999999]
'q'
>>del a
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ship1 | last post: by
14 posts views Thread by Greg Copeland | last post: by
38 posts views Thread by Peteroid | last post: by
22 posts views Thread by divya_rathore_ | last post: by
62 posts views Thread by ivan.leben | last post: by
22 posts views Thread by Jack | last post: by
reply views Thread by leo001 | last post: by

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.