I am facing the following issue which I didnt came across in my other application.
My Sprint datamodel:
import mongoengine as me
class Sprint(me.Document):
start_date = me.DateTimeField(),
end_date = me.DateTimeField(),
sequence = me.IntField(required=True, default=0)
In shell I tried following:
sprint = Sprint.objects.get(sequence=1)
sprint
<Sprint: Sprint object>
sprint.start_date - datetime.utcnow()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'tuple' and 'datetime.datetime'
then I printed sprint.start_date
it returned a tuple instead of datetime object as follows:
sprint.start_date
(<mongoengine.fields.DateTimeField object at 0x22b7dd0>,)
So I did
sprint.start_date[0] - datetime.utcnow()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'DateTimeField' and 'datetime.datetime'
I didn't get this issue any of my earlier project. My current mongoengine version is 0.6.20. I didn't upgarded it for my current project and has been using this for all of my projects.
I am using tornado as the web server
How to convert mongoengine datetime field to compatible to python datetime instance.
Thanks in advance for your help.
The error is in the datamodel definition itself. A type caused all these. :(
start_date and end_date have a ',' in the end which is creating the noise.
After removing it, it rolls. :)
Related
So i've been wondering around the other day and stumbled across this piece of code from datetime.datetime:
def timestamp(self):
"Return POSIX timestamp as float"
if self._tzinfo is None:
s = self._mktime()
return s + self.microsecond / 1e6
else:
return (self - _EPOCH).total_seconds()
I decided to check what self._tzinfo and self._mktime() are, so i ran this code in console:
>>> datetime(1970, 1, 1)._tzinfo
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute '_tzinfo'
>>> datetime(1970, 1, 1)._mktime()
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: 'datetime.datetime' object has no attribute '_mktime'
But to my surprise i got AttributeError on both occasions. PyCharm says Cannot find declaration to go to. Yet, there is a normal declaration of _mktime() method right above the timestamp() method.
Despite all this, of course, timestamp() works totally fine.
>>> datetime(1970, 1, 1).timestamp()
-10800.0
So why is this happening? Why are these attributes not accessible from outside and why does interpreter react to them so weirdly?
I am working on a project in Python. I am a beginner and I am getting this error when I am running the program.
Traceback (most recent call last):
File "E:/Python/1616/checkProfile.py", line 104, in <module>
p.getResults()
File "E:\Python\1616\Profile.py", line 67, in getResults
for i in range(2): self._s[1] += e.getS[1]
TypeError: 'float' object has no attribute '__getitem__'
Error in Line 67
http://pastebin.com/HXvppfmU
to check what are the methods allowed for datatypes,
dir(datatype)
# As for float, try dir(float)
#This will suggest you that there is no method called __getitem__ for float.
# You might be trying to get some data or you are using []/()/{} which is not correct.
Try to post ur code.
i am learning python boto module and i am trying to stop a running instance .
import boto.ec2
conn = boto.ec2.connect_to_region("us-west-2")
conn.stop_instances(instance_ids=['i-0aa5ce441ef7e0e2a'])
but i am getting error which says:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'stop_instances'
i gave AWS_access keys for boto.
can anyone please help me to fix this error ?
As #kindall points out, your conn object is not initialized (it's NoneType). I also see that you're using boto in the example, but I thought that I would provide an example of this using boto3:
import boto3
boto3_session = boto3.Session(profile=some_profile_you_configured)
boto3_client = boto3_session.client('ec2', region_name='us-west-2')
response = boto3_client.stop_instances(InstanceIds=['i-0aa5ce441ef7e0e2a'])
I'm using using SQLAlchemy to fiter but getting an error:
user = session.query.filter(User.id == 99).one()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'function' object has no attribute 'filter'
Does someone know how to filter because on SQLAlchemy Page, I saw this:
query = session.query(User).filter
query is a function, you need to pass in the User class to call it:
user = session.query(User).filter(User.id == 99).one()
^^^^^^
SQLAlchemy cannot divine from the filter alone what type of object you want returned otherwise.
when I execute the following code using python programming language and MySQL database
cursor.execute("select max(propernoun_SRNO) from tblauto_tagged")
starting_index = cursor.fetchone()
ending_index = starting_index +len(s)
I get following error:
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
batch(1,1)
File "C:\Users\vchauhan\Dropbox\Code\proper_noun_function_batch_file_mysql_sept_12.py", line 97, in batch
ending_index = starting_index +len(s)
TypeError: unsupported operand type(s) for +: 'pyodbc.Row' and 'int'
Problem
The problem here is that you are assigning pyodbc.Row instance (returned by .fetchone()) to starting_index, which makes it impossible to add it to the integer (thus the "TypeError: unsupported operand type(s)" error).
Solution
Try to replace this line:
starting_index = cursor.fetchone()
with this line:
starting_index = cursor.fetchone()[0]
More reading
PEP 249 - Python Database API Specification v2.0 (especially part about fetchone())