Reputation: 2830
I have a python script with Tkinter and I want to print in background each queries did in Sqlite database (just for the fun):
I have one Database Object:
import sqlite3
class Database():
def __init__(self):
try:
sqlite3.enable_callback_tracebacks(True)
self.connection = sqlite3.connect('databases.sqlite')
self.cursor = self.connection.cursor()
self.cursor.execute( """ CREATE TABLE .... """ )
except:
print('error in database connection')
def __del__(self):
self.cursor.close()
And a Task object
class Task():
def __init__(self, name="no_name"):
self.database = Database()
data = {"name" : name }
self.database.cursor.execute("INSERT INTO tasks(name) VALUES(:name)" , data )
self.database.connection.commit()
And when I did this new_task = Task('Hello')
I want an automatic outpout in CLI like this:
* executed in 4ms :
INSERT INTO tasks (name) VALUES('Hello');
Any idea? Thank you in advance!
Upvotes: 0
Views: 1124
Reputation: 2929
Is this what you are looking for? I've thought about using decorator, some kind of stopwatch
:
import time
def stopwatch(func):
def wrapper(*args,**kwargs):
start = time.time()
func(*args,**kwargs)
end = time.time()
timed = int((end - start)*1000)
print(timed)
return wrapper
But then I thought about context managers, maybe (I am not the right person to judge) more suitable for this kind of job. Borrowing code from [here][1] I've ended up with (haha) this:
class Timer:
def __enter__(self):
self.start = time.clock()
return self
def __exit__(self, *args):
self.end = time.clock()
# format as milliseconds
self.interval = int((self.end - self.start) * 1000)
class Task():
def __init__(self, name="no_name"):
data = {"name" : name }
sql_template = "INSERT INTO tasks(name) VALUES(:name)"
# do the database thingy inside the Timer context
with Timer() as t:
self.database = Database()
self.database.cursor.execute(sql_template, data)
self.database.connection.commit()
print("* executed in {}ms :".format(t.interval))
print(" {}".format(sql_template))
I've test it a little bit, but applying it to your case I might have done some mistakes as I had to change the Task
__init__
a little bit to be able to reuse the SQL command.
[1]: http://preshing.com/20110924/timing-your-code-using-pythons-with-statement/
Upvotes: 1