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

John Colvin via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Fri Jul 3 03:52:36 PDT 2015


On Friday, 3 July 2015 at 10:23:14 UTC, Laeeth Isharc wrote:
> 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.

Aren't there time-zone concerns? Or is this just a mapping 
between D's std.datetime.DateTime and python's datetime.datetime 
with tzinfo==None, i.e. a naive date?

Also, there would have to be some serious warning signs about 
translating from python to D, in that the micro-seconds will be 
truncated. Doesn't a lack of microseconds make it unusable for 
tick data?


More information about the Digitalmars-d-announce mailing list