class datetime.datetime
A combination of a date and a time. Attributes: year, month, day,
hour, minute, second, microsecond, and tzinfo.
What does this mean? How do I use it?
For instance, I have a DateTimeProperty mDate that I get from a query
in Google App Engine. This mDate has value
mDATE = 2010-11-10 14:35:22.863000
But when I try
datetime.datetime.mDATE.toordinal())
I get AttributeError.
If I try something like
td = mDATE.seconds
in GAE development server I get
AttributeError: 'datetime.datetime' object has no attribute 'seconds'
What am I doing wrong? And how to understand this stuff so that I can
start using the language instead of trying to figure out types. Thanks
for your help.
> Wait a minute i am confused...? Does Python have a "text" object that
> magically turns into a datetime object?
>
> >>> mDATE = 2010-11-10 14:35:22.863000
>
> SyntaxError: invalid syntax
This is the reason I am asking the question. I am confused about what
mDATE is. It is defined as a DateTimeProperty in Google App Engine in
the following model:
class Rep(db.Model):
mAUTHOR = db.UserProperty(auto_current_user=True)
mUNIQUE = db.StringProperty()
mCOUNT = db.IntegerProperty()
mDATE = db.DateTimeProperty(auto_now=True)
mDATE0 = db.DateTimeProperty(auto_now_add=True)
mWEIGHT = db.IntegerProperty()
When I acces it with this query:
QUERY = Rep.all()
QUERY.filter("mAUTHOR =", user)
QUERY.order("-mCOUNT")
RESULTS = QUERY.fetch(10)
But when I try to use like this for instance:
for result in RESULTS:
WEIGHT = (datetime.datetime.result.mDATE.toordinal()) *
result.mCOUNT
I get AttributeError
or if I try to use it like this
dt = result.mDATE.seconds
All good info -- I'd add that you should learn and become comfortable with:
import pdb; pdb.set_trace()
to test running code.
Emile
Note 'second' singular, as with other attributes.
> td = mDATE.seconds
You added a 's' to 'second', so
> in GAE development server I get
> AttributeError: 'datetime.datetime' object has no attribute 'seconds'
--
Terry Jan Reedy
Others have already explained why "mDATE.seconds" does not work, but I
wanted to touch on this as well. The above fails because "mDATE" is
not an attribute of the "datetime.datetime" class, which is to be
expected since you've merely defined it as a local variable. If you
want to call the "toordinal" method of mDATE, just do this:
mDATE.toordinal()
or this:
datetime.datetime.toordinal(mDATE)
But in most cases the first version is preferred.
As an aside, your capitalization scheme would drive me insane. I
suggest reading PEP 8, the Python style guide, and following the
recommendations there. Also, it is not customary in Python to prefix
attribute names with the string "m" or "m_" as it is in C++. Because
Python does not do implicit attribute lookup, it is always clear from
context whether a name refers to an attribute or not, and so the "m"
is superfluous.
Cheers,
Ian