0

I have the following class methods:

def queryCollection(self, query_string={}, distinct_output = "_id"):
    output_array = []
    for property in self.coll.find(query_string).distinct(distinct_output):
        output_array.append(property)
    return set(output_array)

def smallDateQuery(self):
    x = self.queryCollection( { "created_at": {"$gte" : datetime(2015, 3, 1), "$lt": datetime(2015, 3, 30)}} )
    return x

When I call the first one, it works:

x = user.queryCollection({ "created_at": {"$gte" : datetime(2015, 3, 1), "$lt": datetime(2015, 3, 30)}})
print len(x)

When I call the second, it does not:

y = user.smallDateQuery()
print len(y)
quit()

I get the following error:

 x = self.queryCollection( { "created_at": {"$gte" : datetime(2015, 3, 1), "$lt": datetime(2015, 3, 30)}} )
TypeError: 'module' object is not callable

What is the issue?

DBWeinstein
  • 8,605
  • 31
  • 73
  • 118
  • 1
    Have you tried splitting it into more separate steps so see what the uncallable object is? How did you import `datetime`? – jonrsharpe May 07 '15 at 15:02
  • 1
    nice catch!!! on the class module, I just had `import Datetime` but on the main module I had `from datetime import datetime`. Changed the class module and it worked. Thanks! – DBWeinstein May 07 '15 at 15:05
  • 2
    Incidentally, since you're putting the contents of `output_array` into a set, why not do it directly? Eg, `output_set = set()` `for property in thingy: output_set.add(property)`. Also, beware of the [dangers of default mutable arguments](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument), although it doesn't matter here since you don't modify `query_string` in `queryCollection()`. – PM 2Ring May 07 '15 at 15:15

1 Answers1

1

You probably have

import datetime

in which case you should use

datetime.datetime(2015, 3, 1)

The error arises because datetime is a module, the function to call is datetime.datetime.

stellasia
  • 5,372
  • 4
  • 23
  • 43