473,386 Members | 2,042 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,386 software developers and data experts.

writeable buffer and struct.pack_into and struct.unpck_from

Hi,

I can't find in the documentation the way to use these two functions.

can someone share a simple code that utilize these two functions?
Sep 20 '08 #1
4 5925
En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay
<Af**********@gmail.comescribió:
I can't find in the documentation the way to use these two functions.

can someone share a simple code that utilize these two functions?
struct.pack_into is intended to "fill" a buffer you got from somewhere,
probably other language or process. ctypes.create_string_buffer may be
used to create a writable buffer in python code.

pyfrom ctypes import create_string_buffer
pyb = create_string_buffer(10)
pyb.raw
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
pyfrom struct import *
pypack_into("hhh", b, 0, 1, 2, -1)
pyb.raw
'\x01\x00\x02\x00\xff\xff\x00\x00\x00\x00'

unpack_from does the opposite.
Before Python 2.5, you had to use pack to create a string object, and then
copy its contents into the destination buffer; using pack_into avoids the
memory copy.

--
Gabriel Genellina

Sep 20 '08 #2
On Sep 20, 6:42*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Sat, 20 Sep 2008 15:45:48 -0300, Tzury Bar Yochay *
<Afro.Syst...@gmail.comescribió:
I can't find in the documentation the way to use these two functions.
can someone share a simple code that utilize these two functions?

struct.pack_into is intended to "fill" a buffer you got from somewhere, *
probably other language or process. ctypes.create_string_buffer may be *
used to create a writable buffer in python code.

pyfrom ctypes import create_string_buffer
pyb = create_string_buffer(10)
pyb.raw
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
pyfrom struct import *
pypack_into("hhh", b, 0, 1, 2, -1)
pyb.raw
'\x01\x00\x02\x00\xff\xff\x00\x00\x00\x00'

unpack_from does the opposite.
Before Python 2.5,
That was when Python broke string immutability with the ctypes module.
you had to use pack to create a string object, and then *
copy its contents into the destination buffer; using pack_into avoids the*
memory copy.

--
Gabriel Genellina
'mmap' seems to have always offered random-access writes. A search
through the implementation for "tp_as_buffer" returns a lot of zeros
however.

Sep 21 '08 #3
Thanks Gabriel,
I was missing the information how to create a writable buffer.
Sep 21 '08 #4
On Sep 21, 3:16*pm, Tzury Bar Yochay <Afro.Syst...@gmail.comwrote:
Thanks Gabriel,
I was missing the information how to create a writable buffer.
array.array objects also have the writable buffer nature:
>>import array
b = array.array('c', '\0' * 10)
b
array('c', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>import struct
struct.pack_into('<hhhh', b, 0, 1, 2, -1, -32768)
b
array('c', '\x01\x00\x02\x00\xff\xff\x00\x80\x00\x00')

HTH,
John
Sep 21 '08 #5

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

Similar topics

4
by: ma740988 | last post by:
A few days ago I recieved yet again advice on implementing a buffer of bytes. At issue: (part 1) how do I take the contents of a struct, then dump (used sparingly) it into a byte buffer. ...
2
by: derek.google | last post by:
I have an application that's crashing because of an alignment problem, and this is the smallest program that demonstrates what's happening: int main() { struct Message { unsigned short size;...
5
by: Roy Hills | last post by:
When I'm reading from or writing to a network socket, I want to use a struct to represent the structured data, but must use an unsigned char buffer for the call to sendto() or recvfrom(). I have...
0
by: Steve Chaplin | last post by:
I'm trying to create an object (as a C extension) to support the buffer interface and was getting "TypeError: buffer is read-only" errors. So I had a look at the array object and it has the same...
4
by: aki | last post by:
Hi all, i am writing codes for implementing network protocols. i have written a method which is receiving a packet from network. i have assumed that the packet i will receive will be of type...
2
by: patrickdepinguin | last post by:
Hi, I use zlib to write data structures to a compressed file, using the gzwrite function. Afterwards I read the data back with gzread. I notice that this works well when the data written is not...
36
by: James Harris | last post by:
Initial issue: read in an arbitrary-length piece of text. Perceived issue: handle variable-length data The code below is a suggestion for implementing a variable length buffer that could be used...
4
by: castironpi | last post by:
I'd like to seriously nominate this idea and get a considered opinion on it. struct.Struct lets you encode Python objects into structured memory. It accepts a format string, and optionally a...
1
by: Slain | last post by:
I have a buffer, which has a header and then data. There is some leeway space between the header and the data in the buffer. I have to add some more data which is a struct and I know the size of...
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
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.