Hi all,I am trying to write a Cython extension type, Timestamp, that mimics datetime but enriches it with metadata. This is for the timeseries functionality we're trying to build up in pandas.It seems like I cannot derive from datetime directly. I tried this, and it worked syntactically, but there seems to be no way to intercept the constructor call and construct the super class appropriately.So I went with composition, where datetime is a member variable. However, I cannot get this sort of thing to work:datetime <comparison> Timestampwhile this is just fine:Timestamp <comparison> datetimeHow would you all approach this situation?Thanks in advance,Adam
Hm, how does it now construct properly? You tried something along the lines of
cdef extern from "datetime.h":
ctypedef class datetime.datetime [ object PyDateTime_DateTime ]:
pass
cdef class MyDateTime(datetime):
pass
?
>> So I went with composition, where datetime is a member variable. However,
>> I cannot get this sort of thing to work:
>>
>> datetime <comparison> Timestamp
>>
>> while this is just fine:
>>
>> Timestamp <comparison> datetime
>>
>> How would you all approach this situation?
How does it not work? In the latter case your comparison method will
only be invoked if Timestamp returns NotImplemented.
>> Thanks in advance,
>> Adam
>
>
First thing to note is that when implementing __richcmp__, then,
confusingly, "self" may be the second argument rather than the first. So
you need to do typechecks on both arguments etc.
Dag
I thought the same thing, but a test with a print actually shows that
it inverts the operators. BTW, we should update the documentation to
use Py_LT and friends.
On 5 February 2012 22:31, Adam Klein <ad...@lambdafoundry.com> wrote:Hm, how does it now construct properly? You tried something along the lines of
> Sorry, fixing subject line and reposting. Copy-and-paste stupidity.
>
>> Hi all,
>>
>> I am trying to write a Cython extension type, Timestamp, that mimics
>> datetime but enriches it with metadata. This is for the timeseries
>> functionality we're trying to build up in pandas.
>>
>> It seems like I cannot derive from datetime directly. I tried this, and
>> it worked syntactically, but there seems to be no way to intercept the
>> constructor call and construct the super class appropriately.
cdef extern from "datetime.h":
ctypedef class datetime.datetime [ object PyDateTime_DateTime ]:
pass
cdef class MyDateTime(datetime):
pass
?
How does it not work? In the latter case your comparison method will
>> So I went with composition, where datetime is a member variable. However,
>> I cannot get this sort of thing to work:
>>
>> datetime <comparison> Timestamp
>>
>> while this is just fine:
>>
>> Timestamp <comparison> datetime
>>
>> How would you all approach this situation?
only be invoked if Timestamp returns NotImplemented.
>> Thanks in advance,
>> Adam
>
>
Liskov's substitution principle doesn't quite agree, but you can
filter arguments to your heart's content. E.g. def __init__(self,
arg1, arg2): super(MyClass, self).__init__(arg1)
>>
>>
>> >> So I went with composition, where datetime is a member variable.
>> >> However,
>> >> I cannot get this sort of thing to work:
>> >>
>> >> datetime <comparison> Timestamp
>> >>
>> >> while this is just fine:
>> >>
>> >> Timestamp <comparison> datetime
>> >>
>> >> How would you all approach this situation?
>>
>> How does it not work? In the latter case your comparison method will
>> only be invoked if Timestamp returns NotImplemented.
>
>
> I implemented __richcmp__, which only gets called when Timestamp is on the
> LHS of the comparison. I would expect datetime to try to invoke Timestamp's
> comparison on failure, but this doesn't seem to be what it does ...
Oh sorry, you're implementing Timestamp, not datetime. Well, yes, the
LHS gets the comparison function called. The RHS only gets its
comparison function called when the LHS returns NotImplemented. If the
LHS thinks it knows the answer, then the RHS can't change that.
>>
>>
>> >> Thanks in advance,
>> >> Adam
>> >
>> >
>
>
Oh sorry, you're implementing Timestamp, not datetime. Well, yes, the
>>
>>
>> >> So I went with composition, where datetime is a member variable.
>> >> However,
>> >> I cannot get this sort of thing to work:
>> >>
>> >> datetime <comparison> Timestamp
>> >>
>> >> while this is just fine:
>> >>
>> >> Timestamp <comparison> datetime
>> >>
>> >> How would you all approach this situation?
>>
>> How does it not work? In the latter case your comparison method will
>> only be invoked if Timestamp returns NotImplemented.
>
>
> I implemented __richcmp__, which only gets called when Timestamp is on the
> LHS of the comparison. I would expect datetime to try to invoke Timestamp's
> comparison on failure, but this doesn't seem to be what it does ...
LHS gets the comparison function called. The RHS only gets its
comparison function called when the LHS returns NotImplemented. If the
LHS thinks it knows the answer, then the RHS can't change that.
>>
>>
>> >> Thanks in advance,
>> >> Adam
>> >
>> >
>
>
You're talking about this error? "__new__ method of extension type
will change semantics in a future version of Pyrex and Cython. Use
__cinit__ instead." You could subclass your class and override
__new__.
That would work, but the point was to write a python subclass, not to
subclass in python :) That means you can do it in either Python or
Cython, i.e. write 'class' instead of 'cdef class'.
... and eventually fix the problem by generating an implementation for
__richcmp__() when __eq__() and friends are defined.
http://trac.cython.org/cython_trac/ticket/130
Stefan
+1 for that, the __richcmp__ is kind of a wart.
On a related note, should we add these things to Cython/Includes/cpython ?
I don't think we currently have any stdlib modules there.
Stefan
It's already there in cpython/object.pxd.
Not on my side - I don't see any "datetime" stuff in cpython/*.pxd.
Stefan
Oh, sorry you were talking about datetime. We definitely could yeah,
although I don't think many people are using it.
Hmm, with the introduction of dattetime support in NumPy 1.6 series, I foresee that many people will start using it gradually. So +1 for this.
Francesc Alted
Good point. They are encoded as 64 bit integers though, right? In any
case, supporting more of Python's public interface in pxds is always
good.
Yes, int64 for both timedelta64 and datetime64. For more info, see the NEP:
https://github.com/numpy/numpy/blob/master/doc/neps/datetime-proposal.rst
Francesc Alted
--
---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/JBVhaeNMQhg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/0a88764d-a8ba-eddb-e8f8-fca4a13b15e8%40behnel.de.
--
---
You received this message because you are subscribed to a topic in the Google Groups "cython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cython-users/JBVhaeNMQhg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/2c0dae4a-600a-1acd-bb0f-4c98cc3746dc%40behnel.de.