Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87146

Re: Adding a 'struct' into new python type

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <lakshmipathi.g@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; "subject:' ": 0.07; 'etc).': 0.09; 'foss': 0.09; 'subject:into': 0.09; 'translate': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; '"test"': 0.16; "'c'": 0.16; '(say': 0.16; '*i*': 0.16; 'cc:name:python list': 0.16; 'char,': 0.16; 'dependency,': 0.16; 'fine.': 0.16; 'luck,': 0.16; 'subject:Adding': 0.16; 'subject:type': 0.16; "test'": 0.16; 'typedef': 0.16; 'types,': 0.16; 'subject:python': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'example': 0.22; 'cc:addr:python.org': 0.22; 'this?': 0.23; 'char': 0.24; 'pointer': 0.24; 'test.': 0.24; 'cheers,': 0.24; 'cc:2**0': 0.24; 'class.': 0.26; 'define': 0.26; 'post': 0.26; 'header:In-Reply-To:1': 0.27; '----': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; "skip:' 10": 0.31; 'int,': 0.31; 'object.': 0.31; 'struct': 0.31; 'this.': 0.32; 'another': 0.32; 'basic': 0.35; "can't": 0.35; 'something': 0.35; 'convert': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'entry': 0.36; 'thanks': 0.36; 'hi,': 0.36; 'similar': 0.36; 'example,': 0.37; 'turn': 0.37; 'subject:new': 0.38; 'jason': 0.38; 'handle': 0.38; 'pm,': 0.38; 'sure': 0.39; 'how': 0.40; 'conversion': 0.61; 'new': 0.61; 'simple': 0.61; 'linked': 0.65; 'to:addr:gmail.com': 0.65; 'details': 0.65; 'believe': 0.68; 'mar': 0.68; 'response.': 0.68; 'to,': 0.72; 'potentially': 0.81; '2015': 0.84; 'double,': 0.84; 'float,': 0.84; 'struct.': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=7LWhfrAChc7pG6jqw8n6274KuB/2ZIiE19AxuLwWmgE=; b=MRvD6m5w9VhCVRUQtjgKTBH6mo6dkAtCzrkMZmCTrlbBD1pBqaWATU9Bk96/OL2okt IZqUGyGhfJ5LMVABe51jtIWYDBUE9uRzMV5+ziggNK+uDrGVitBx6C8q8hEFE3Qh7hq3 ePfR/JKRCk6pUqFuD+dKds4VbMM8jEvU5EFXh2d1xr36k6v7o5YNHRxBPOg9cR41K4q2 O9TQZRWpBEbmdQgw7uKdiImYx5AUte5E2O1F7qkaKF70dkWqtXELezAXZho/5zXK0a1x qDblpTBebKOtHhyAeOu5MZSykOENIVQkfEK87FyL+x5+rXoTV3uilhOqm/KmlcrCox21 VYXA==
X-Received by 10.50.36.103 with SMTP id p7mr68320612igj.20.1425833507335; Sun, 08 Mar 2015 09:51:47 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <CAEk9e3q6KwG02HKoZhvOpcU5Zm9VtsKLS6UuMTKG=c8ypZ506A@mail.gmail.com>
References <CAKuJGC_NxRa8MPGihJ9y3DoOvkvkpp0tzeeXZkfqJJ+2vowtzg@mail.gmail.com> <CAEk9e3q6KwG02HKoZhvOpcU5Zm9VtsKLS6UuMTKG=c8ypZ506A@mail.gmail.com>
From "Lakshmipathi.G" <lakshmipathi.g@gmail.com>
Date Sun, 8 Mar 2015 22:21:27 +0530
Subject Re: Adding a 'struct' into new python type
To Jason Swails <jason.swails@gmail.com>
Cc python list <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.169.1425833515.21433.python-list@python.org> (permalink)
Lines 77
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425833515 news.xs4all.nl 2829 [2001:888:2000:d::a6]:38281
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87146

Show key headers only | View raw


Hi Jason,

Thanks for the response. Okay, Will try to convert 'struct test' into
new python object (say 'PyStructTest') and include it inside
CountDictType as pyobject instead of 'struct'.
Will post details here, If i encounter any issues.
----
Cheers,
Lakshmipathi.G
FOSS Programmer.
www.giis.co.in/readme.html


On Sat, Mar 7, 2015 at 9:23 PM, Jason Swails <jason.swails@gmail.com> wrote:
>
>
> On Sat, Mar 7, 2015 at 4:15 AM, Lakshmipathi.G <lakshmipathi.g@gmail.com>
> wrote:
>>
>> Hi,
>>
>> I'm following this example :
>> http://nedbatchelder.com/text/whirlext.html#h_making_a_type and trying
>> to add
>> new data into 'CountDict' type
>>
>> Adding a simple 'char' works well.
>>
>> typedef struct {
>>    PyObject_HEAD
>>    PyObject * dict;
>>    int count;
>>    char c;  //add this and placed an entry into PyMemberDef as T_CHAR.
>> } CountDict;
>>
>> I can access  'c' from python code,no issues so far.
>>
>> Now I want to added 'struct type' into this 'CountDict' type.
>> struct test {
>> int x;
>> };
>>
>> typedef struct {
>>    PyObject_HEAD
>>    PyObject * dict;
>>    int count;
>>    char c;
>>    struct test t1; //?? how to add this
>> } CountDict;
>>
>>
>>
>> How to do achieve this? (Due to legacy code dependency, I can't use
>> ctype/cpython etc).
>> thanks for any help/pointers.
>
>
> The way *I* would do this is turn struct test into another Python object.
> Then instead of defining it in CountDict as type "struct test", define it as
> the PyStructTest that you assigned it to when you turned it into a Python
> class.
>
> For example, in that website you linked to, CountDict was turned into a
> CountDictType Python type.  So do something similar to "test" in which you
> turn it into a PyStructTest type in the same way.  Then declare it as a
> PyStructTest pointer instead of a struct test.
>
> I'm not sure you can get around an approach like this.  I believe that in
> order to access data from Python, it needs to be a Python type.  The
> struct-to-Python-type conversion *knows* how to translate basic types, like
> char, double, float, int, and long into their Python equivalents; but it
> can't handle something as potentially complicated as an arbitrary struct.
> Of course, if all you want to do is access t1 from C, then I think what you
> have is fine.
>
> Good luck,
> Jason

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Adding a 'struct' into new python type "Lakshmipathi.G" <lakshmipathi.g@gmail.com> - 2015-03-08 22:21 +0530

csiph-web