Returning a date as string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?iso-8859-1?B?Qmr2cm4gS2VpbA==?=

    #1

    Returning a date as string

    Hello pythons,

    I have little problem with understanding conversions in python. I've
    written a little class - nothing much, just to try out Python a little
    - containing the following method:

    def __repr__(self):
    """Serializ es the note.

    Currently the format of notes isn't decided upon. XML output
    is
    projected."""
    return "Due: " + str(self.dateDu e) + \
    "\nDate: " + str(self.dateCr eated) + \
    "\nSubject: " + self.subject + \
    "\n" + self.content

    The fields "dateDue" and "dateCreate d" contain datetime.date objects.
    Now when I try to serialize the whole thing:
    >>myNote
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "notes.py", line 81, in __repr__
    return "Due: " + str(self.dateDu e) + \
    TypeError: cannot concatenate 'str' and 'datetime.date' objects

    I tryed different variant before I wrapped "self.dateD ue" in a str()
    constructor:
    I tried to put it in brackets, or call its .isoformat() method.
    Nothing works. It still complains that I was trying to concatenate a
    string with a date, but I really wanna concatenate a string with a
    string!

    Could anyone please tell me what I am doing wrong?

    Greetings,
    Björn

  • 7stud

    #2
    Re: Returning a date as string

    On Apr 21, 2:59 pm, Björn Keil <abgr...@silber drache.netwrote :
    Hello pythons,
    >
    I have little problem with understanding conversions in python. I've
    written a little class - nothing much, just to try out Python a little
    - containing the following method:
    >
    def __repr__(self):
    """Serializ es the note.
    >
    Currently the format of notes isn't decided upon. XML output
    is
    projected."""
    return "Due: " + str(self.dateDu e) + \
    "\nDate: " + str(self.dateCr eated) + \
    "\nSubject: " + self.subject + \
    "\n" + self.content
    >
    The fields "dateDue" and "dateCreate d" contain datetime.date objects.
    Now when I try to serialize the whole thing:
    >
    >myNote
    >
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "notes.py", line 81, in __repr__
    return "Due: " + str(self.dateDu e) + \
    TypeError: cannot concatenate 'str' and 'datetime.date' objects
    >
    I tryed different variant before I wrapped "self.dateD ue" in a str()
    constructor:
    I tried to put it in brackets, or call its .isoformat() method.
    Nothing works. It still complains that I was trying to concatenate a
    string with a date, but I really wanna concatenate a string with a
    string!
    >
    Could anyone please tell me what I am doing wrong?
    >
    Greetings,
    Björn
    This works for me:

    import datetime

    class MyDate(object):
    def __init__(self, date):
    self.d = date
    def __repr__(self):
    return str(self.d)

    md = MyDate(datetime .date.today())
    print "the result is: " + repr(md)

    ##output: the result is: 2007-04-21

    Comment

    • John Machin

      #3
      Re: Returning a date as string

      On Apr 22, 6:59 am, Björn Keil <abgr...@silber drache.netwrote :
      Hello pythons,
      >
      I have little problem with understanding conversions in python. I've
      written a little class - nothing much, just to try out Python a little
      - containing the following method:
      >
      def __repr__(self):
      """Serializ es the note.
      >
      Currently the format of notes isn't decided upon. XML output
      is
      projected."""
      Insert here:

      print "Due: %r" % self.dateDue
      print "Created: %r" % self.dateCreate d
      print "Subject: %r" % self.subject
      print "Content: %r % self.content

      return "Due: " + str(self.dateDu e) + \
      "\nDate: " + str(self.dateCr eated) + \
      "\nSubject: " + self.subject + \
      "\n" + self.content
      >
      The fields "dateDue" and "dateCreate d" contain datetime.date objects.
      Now when I try to serialize the whole thing:
      >
      >myNote
      >
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "notes.py", line 81, in __repr__
      return "Due: " + str(self.dateDu e) + \
      TypeError: cannot concatenate 'str' and 'datetime.date' objects
      >
      I tryed different variant before I wrapped "self.dateD ue" in a str()
      constructor:
      I tried to put it in brackets, or call its .isoformat() method.
      Nothing works. It still complains that I was trying to concatenate a
      string with a date, but I really wanna concatenate a string with a
      string!
      >
      Could anyone please tell me what I am doing wrong?
      Not showing us the *whole* code that you are executing.

      Suggestions:
      (1) Maybe dates have crept into self.subject and self.content. The
      print statements above should show exactly what you've got.

      (2) Maybe you didn't save it before you ran it. Maybe you forgot to
      reload(yourModu le). Try making up the smallest possible script that
      demonstrates your problem. Get out of whatever IDE you may be using
      and go to the OS command-line prompt. Use type (windows) or cat (*x)
      to print your script on the console. Run your script. Copy the console
      contents and paste it into your mail/news client.

      HTH,
      John

      Comment

      • =?iso-8859-1?B?Qmr2cm4gS2VpbA==?=

        #4
        Re: Returning a date as string

        On 22 Apr., 02:26, John Machin <sjmac...@lexic on.netwrote:
        On Apr 22, 6:59 am, Björn Keil <abgr...@silber drache.netwrote :
        >
        Hello pythons,
        >
        I have little problem with understanding conversions in python. I've
        written a little class - nothing much, just to try out Python a little
        - containing the following method:
        >
        def __repr__(self):
        """Serializ es the note.
        >
        Currently the format of notes isn't decided upon. XML output
        is
        projected."""
        >
        Insert here:
        >
        print "Due: %r" % self.dateDue
        print "Created: %r" % self.dateCreate d
        print "Subject: %r" % self.subject
        print "Content: %r % self.content
        >
        >
        >
        return "Due: " + str(self.dateDu e) + \
        "\nDate: " + str(self.dateCr eated) + \
        "\nSubject: " + self.subject + \
        "\n" + self.content
        >
        The fields "dateDue" and "dateCreate d" contain datetime.date objects.
        Now when I try to serialize the whole thing:
        >
        >>myNote
        >
        Traceback (most recent call last):
        File "<stdin>", line 1, in ?
        File "notes.py", line 81, in __repr__
        return "Due: " + str(self.dateDu e) + \
        TypeError: cannot concatenate 'str' and 'datetime.date' objects
        >
        I tryed different variant before I wrapped "self.dateD ue" in a str()
        constructor:
        I tried to put it in brackets, or call its .isoformat() method.
        Nothing works. It still complains that I was trying to concatenate a
        string with a date, but I really wanna concatenate a string with a
        string!
        >
        Could anyone please tell me what I am doing wrong?
        >
        Not showing us the *whole* code that you are executing.
        >
        Suggestions:
        (1) Maybe dates have crept into self.subject and self.content. The
        print statements above should show exactly what you've got.
        >
        (2) Maybe you didn't save it before you ran it. Maybe you forgot to
        reload(yourModu le). Try making up the smallest possible script that
        demonstrates your problem. Get out of whatever IDE you may be using
        and go to the OS command-line prompt. Use type (windows) or cat (*x)
        to print your script on the console. Run your script. Copy the console
        contents and paste it into your mail/news client.
        >
        HTH,
        John
        Ah, yes point (2) is it. I didn't do "reload" but simply "del
        mylibrary, myobject" and the import the library again.
        In effect the code quoted in the error message was up to date but the
        code used was still the cached bytecode. Some problems do in fact
        solve themselves when you sleep a night over it, it seems.

        Thank you. Good fight, good night!

        Comment

        Working...