Jump to page: 1 2
Thread overview
beginner's pyd question - exporting structs to python
Aug 18, 2014
Laeeth Isharc
Aug 18, 2014
Laeeth Isharc
Aug 18, 2014
Russel Winder
Aug 18, 2014
Laeeth Isharc
Aug 18, 2014
Russel Winder
Aug 18, 2014
Laeeth Isharc
Aug 18, 2014
Laeeth Isharc
Re: London D User Group [was beginner's pyd question - exporting structs to python]
Aug 19, 2014
Russel Winder
Aug 18, 2014
Russel Winder
Aug 18, 2014
Laeeth Isharc
Aug 18, 2014
Russel Winder
Aug 18, 2014
Laeeth Isharc
Aug 19, 2014
bachmeier
Aug 19, 2014
Russel Winder
Aug 19, 2014
Russel Winder
Re: OT On NumPy [was beginner's pyd question - exporting structs to python]
Aug 19, 2014
Russel Winder
August 18, 2014
Hi there.

Brief introduction, and a beginner's question.

I just started playing with D a couple of weeks ago.  I have been programming in C on and off since the late 80s, but I do finance for a living and my programming skills grew rusty.  I have a bit more time now to catch up with developments, and D looks a very intriguing robust alternative to C when python won't cut the mustard.

I am trying to implement the Demark technical analysis indicators in D, and want to be able to call them from python.

So I have set up pyd okay, and the examples seem to work fine.  Running on Fedora 20.  I am struggling a bit with the syntax on wrap_struct to export a struct and function returning a struct to python.  Here is what I have:

module hello2;

import pyd.pyd;
import std.stdio;
import std.conv;

struct t_mystruct {
  int i;
  string s;
};

t_mystruct hello(int[] inp) {
    int i;
    t_mystruct mystruct;

    mystruct.i=inp.length;
    mystruct.s="hello there";
    return mystruct;
}

extern(C) void PydMain() {
    def!(hello)();
    module_init();
    wrap_struct!(
      t_mystruct,
      "t_mystruct"
    );
}

Error:

def: hello
/root/anaconda/lib/python2.7/site-packages/celerid/infrastructure/pyd/def.d(151): Error: static assert  "string parameters must be wrapped with Docstring, Mode, etc"
/root/anaconda/lib/python2.7/site-packages/celerid/infrastructure/pyd/class_wrap.d(1474):        instantiated from here: Args!("", "", "t_mystruct", "", "t_mystruct")
hellostruct.d(30):        instantiated from here: wrap_struct!(t_mystruct, "t_mystruct")
error: command 'dmd' failed with exit status 1
[root@fedora demark]#

Apologies if it's in one of the examples - I did poke around, and couldn't see any obvious sample.

Many thanks.


Laeeth.
August 18, 2014
Thank to jwhear on irc who solved it for me despite claiming not to be a pyd guru.

In case it's of benefit, here is what works:

module hellostruct;

import pyd.pyd;
import std.stdio;
import std.conv;

struct t_mystruct {
  int i;
  string s;
};

t_mystruct hellostruct(int[] inp) {
    int i;
    t_mystruct mystruct;

    for (i=0;i<inp.length;i++)
    {
      writefln("inp " ~ to!string(i) ~ " is " ~ to!string(inp[i]));
    }
    writefln(" * ok, bye");
    mystruct.i=99;
    mystruct.s="hello there";
    return mystruct;
}

extern(C) void PydMain() {
    def!(hellostruct)();
    module_init();
    wrap_struct!(
      t_mystruct,
      Member!"s",
      Member!"i"
    )();
}

[root@fedora demark]# cat test.py
import os.path, sys
import distutils.util

# Append the directory in which the binaries were placed to Python's sys.path,
# then import the D DLL.
libDir = os.path.join('build', 'lib.%s-%s' % (
    distutils.util.get_platform(),
    '.'.join(str(v) for v in sys.version_info[:2])
))
sys.path.append(os.path.abspath(libDir))

import hellostruct
inp=[1,2,3,4]
mystruct= hellostruct.hellostruct(inp)
print mystruct.i
print mystruct.s
August 18, 2014
On Mon, 2014-08-18 at 16:39 +0000, Laeeth Isharc via Digitalmars-d-learn wrote:
> Hi there.
> 
> Brief introduction, and a beginner's question.
> 
> I just started playing with D a couple of weeks ago.  I have been programming in C on and off since the late 80s, but I do finance for a living and my programming skills grew rusty.  I have a bit more time now to catch up with developments, and D looks a very intriguing robust alternative to C when python won't cut the mustard.

All the cool folk doing data analysis and visualization using Python no longer bother with hand written C (*) for when pure Python won't cut the mustard.  If Numba can't do the job, then Cython gets used.

I have all my computational pure Python source codes running as fast as C these days thanks to Numba. (And judicious profiling.)

I would say that Python folk will now only be looking to C, C++, Fortran, D, for pre-written libraries in those language. Given all the codes are written in C, C++ or Fortran with none in D…

On the other hand PyD does work for me, but I only pass primitive types, not C structs.


(*) Nor C++, nor D.
-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

August 18, 2014
On Mon, 2014-08-18 at 17:17 +0000, Laeeth Isharc via Digitalmars-d-learn wrote:
> Thank to jwhear on irc who solved it for me despite claiming not to be a pyd guru.

:-)

[…]
>      distutils.util.get_platform(),
[…]

Does os.uname() not provide sufficient information?

[…]
> print mystruct.i
> print mystruct.s

Should use print as a function not as a statement. Use Python 3, or if you have to use Python 2 (which almost no-one does):

from __future__ import print_function

as the first statement.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

August 18, 2014
> All the cool folk doing data analysis and visualization using Python no longer bother with hand written C (*) for when pure Python won't cut the mustard.  If Numba can't do the job, then Cython gets used.
>
> I have all my computational pure Python source codes running as fast as C these days thanks to Numba. (And judicious profiling.)
>
> I would say that Python folk will now only be looking to C, C++,
> Fortran, D, for pre-written libraries in those language. Given all the codes are written in C, C++ or Fortran with none in D…

Thanks for the colour - I appreciate it.  I have played with
numba and pypy with numpy and it seems a powerful tool for some
kinds of jobs.  Perhaps it is my relative unfamiliarity with
python, but for the time being I feel more comfortable with C
type languages for other kinds of work.  As a pragmatic idealist,
one may as well use whatever tool seems to be pretty good
generally and which one feels confident in wielding to accomplish
the task at hand.

Out of curiosity, what do you use D for given your views about
the redundancy of C type languages for non-system programming?

"Should use print as a function not as a statement. Use Python 3,
or if
you have to use Python 2 (which almost no-one does):

from __future__ import print_function

as the first statement."

Thank you.
August 18, 2014
On Monday, 18 August 2014 at 18:08:59 UTC, Russel Winder via Digitalmars-d-learn wrote:

> […]
>>      distutils.util.get_platform(),
> […]
>
> Does os.uname() not provide sufficient information?

This was boilerplate generated by pyd.
August 18, 2014
On Mon, 2014-08-18 at 18:59 +0000, Laeeth Isharc via Digitalmars-d-learn
wrote:
[…]
> Thanks for the colour - I appreciate it.  I have played with numba and pypy with numpy and it seems a powerful tool for some kinds of jobs.  Perhaps it is my relative unfamiliarity with python, but for the time being I feel more comfortable with C type languages for other kinds of work.  As a pragmatic idealist, one may as well use whatever tool seems to be pretty good generally and which one feels confident in wielding to accomplish the task at hand.

Whilst the hardcore Pythonistas remain Pythonistas, some of the periphery has jumped ship to Go. Sadly D did not capture these folk, it perhaps should have done. It would be easy to blame fadism, but I think the actual reasons are far less superficial.

For me, NumPy has some serious problems despite being the accepted norm for computational work. The USP for Python/NumPy/SciPy/Matplotlib is that it costs an infinite amount less than Mathematica and Matlab. This is an non-trivial issue from which I am gaining a lot of business :-)

