Wednesday, February 13, 2013

Reorder/Reset auto increment primary key?


Following query can be used for reorder auto increment of a table.In this example reorder the id column of users table. If there are foreign key, make sure the action is cascade.

SET @count = 0;
UPDATE `users` SET `users`.`id` = @count:= @count + 1;

Tuesday, February 12, 2013

locks in python thread

When doing multi threading program in any language, it is a big problem when having shared variables or some shared objects. In this kind of situation, it is required to lock some variables until finish the some process of one thread. In order to do this we can use locks. In python there is a library for import in order to do this.

ex:

from threading import  Lock

......
....

#initialize lock
lock=Lock()

.....

#when needed to add lock
lock.acquire()
#do the stuff
#remove lock
lock.release()

copy object to another object using python

it is really easy to copy object with all attributes to another object in python. All you need to import copy library.

ex:

import copy

class Test:
       def __init__(self,name):
              self.name=name

def main():
       a=Test('madura')
       b=copy.deepcopy(a)

This will copy Test class object a, to Test class object b with the attribute values

Monday, February 11, 2013