473,387 Members | 1,420 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,387 software developers and data experts.

Struct class random access

struct.Struct lets you encode Python objects into structured memory.
It accepts a format string, and optionally a buffer and offset to/from
which to read/write the structure. What do you think of random access
for the results?

(unproduced)
>>packer= struct.Struct( 'IIIf255p' )
packer.pack_into( buf, off, 10, 20, 30, 0.5, 'abc' )
packer.unpack_from( buf, off, 2 ) #reads field 2
30

Does this take a PEP, or just a patch submission?
Aug 25 '08 #1
6 1255
On Mon, 25 Aug 2008 13:03:09 -0700, castironpi wrote:
struct.Struct lets you encode Python objects into structured memory. It
accepts a format string, and optionally a buffer and offset to/from
which to read/write the structure. What do you think of random access
for the results?

(unproduced)
>>>packer= struct.Struct( 'IIIf255p' )
packer.pack_into( buf, off, 10, 20, 30, 0.5, 'abc' )
packer.unpack_from( buf, off, 2 ) #reads field 2
30
I don't like it for the same reason I don't like index access on tuples
or lists that represent a "record" -- the numbers are quite meaningless.
Names for the components result in much easier to understand source code,
so I would prefer to use `ctypes` or `construct` to create such a record.

Ciao,
Marc 'BlackJack' Rintsch
Aug 25 '08 #2
On Aug 25, 4:25*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Mon, 25 Aug 2008 13:03:09 -0700, castironpi wrote:
struct.Struct lets you encode Python objects into structured memory. It
accepts a format string, and optionally a buffer and offset to/from
which to read/write the structure. *What do you think of random access
for the results?
(unproduced)
>>packer= struct.Struct( 'IIIf255p' )
packer.pack_into( buf, off, 10, 20, 30, 0.5, 'abc' )
packer.unpack_from( buf, off, 2 ) #reads field 2
30

I don't like it for the same reason I don't like index access on tuples
or lists that represent a "record" -- the numbers are quite meaningless. *
Names for the components result in much easier to understand source code,
so I would prefer to use `ctypes` or `construct` to create such a record.

Ciao,
* * * * Marc 'BlackJack' Rintsch
I'm interested in the speed benefit, so you don't have to reconstruct
the entire 'record' just to read/write one 'field'. How in ctypes?
Aug 25 '08 #3
On Aug 25, 4:49*pm, castironpi <castiro...@gmail.comwrote:
On Aug 25, 4:25*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Mon, 25 Aug 2008 13:03:09 -0700, castironpi wrote:
struct.Struct lets you encode Python objects into structured memory. It
accepts a format string, and optionally a buffer and offset to/from
which to read/write the structure. *What do you think of random access
for the results?
(unproduced)
>>>packer= struct.Struct( 'IIIf255p' )
>>>packer.pack_into( buf, off, 10, 20, 30, 0.5, 'abc' )
>>>packer.unpack_from( buf, off, 2 ) #reads field 2
30
I don't like it for the same reason I don't like index access on tuples
or lists that represent a "record" -- the numbers are quite meaningless.. *
Names for the components result in much easier to understand source code,
so I would prefer to use `ctypes` or `construct` to create such a record.
Ciao,
* * * * Marc 'BlackJack' Rintsch

I'm interested in the speed benefit, so you don't have to reconstruct
the entire 'record' just to read/write one 'field'. *How in ctypes?
Model the constructor after 'namedtuple' type.

(unproduced)
>>packer= struct.Struct( 'IIIf255p', 'i1', 'i2', 'i3', 'afloat', 'name')
packer.pack_into( buf, off, 10, 20, 30, 0.5, 'abc' )
packer.unpack_from( buf, off, 'i3' )
30
>>packer.unpack_from( buf, off )
( 10, 20, 30, 0.5, 'abc' )

You still get marginal speed benefit in sequential access. You avoid
the construction of n-1 objects.
Aug 25 '08 #4
On Mon, 25 Aug 2008 14:49:14 -0700, castironpi wrote:
I'm interested in the speed benefit, so you don't have to reconstruct
the entire 'record' just to read/write one 'field'. How in ctypes?
Only the field accessed is converted.

Ciao,
Marc 'BlackJack' Rintsch
Aug 26 '08 #5
On Aug 25, 11:47*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Mon, 25 Aug 2008 14:49:14 -0700, castironpi wrote:
I'm interested in the speed benefit, so you don't have to reconstruct
the entire 'record' just to read/write one 'field'. *How in ctypes?

Only the field accessed is converted.

Ciao,
* * * * Marc 'BlackJack' Rintsch
I know that. I was asking how to write 'unpack_from( buf, off, 2 )',
when buf is a non-ctypes buffer.
Aug 26 '08 #6
On Aug 26, 12:41*am, castironpi <castiro...@gmail.comwrote:
On Aug 25, 11:47*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Mon, 25 Aug 2008 14:49:14 -0700, castironpi wrote:
I'm interested in the speed benefit, so you don't have to reconstruct
the entire 'record' just to read/write one 'field'. *How in ctypes?
Only the field accessed is converted.
Ciao,
* * * * Marc 'BlackJack' Rintsch

I know that. *I was asking how to write 'unpack_from( buf, off, 2 )',
when buf is a non-ctypes buffer.
The code with ctypes is more elegant than I thought.

from ctypes import *
prototype= PYFUNCTYPE( c_int, py_object, POINTER(c_void_p),
POINTER(c_uint) )
PyObject_AsWriteBuffer= prototype( ( "PyObject_AsWriteBuffer",
pythonapi ) )
def refas( buf, offset, tp ):
''' return an instance of |tp| that refers to |offset| bytes into
buffer |buf| '''
_b, _s= c_void_p(0), c_uint(0)
PyObject_AsWriteBuffer( buf, byref(_b), byref(_s) ) #should return
0
c= cast( _b.value+ offset, POINTER( tp ) )
return c.contents

'tp' can be any class that is derived from ctypes.Structure. 'buf'
can be any object that supports the buffer protocol, including
'mmap'. Remember when mapping pointers to store offsets, not memory
addresses.

I'd like to know how supported this is considered to be, across
platforms and versions. Can I rely on this in the future?
Aug 27 '08 #7

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

Similar topics

2
by: Thomas | last post by:
What's the quickest way to write and read 10.000 integer values ( or more ) to and from a file? Using struct somehow? The example in the docs shows how to handle to or three arguments, but is the...
2
by: SACHIN | last post by:
I have this class as part of a Consol application. using System; namespace Bugreport { /// <summary> /// This class tries to use the Class/Struct combination. /// </summary> class Class1 {
60
by: Mohd Hanafiah Abdullah | last post by:
Is the following code conformat to ANSI C? typedef struct { int a; int b; } doomdata; int main(void) { int x;
7
by: Urs Wigger | last post by:
In a C++ project, I have the following struct definition: struct GridModeDataT { double dVal1; double dVal2; double dVal3; long ...
8
by: Gregory | last post by:
I have a question about using STL containers in C++ class public interface. Lets say that I want to return some container from class method or accept class method parameter as some container. For...
23
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
8
by: nurxb01 | last post by:
Hi I have a very basic doubt. The application i'm working on has some old code already in place. There are some struct data type decleared in a .h file and are being used for RPC communication....
16
by: BartlebyScrivener | last post by:
I am a mere hobbyist. Spent several hours trying to make a class, because I think this is an occasion where I need one. But I can't make it work. This code "works" (only because of the global c,...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.