Let's start.
a = 0.7
b = 0.3
a - b = 0.4
if a - b == 0.4:
print ' a - b == 0.4'
else:
print 'a - b != 0.4'
Result a - b != 0.4
OH WHAT'S...
Solution:
if str(a -b) == str(0.4):
print 'a - b == 0.4'
Read Python Doc for more information
Monday, December 14, 2009
Monday, December 7, 2009
Query with variable and AND
It is
query =db.GqlQuery("SELECT * FROM table WHERE 1st_attribute =:variable AND 2nd_attribute ='value' ", variable = data)
query =db.GqlQuery("SELECT * FROM table WHERE 1st_attribute =:variable AND 2nd_attribute ='value' ", variable = data)
Tuesday, December 1, 2009
invalid literal for int with base 10
/?p=33
I get quite some traffic for the term “invalid literal for int with base 10″. I guess a lot people fumbling around with Python or Django are getting this error message once in a while. This happens when you try to cast a string into an integer and this string does not really contain a “digit”:
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> string = “TestString”
>>>number = int(string)
Traceback (most recent call last):
File “”, line 1, in
ValueError: invalid literal for int() with base 10: ‘TestString’
>>>string = “1″
>>>number = int(string)
>>>number
1
If you work with the Django framework and get this error while trying to request a web page I would check the parameters. Since every parameter that is getting passed in a request is a string you probably cast them into integers somewhere in your source code. It can be a category id or similar. Now when that category id for whatever reason is empty or does not contain a string that can be cast into an integer, you will get the “invalid literal for int with base 10″ error message.
To avoid this error you should check if the string contains a digit:
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> string = “1″
>>> if string.isdigit():
… number = int(string)
… else:
… print “The variable String contains no digit”
…
>>> number
1
str.isdigit()
Return true if all characters in the string are digits and there is at least one character, false otherwise.
For 8-bit strings, this method is locale-dependent.
More on String operations can be found in the Python documentation.
I get quite some traffic for the term “invalid literal for int with base 10″. I guess a lot people fumbling around with Python or Django are getting this error message once in a while. This happens when you try to cast a string into an integer and this string does not really contain a “digit”:
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> string = “TestString”
>>>number = int(string)
Traceback (most recent call last):
File “
ValueError: invalid literal for int() with base 10: ‘TestString’
>>>string = “1″
>>>number = int(string)
>>>number
1
If you work with the Django framework and get this error while trying to request a web page I would check the parameters. Since every parameter that is getting passed in a request is a string you probably cast them into integers somewhere in your source code. It can be a category id or similar. Now when that category id for whatever reason is empty or does not contain a string that can be cast into an integer, you will get the “invalid literal for int with base 10″ error message.
To avoid this error you should check if the string contains a digit:
Python 2.6 (r26:66721, Oct 2 2008, 11:35:03) [MSC v.1500 32 bit (Intel)] on win32
Type “help”, “copyright”, “credits” or “license” for more information.
>>> string = “1″
>>> if string.isdigit():
… number = int(string)
… else:
… print “The variable String contains no digit”
…
>>> number
1
str.isdigit()
Return true if all characters in the string are digits and there is at least one character, false otherwise.
For 8-bit strings, this method is locale-dependent.
More on String operations can be found in the Python documentation.
Wednesday, November 25, 2009
Count Instance method of Query Class
from google.appengine.ext import db
from google.appengine.api import users
class FirstModel(db.Model):
prop = db.IntegerProperty()
class SecondModel(db.Model):
prop2 = db.IntegerProperty()
reference = db.ReferenceProperty(FirstModel, collection_name = "relation")
//Input data to datastore
firstmodel = FirstModel(prop = 06)
firstmodel.put()
secondmodel = SecondModel(prop2 = 1, reference = firstmodel)
secondmodel.put()
secondmodel = SecondModel(prop2 = 2, reference = firstmodel)
secondmodel.put()
//Count
query = firstmodel.relation
query_count = query.count()
print query_count
OR
query = FirstModel.all()
query_count = query.count()
print query_count
Result:
2
Google app engine simple query
from google.appengine.ext import db
from google.appengine.api import users
class FirstModel(db.Model):
prop = db.IntegerProperty()
class SecondModel(db.Model):
prop2 = db.IntegerProperty()
reference = db.ReferenceProperty(FirstModel, collection_name = "relation")
// Put data to datastore
firstmodel = FirstModel(prop = 06)
firstmodel.put()
secondmodel = SecondModel(prop2 = 1, reference = firstmodel)
secondmodel.put()
secondmodel = SecondModel(prop2 = 2, reference = firstmodel)
secondmodel.put()
Get all entity by using collection_name
query = firstmodel.relation
query = query.fetch(30)
print query
The Result
2 entity are displayed.
[<__main__.SecondModel object at 0x020736B0>, <__main__.SecondModel object at 0x02073790>]
from google.appengine.api import users
class FirstModel(db.Model):
prop = db.IntegerProperty()
class SecondModel(db.Model):
prop2 = db.IntegerProperty()
reference = db.ReferenceProperty(FirstModel, collection_name = "relation")
// Put data to datastore
firstmodel = FirstModel(prop = 06)
firstmodel.put()
secondmodel = SecondModel(prop2 = 1, reference = firstmodel)
secondmodel.put()
secondmodel = SecondModel(prop2 = 2, reference = firstmodel)
secondmodel.put()
Get all entity by using collection_name
query = firstmodel.relation
query = query.fetch(30)
print query
The Result
2 entity are displayed.
[<__main__.SecondModel object at 0x020736B0>, <__main__.SecondModel object at 0x02073790>]