Can't subclass datetime.datetime?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant Edwards

    #1

    Can't subclass datetime.datetime?

    Is it true that a datetime object can convert itself into a
    string, but not the other way around? IOW, there's no simple
    way to take the output from str(d) and turn it back into d?

    So, I tried to create a class that knows how to do that, but I
    don't seem to be able to subclass datetime.dateti me:

    import datetime

    class MyDatetime(date time.datetime):
    def __init__(self,s ):
    s1,s2 = s.split(' ')
    v = s1.split('-') + s2.split(':')
    v = map(int,v)
    datetime.dateti me.__init__(sel f,v[0],v[1],v[2],v[3],v[4],v[5])

    s = '2005-02-14 12:34:56'
    d = MyDatetime(s)

    Running the above yields:

    Traceback (most recent call last):
    File "dt.py", line 11, in ?
    d = MyDatetime(s)
    TypeError: function takes at least 3 arguments (1 given)

    What's going on?

    --
    Grant Edwards grante Yow! I'm in a twist
    at contest!! I'm in a
    visi.com bathtub! It's on Mars!! I'm
    in tip-top condition!
  • Steven Bethard

    #2
    Re: Can't subclass datetime.dateti me?

    Grant Edwards wrote:[color=blue]
    > Is it true that a datetime object can convert itself into a
    > string, but not the other way around? IOW, there's no simple
    > way to take the output from str(d) and turn it back into d?[/color]

    I assume this is true because there is not one standard format for a
    date-time string. But I don't use the module enough, so I'll let
    someone else answer this part of the question.
    [color=blue]
    > import datetime
    >
    > class MyDatetime(date time.datetime):
    > def __init__(self,s ):
    > s1,s2 = s.split(' ')
    > v = s1.split('-') + s2.split(':')
    > v = map(int,v)
    > datetime.dateti me.__init__(sel f,v[0],v[1],v[2],v[3],v[4],v[5])
    >
    > s = '2005-02-14 12:34:56'
    > d = MyDatetime(s)
    >
    > Running the above yields:
    >
    > Traceback (most recent call last):
    > File "dt.py", line 11, in ?
    > d = MyDatetime(s)
    > TypeError: function takes at least 3 arguments (1 given)[/color]

    datetime.dateti me objects are immutable, so you need to define __new__
    instead of __init__:

    py> class DateTime(dateti me.datetime):
    .... def __new__(cls, s):
    .... s1, s2 = s.split(' ')
    .... v = map(int, s1.split('-') + s2.split(':'))
    .... return datetime.dateti me.__new__(cls, *v)
    ....
    py> DateTime('2005-02-14 12:34:56')
    DateTime(2005, 2, 14, 12, 34, 56)

    Steve

    Comment

    • Grant Edwards

      #3
      Re: Can't subclass datetime.dateti me?

      On 2005-02-14, Steven Bethard <steven.bethard @gmail.com> wrote:[color=blue]
      > Grant Edwards wrote:[color=green]
      >> Is it true that a datetime object can convert itself into a
      >> string, but not the other way around? IOW, there's no simple
      >> way to take the output from str(d) and turn it back into d?[/color]
      >
      > I assume this is true because there is not one standard format
      > for a date-time string.[/color]

      There seems to be a de-facto standard format: that which is
      returned by the str(d).
      [color=blue][color=green]
      >> class MyDatetime(date time.datetime):
      >> def __init__(self,s ):[/color][/color]
      [...]
      [color=blue]
      > datetime.dateti me objects are immutable, so you need to define
      > __new__ instead of __init__:[/color]

      Thanks. I should have known that. I guess I've never subclassed
      an immutible type before.

      --
      Grant Edwards grante Yow! It's hard being
      at an ARTIST!!
      visi.com

      Comment

      • Kent Johnson

        #4
        Re: Can't subclass datetime.dateti me?

        Grant Edwards wrote:[color=blue]
        > Is it true that a datetime object can convert itself into a
        > string, but not the other way around? IOW, there's no simple
        > way to take the output from str(d) and turn it back into d?[/color]

        According to this thread, a patch has been checked in that adds strptime() to datetime. So there is
        something to look forward to...
        http://tinyurl.com/4fbkb

        Kent

        Comment

        Working...