> Out of curiosity, what do you use D for given your views about the redundancy of C type languages for non-system programming?

In a sense I could rightly be labelled a D dilettante. I had a possible startup a couple of years ago where we would have used D, but it never started. A side-effect was that I gave some support to David Simcha creating std.parallelism, but for the last couple of years my income has come from Python or Groovy/GPars with no real D activity.

[…]

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

August 18, 2014
On Mon, 2014-08-18 at 19:00 +0000, Laeeth Isharc via Digitalmars-d-learn wrote:
> On Monday, 18 August 2014 at 18:08:59 UTC, Russel Winder via Digitalmars-d-learn wrote:
> 
> > […]
> >>      distutils.util.get_platform(),
> > […]
> >
> > Does os.uname() not provide sufficient information?
> 
> This was boilerplate generated by pyd.

Hummm… if I get time I feel a PR in order.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

August 18, 2014
On Monday, 18 August 2014 at 19:28:55 UTC, Russel Winder via Digitalmars-d-learn wrote:
> On Mon, 2014-08-18 at 19:00 +0000, Laeeth Isharc via Digitalmars-d-learn
> wrote:
>> On Monday, 18 August 2014 at 18:08:59 UTC, Russel Winder via Digitalmars-d-learn wrote:
>> 
>> > […]
>> >>      distutils.util.get_platform(),
>> > […]
>> >
>> > Does os.uname() not provide sufficient information?
>> 
>> This was boilerplate generated by pyd.
>
> Hummm… if I get time I feel a PR in order.

Perhaps I should rephrase.  I copied the example as a base, so technically it wasn't generated.  But no harm in tweaking the examples.
August 18, 2014
> Whilst the hardcore Pythonistas remain Pythonistas, some of the
> periphery has jumped ship to Go. Sadly D did not capture these folk, it perhaps should have done. It would be easy to blame fadism, but I think the actual reasons are far less superficial.

So I gather that you agree that "what everyone is doing" may not be the best in this case (python vs D) if there are no direct network effects beyond libraries and getting help and you have the freedom to determine your own platform choices?

> For me, NumPy has some serious problems despite being the accepted norm for computational work.

If not too offtopic, do you have a link describing, or would you briefly summarize these problems?  I am intrigued.  And what would you suggest in its place?  Fortran?

>> Out of curiosity, what do you use D for given your views about
>> the redundancy of C type languages for non-system programming?
>
> In a sense I could rightly be labelled a D dilettante. I had a possible startup a couple of years ago where we would have used D, but it never started. A side-effect was that I gave some support to David Simcha creating std.parallelism, but for the last couple of years my income has come from Python or Groovy/GPars with no real D activity.

Would you consider D stable enough/suitable for general financial market work with development initially by a small underresourced team?  Not ultra high frequency execution - at most legging in and managing longer term positions.  But I am more interested in sentiment analysis, producing technical analysis indicators that summarize market activity across many different securities, some bond arb stuff.  C++ just seems so ugly, and I feel uncomfortable only having python in the toolbox.  D seems so far to be quite suitable...
« First   ‹ Prev
1 2