小编mlu*_*bke的帖子

在python MySQLDB IN子句中使用列表

我知道如何将列表映射到字符串:

foostring = ",".join( map(str, list_of_ids) )
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用以下内容将该字符串转换为IN子句:

cursor.execute("DELETE FROM foo.bar WHERE baz IN ('%s')" % (foostring))
Run Code Online (Sandbox Code Playgroud)

我需要的是使用MySQLDB安全地完成同样的事情(避免SQL注入).在上面的示例中,因为foostring不作为参数传递来执行,所以它很容易受到攻击.我还必须在mysql库之外引用并转义.

(有一个相关的SO问题,但那里列出的答案要么对MySQLDB不起作用,要么易受SQL注入攻击.)

python mysql

75
推荐指数
2
解决办法
3万
查看次数

使用LIKE vs. =进行精确的字符串匹配

首先,我认识到两者之间的差异:
- 比较可用的通配符%和_
- 重要的尾随空白
- 一致的问题

所有其他条件相同,对于更有效的精确字符串匹配:

SELECT field WHERE 'a' = 'a';
Run Code Online (Sandbox Code Playgroud)

要么:

SELECT field WHERE 'a' LIKE 'a';
Run Code Online (Sandbox Code Playgroud)

或者:差异是如此微不足道,以至于无关紧要?

mysql string performance

32
推荐指数
2
解决办法
5万
查看次数

python中的抽象类+ mixin +多重继承

所以,我认为代码可能解释了我想要做的比我言语更好,所以这里是:

import abc

class foo(object):
    __metaclass__ = abc.ABCMeta
    @abc.abstractmethod
    def bar(self):
        pass


class bar_for_foo_mixin(object):
    def bar(self):
        print "This should satisfy the abstract method requirement"


class myfoo(foo, bar_for_foo_mixin):
    def __init__(self):
        print "myfoo __init__ called"
        self.bar()

obj = myfoo()
Run Code Online (Sandbox Code Playgroud)

结果:

TypeError: Can't instantiate abstract class myfoo with abstract methods bar
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用mixin类来满足abstract/interface类的要求.我错过了什么?

python abstract-class multiple-inheritance mixins

22
推荐指数
1
解决办法
1万
查看次数

python MySQLDB查询超时

我试图在python MySQLDB中强制执行查询的时间限制.我有一种情况,我无法控制查询,但需要确保它们不会超过设定的时间限制.我已经尝试使用signal.SIGALRM中断执行的调用,但这似乎不起作用.信号被发送,但是在执行完成调用之后才会被捕获.

我写了一个测试用例来证明这种行为:

#!/usr/local/bin/python2.6

import time
import signal

from somewhere import get_dbc

class Timeout(Exception):
    """ Time Exceded """

def _alarm_handler(*args):
    raise Timeout

dbc = get_dbc()

signal.signal(signal.SIGALRM, _alarm_handler)
signal.alarm(1)

try:
    print "START:  ", time.time()
    dbc.execute("SELECT SLEEP(10)")
except Timeout:
    print "TIMEOUT!", time.time()'
Run Code Online (Sandbox Code Playgroud)

"SELECT SLEEP(10)"模拟慢速查询,但我确实看到了与实际慢速查询相同的行为.

结果:

START:   1254440686.69
TIMEOUT! 1254440696.69
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,它已经睡了10秒然后我得到了Timeout Exception.

问题:

  1. 为什么在执行完成之后我才收到信号?
  2. 是否有另一种可靠的方法来限制查询执行时间?

python mysql timeout

9
推荐指数
1
解决办法
1万
查看次数

在python中排序字典列表

我有一个字典的python列表:

mylist = [
{'id':0, 'weight':10, 'factor':1, 'meta':'ABC'},
{'id':1, 'weight':5, 'factor':1, 'meta':'ABC'},
{'id':2, 'weight':5, 'factor':2, 'meta':'ABC'},
{'id':3, 'weight':1, 'factor':1, 'meta':'ABC'}
]
Run Code Online (Sandbox Code Playgroud)

什么是最有效/最干净的方式来按重量排序该列表然后因素(数字).结果列表应如下所示:

mylist = [
{'id':3, 'weight':1, 'factor':1, 'meta':'ABC'},
{'id':1, 'weight':5, 'factor':1, 'meta':'ABC'},
{'id':2, 'weight':5, 'factor':2, 'meta':'ABC'},
{'id':0, 'weight':10, 'factor':1, 'meta':'ABC'},
]
Run Code Online (Sandbox Code Playgroud)

python dictionary list

8
推荐指数
1
解决办法
682
查看次数