← All posts tagged Python

Skyrzn
Python PyQt Надоело каждый раз преобразовывать QString в str (с str мне работать удобнее). Для этого приходится делать что-то типа:
str(self._usernameEdit.text().toUtf8()).
Эта проблема решается одной строкой:
QString.toStr = lambda self: str(self.toUtf8())
, которую надо выполнить один раз где-нибудь в начале программы.
В дальнейшем преобразовывание выглядит так:
self._usernameEdit.text().toStr()
Python рулит! :)
Skyrzn
Python SQL говнокод Увидел сегодня в триггере таблицы просто феерический код. У кого-то из коллег явно в роду были индусы.

if (TD['new']['board'] is None) and (TD['new']['place'] is None) and (TD['new']['vendor_parts'] is None):
if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board is NULL)and(place is NULL)and(vendor_parts is NULL)and(model_id=%s)and(id<>%s))"%(TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

if (TD['new']['board'] is not None) and (TD['new']['place'] is None) and (TD['new']['vendor_parts'] is None):
if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board='%s')and(place is NULL)and(vendor_parts is NULL)and(model_id=%s)and(id<>%s))"%(TD['new']['board'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

if (TD['new']['board'] is None) and (TD['new']['place'] is not None) and (TD['new']['vendor_parts'] is None):
if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board is NULL)and(place='%s')and(vendor_parts is NULL)and(model_id=%s)and(id<>%s))"%(TD['new']['place'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

if (TD['new']['board'] is not None) and (TD['new']['place'] is not None) and (TD['new']['vendor_parts'] is None):
if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board='%s')and(place='%s')and(vendor_parts is NULL)and(model_id=%s)and(id<>%s))"%(TD['new']['board'], TD['new']['place'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

if (TD['new']['board'] is None) and (TD['new']['place'] is None) and (TD['new']['vendor_parts'] is not None):
if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board is NULL)and(place is NULL)and(vendor_parts='%s')and(model_id=%s)and(id<>%s))"%(TD['new']['vendor_parts'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

if (TD['new']['board'] is not None) and (TD['new']['place'] is None) and (TD['new']['vendor_parts'] is not None):
if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board='%s')and(place is NULL)and(vendor_parts='%s')and(model_id=%s)and(id<>%s))"%(TD['new']['board'], TD['new']['vendor_parts'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

if (TD['new']['board'] is None) and (TD['new']['place'] is not None) and (TD['new']['vendor_parts'] is not None):
if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board is NULL)and(place='%s')and(vendor_parts='%s')and(model_id=%s)and(id<>%s))"%(TD['new']['place'], TD['new']['vendor_parts'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'

if (TD['new']['board'] is not None) and (TD['new']['place'] is not None) and (TD['new']['vendor_parts'] is not None):
if plpy.execute("SELECT count(*) FROM rma.component_specification where ((board='%s')and(place='%s')and(vendor_parts='%s')and(model_id=%s)and(id<>%s))"%(TD['new']['board'], TD['new']['place'], TD['new']['vendor_parts'], TD['new']['model_id'], TD['new']['id']))[0]['count'] > 0 :
raise Exception, 'Model, place, board and vendor_parts must be unique! Record for this combination already exists.'
Skyrzn
Python Мдааа... Только я мог в питоне допустить утечку памяти. Никто не знает, как получить все ссылки на объект? Судя по всему, кто-то продолжает ссылаться и не дает сборщику мусора этот объект удалить. Гугл что-то молчит.