473,666 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_i nto( 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 1277
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.unpac k_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.net wrote:
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_i nto( 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...@gma il.comwrote:
On Aug 25, 4:25*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
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.unpac k_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_i nto( 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.net wrote:
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...@gma il.comwrote:
On Aug 25, 11:47*pm, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
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_AsWrit eBuffer= prototype( ( "PyObject_AsWri teBuffer",
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_AsWrit eBuffer( 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.Structur e. '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
3080
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 fastest way to write and read just a simple : while data: f.write(struct.pack(format, integer-value)) or is there another, quicker and nicer way to do it????
2
11243
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
2917
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
1871
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 lNumberOfPoints; int bUseRegion; enum ES_RegionType regionType;
8
3944
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 example: class A { public: const vector<int>& getTable() { return m_table; }
23
5802
by: myth.drannon | last post by:
lets say I have a header file : struct AAAA { blabla..... }; typedef struct AAAA A; typedef struct BBB
8
2243
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. Further in the application code at some places forward declearion for these datatypes are given as class . The code compiles without any errors and warning on solaris (g++)and linux (g++)but give Compiler Warning C4099 on windows (MSDEV VC6.0) but...
16
1352
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, which I know I'm supposed to avoid, by using a Class). I edited the rest to leave out the irrelevant formatting and printing of the quotations. I've read about Classes several times, but I don't "get" them yet. Obviously. If I can solve one...
4
1724
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 buffer and offset to/from which to read/write the structure. What do you think of random access to the results? To avoid Marc's concern about meaningless anonymous record entries, model the constructor after 'namedtuple' type. (unproduced)
0
8355
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8638
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7381
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6191
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2006
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.