D extensions to python, inline in an ipython/jupyter notebook

Laeeth Isharc via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Fri Jul 3 03:23:08 PDT 2015


On Thursday, 2 July 2015 at 22:55:51 UTC, Laeeth Isharc wrote:
>>> It would be v helpful to have a Datetime conversion from D.  
>>> Looks like there is a macro for converting from ymd in 
>>> datetime.h, so I guess one could just write some code against 
>>> this API in C, D, or Cython and link it in with D so one can 
>>> transfer data structures over more easily.
>>
>> I know just enough about that topic to be very scared.
>
> I should be able to call these from D without changing PyD, I 
> think, for D to Python.  Not sure about Python to D, but maybe.
>
> https://github.com/ariovistus/pyd/blob/master/infrastructure/python/python.d

Doh!

It's already in deimos, but you need to call PyDateTime_IMPORT(); 
before calling the conversion function.

So all that is needed is the following:

module example;
import std.datetime;
import std.stdio;
import pyd.pyd;
import std.conv;
import deimos.python.datetime;


DateTime foo()
{
     return DateTime(1999,1,1);
}
extern(C) void PydMain() {
     ex_d_to_python((DateTime dt) => 
PyDateTime_FromDateAndTime(dt.year,dt.month,dt.day,dt.hour,dt.minute,cast(int)dt.second,0));
     def!foo();
     module_init();
     PyDateTime_IMPORT();
}

Ideally, shouldn't PyD do this for you ?  I think the code is 
already there in make_object.d, but it doesn't work - maybe 
because PyDateTime_IMPORT() has not been called if it needs to be.

Thanks.


Laeeth.


More information about the Digitalmars-d-announce mailing list