The Artima Developer Community
Sponsored Link

Python Answers Forum
Date Type?

8 replies on 1 page. Most recent reply: Jan 11, 2005 5:32 AM by Girts Kalnins

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 8 replies on 1 page
Maverick

Posts: 11
Nickname: maverick43
Registered: Oct, 2004

Date Type? Posted: Nov 9, 2004 1:02 PM
Reply to this message Reply
Advertisement
How do i find my input value is of Datetime type?
For strings i could use
If type(i)==type('string')..

How do i approach the same for Dates or Datetime.

I am using the following :

import datetime

i=raw_input("Enter value : ")
if type(i)==type('string'):
print ...
elif type(i)==datetime.datetimetype:
print..



But i am getting the following error mssg

Traceback (most recent call last):
File "C:\Sree\Python\test_class.py", line 10, in -toplevel-
stmt1=testcur.Insert("test",flist,values)
File "C:\Sree\Python\baseCursor1.py", line 50, in Insert
elif type(i)==datetime.DateTimeType:
AttributeError: 'module' object has no attribute 'DateTimeType'

Thanks,


Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: Date Type? Posted: Nov 10, 2004 3:35 AM
Reply to this message Reply
Hey there,

Have a look at the builtin functions documentation for raw_input (http://python.org/doc/2.3.4/lib/built-in-funcs.html). raw_input() *always* returns a string.

So you're going to have to figure out a way of turning that string into a date object (if that's what you want).

Maverick

Posts: 11
Nickname: maverick43
Registered: Oct, 2004

Re: Date Type? Posted: Nov 10, 2004 7:01 AM
Reply to this message Reply
Andy,
My real problem is to find if the value i have, is Date datatype or string data type.


I am trying to find out, using Type(), whether the input(let's say an argument passed to current module/class) is date or string? if date, then i can format the value into a valid date format and insert into table. That's the idea.

Thanks ,

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Date Type? Posted: Nov 10, 2004 10:47 AM
Reply to this message Reply
type() won't help you out in this case. As Andy said, the data type returned from raw_input() is always string, whether it contains "11/10/2004" or "09:12:03 pm" or "Bob" or "123" or "anything else."

It sounds like what you want to do is convert a string that contains a date to a datetime object. I haven't done this particular thing (in Python, at least, as far as I remember...), so I'm not sure if the datatime module contains a facility to do this for you, or whether you need to use a regular expression (in the re module). datetime does have a fromtimestamp() method, which will help if the date/time string is in that format (see the Python documentation of datetime and/or experiment with it in IDLE).

I would also search the Python Cookbook (http://www.google.com/search?q=Python+Cookbook), as I'm sure you are not the first person to encounter this problem.

Andy

Posts: 28
Nickname: adn
Registered: Jul, 2004

Re: Date Type? Posted: Nov 10, 2004 1:16 PM
Reply to this message Reply
(back from work)

I was going to talk about parsing strings for times ...

Yep, the facility exists in the time module. Behold:

>>> import time
>>> time.strptime('May 2004', '%b %Y')
(2004, 5, 1, 0, 0, 0, 5, 122, -1)

http://www.python.org/doc/current/lib/module-time.html

Have a look at the docs for strptime and strftime. They'll help you parse & create dates from tuples.

Also the mxDateTime package from http://www.egenix.com/files/python/mxDateTime.html comes recommended for manipulating time and dates.

Of course, you have to know what format the date will be in when the user types it in (or try many) and how you will tell it apart from a non-date string.

Maverick

Posts: 11
Nickname: maverick43
Registered: Oct, 2004

Re: Date Type? Posted: Nov 12, 2004 6:44 AM
Reply to this message Reply
Let me rephrase my question, i think by using raw_input in my code i am confusing you .

I just want to know if the argument i passed from main modeule to class B is Date Datatype, is there any way?
I can find if it's a string datatype , but how to find if it's a date datatype. I am not interested in it's format.

Any ideas?

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Date Type? Posted: Nov 12, 2004 8:39 AM
Reply to this message Reply
Change your code to this:
...
   elif type(i)==datetime.datetime:
...


By the way, if you use dir() on the datetime module, you'll see there is no "datetimetype" in the module:
>>> dir(datetime)
['MAXYEAR', 'MINYEAR', '__doc__', '__file__', '__name__', 'date', 'datetime', 'time', 'timedelta', 'tzinfo']

Maverick

Posts: 11
Nickname: maverick43
Registered: Oct, 2004

Re: Date Type? Posted: Nov 12, 2004 1:14 PM
Reply to this message Reply
You are correct. type(i)=datetime.datetime did work.
I have found some where that DateTime.DateTimeType was being used for checking the date type. I guess it depends on what DBI we are using :-(

Thanks for all your help.

Girts Kalnins

Posts: 23
Nickname: smejmoon
Registered: May, 2003

Re: Date Type? Posted: Jan 11, 2005 5:32 AM
Reply to this message Reply
Are you sure you know what you are doing?

I would suggest using isinstance, because then you can test against class expected and also it's subclasses.


>>> class sup:pass
...
>>> class sub(sup):pass
...
>>> var = sub()
>>> isinstance(var, sub)
True
>>> isinstance(var, sup)
True
>>> var.__class__ == sub
True
>>> var.__class__ == sup
False
>>> type(sub)
<type 'classobj'>
>>> type(var)
<type 'instance'>
>>> class other:pass
...
>>> another = other()
>>> type(another) == type(var)
True
>>>

Flat View: This topic has 8 replies on 1 page
Topic: Python course/training in Singapore? Previous Topic   Next Topic Topic: Importing module which contains Dot

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use