Instead:
i=0 end=20 last_id=0 data=[] while(i<=end): i = i + 1 ...
the code:
last_id=0 data=[] for i in xrange(1, 22): ...
The same semantics, more compact and Pythonic.
Instead
if not last or last == None:
just do
if not last:
since None is false-ish anyway (so not last is True when last is None). In general, when you want to check if something is None). In general, when you want to check if something is None , code is None , not == None`.
IN
if(j['id'] <> last_id):
lose extra parentheses and instead of the deprecated operator <> and code
if j['id'] != last_id:
and remove extra parentheses from other if .
Instead:
if len(data) == 0:
the code:
if not data:
since any empty container is false-ish.
IN
hash_str = str(hash.hexdigest())
instead of this
hash_str = hash.hexdigest()
since the method already returns a string, making str call redundant.
Instead:
for item in data: writer.writerow(item)
using
writer.writerows(data)
which executes the loop on your behalf.
Instead
ofile = open('tmp/'+file_name, mode='ab') ... ofile.close()
use (in Python 2.6 or better, or in 2.5, starting the module with
from __future__ import with_statement
to "import from the future" function of the with ) operator:
with open('tmp/'+file_name, mode='ab') as ofile: ...
which guarantees closure for you (including in cases where an exception may occur).
Instead
print "Upload Error: "+uploadr[0]
using
print "Upload Error:", uploadr[0]
and similarly for other print statements, a comma inserts space for you.
I am sure that there are more such trifles, but there are several that "jumped to the eye" when I looked at your code.