Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to read the Python tutorial?

11 views
Skip to first unread message

Zeynel

unread,
Nov 10, 2010, 10:13:06 AM11/10/10
to
For instance, when the tutorial has http://docs.python.org/release/2.6/library/datetime.html

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.

Message has been deleted

Zeynel

unread,
Nov 10, 2010, 11:05:55 AM11/10/10
to
On Nov 10, 10:51 am, rantingrick <rantingr...@gmail.com> wrote:

> 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

Message has been deleted

Emile van Sebille

unread,
Nov 10, 2010, 3:33:02 PM11/10/10
to pytho...@python.org
On 11/10/2010 8:19 AM rantingrick said...
> I would start at the loop and figure out what is going on from there
> with some print statements and the functions i showed you. Debugging
> is a large part of any programming project. We all do it everyday. So
> the sooner you learn the better. If you have a specific question feel
> free to ask, but general debug issues are better solved yourself.
>
> A few more indispensable functions are dir() and help()

All good info -- I'd add that you should learn and become comfortable with:

import pdb; pdb.set_trace()

to test running code.

Emile


MRAB

unread,
Nov 10, 2010, 3:50:00 PM11/10/10
to pytho...@python.org
If you lookup 'datetime' in Python's documentation you'll find that the
attribute is called 'second'.

Terry Reedy

unread,
Nov 10, 2010, 3:55:32 PM11/10/10
to pytho...@python.org
On 11/10/2010 10:13 AM, Zeynel wrote:
> For instance, when the tutorial has http://docs.python.org/release/2.6/library/datetime.html
>
> class datetime.datetime
> A combination of a date and a time. Attributes: year, month, day,
> hour, minute, second, microsecond, and tzinfo.

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

Ian

unread,
Nov 10, 2010, 4:54:08 PM11/10/10
to
On Nov 10, 8:13 am, Zeynel <azeyn...@gmail.com> wrote:
> But when I try
>
> datetime.datetime.mDATE.toordinal())
>
> I get AttributeError.

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

0 new messages