[C++-sig] automatic conversion of boost date_time to Python date

Luca Sbardella luca.sbardella at gmail.com
Tue Apr 10 23:51:38 CEST 2007


Thanks Roman, it was useful, however I'm getting some specific problem in
the implementation (I cannot debug the code since I cannot load the
PyDateTime static library).  the Python documentation says I should declare
PyDateTime_IMPORT but this does not compile!!!!
Still trying at the moment but this is what I have done, any comment would
be welcome.
Best
Luca


    template<class T, class TfromPy>
    struct ObjFromPy
    {
      ObjFromPy()
      {
        boost::python::converter::registry::push_back(&TfromPy::convertible,


&TfromPy::construct,

boost::python::type_id<T>());
      }
    };
    //
    template<class T, class TtoPy, class TfromPy>
    struct register_python_conversion
    {
      register_python_conversion()
      {
        boost::python::to_python_converter<T,TtoPy>();
        ObjFromPy<T,TfromPy>();
      }
    };
    //
    // IMPLEMENTATION
    //
    struct DateToPy
    {
      static PyObject* convert(const qm_date& dte)
      {
        return PyDate_FromDate( dte.year(), dte.month(), dte.day());
      }
    };
    //
    struct DateFromPy
    {
      //
      static void* convertible(PyObject* obj_ptr)
      {
        if(!PyDate_Check(obj_ptr)) return 0;
        return obj_ptr;
      }
      //
      static void construct(PyObject* obj_ptr,

boost::python::converter::rvalue_from_python_stage1_data* data)
      {
        int y = PyDateTime_GET_YEAR(obj_ptr);
        int m = PyDateTime_GET_MONTH(obj_ptr);
        int d = PyDateTime_GET_DAY(obj_ptr);
        qm_date* dte = new qm_date(y,m,d);
        data->convertible = (void*)dte;
      }
    };
    //
    //
    typedef register_python_conversion<qm_date, DateToPy, DateFromPy>
date_python_conversion;
    //

On 10/04/07, Roman Yakovenko <roman.yakovenko at gmail.com> wrote:
>
> On 4/10/07, Quant Mind <quant.mind at gmail.com > wrote:
>
> > Here is the problem.
> > I'm using boost date_time library for a calendar C++ application. I
> > exposed my C++ Calendar class to Python using boost python and everything is
> > going smoothly.
> > The class contains several functions which have boost::gregorian::date
> > as arguments.
> > Obviously, this class is not know to Python and I would like to
> > implement a custom converter between boost::gregorian::date and Python
> > datetime.date.
> > What would be the correct way to deal with this problem?
>
>
> Let me know if next link helped you: http://language-binding.net/pyplusplus/troubleshooting_guide/automatic_conversion/automatic_conversion.html
>
>
>
> --
> Roman Yakovenko
> C++ Python language binding
> http://www.language-binding.net/
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070410/d86150d1/attachment.htm>


More information about the Cplusplus-sig mailing list