473,566 Members | 2,784 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Apr 21 '07 #1
3 8035
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

Apr 21 '07 #2
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

Apr 22 '07 #3
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!

Apr 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5689
by: Shawn Wilson | last post by:
Hi, My host keeps messing things up and not telling me. I am trying to write a cron job that will test to make sure the "unusual" functions I use work and, if not, email me. I want it to send me the build date as well. The weird thing is, my cron script (using phpinfo) gives me a different build date than phpinfo() in an "admin tool"...
0
2045
by: Nick Thurn | last post by:
Folks, I'm trying to define a oracle Java Stored Proc interface to an existing timeseries database. Series are accessed by name and are returned as vectors of type pairs, specifically (Date, number) (Date, date), (date, string), (number, number), (number, date) or (number,string). I could simply create all the types and their table types
2
4275
by: Duncan Welch | last post by:
Hi, I've got a fairly simple date control, that I'm creating dynamically in my page. The reason for creating it dynamically is that I want the ID to vary, depending on it's situation. The control loads on the page, and the set date is shown. The problem is, when I submit the page, the control doesn't return the date. What am I doing...
4
2808
by: Earl T | last post by:
When I try to get the netscape version for version 7, I get the HttpBrowserCapabilities class returning the version as 5 and not 7. (see code and output below) CODE HttpBrowserCapabilities bc; string s; bc = Request.Browser; ....
5
10362
by: LS | last post by:
Can a WebMethod return an Interface type? Can we pass an interface parameter ? Example : public interface IEntity { long Id { get; set; } string Name { get; set; } }
1
2055
by: Matthias De Ridder | last post by:
Hello, I really hope that someone will be able to help me, because I'm desperate now! I'm a student, graduating this year, and I'm working on a thesis where C# Web Services are involved. I only have three weeks to finish it all! My GUI and Web services were finished, but I hadn't tested them. So I linked the GUI to the Web service and...
5
19570
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was having a problem in the calling program. I searched online and found suggestions that I return an Array instead so I modified my code (below) to return...
6
7644
by: SQL Server | last post by:
I've been working this for a while. Kind of new to SQL Server functions and not seeing what I am doing wrong. I have this function CREATE FUNCTION dbo.test (@Group varchar(50)) RETURNS smalldatetime AS BEGIN Declare @retVal varchar(10) (SELECT @retVal= MIN() FROM dbo.t_master_schedules WHERE (event_id = 13) AND (group_ =@Group))...
11
1977
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I have the method below that returns a bool, true or false depending on if the conversion to date tiem works. It takes a string input. I am only returning the bool but would also like to return the string message ex.message, just wondering how to do this since I think you can only return one thing with the function. Thanks. public...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.